Re: [PATCH v6 14/17] lib/bootconfig: narrow offset type in xbc_init_node()

From: Google

Date: Mon Mar 16 2026 - 20:57:55 EST


On Sun, 15 Mar 2026 12:20:12 +0000
Josh Law <objecting@xxxxxxxxxxxxx> wrote:

> lib/bootconfig.c:415:32: warning: conversion to 'long unsigned int'
> from 'long int' may change the sign of the result [-Wsign-conversion]
>
> Pointer subtraction yields ptrdiff_t (signed long), which was stored in
> unsigned long. The offset is immediately checked against XBC_DATA_MAX
> (32767) and then truncated to uint16_t, so unsigned int is sufficient.
> Add an explicit cast on the subtraction to suppress the sign-conversion
> warning.
>
> Signed-off-by: Josh Law <objecting@xxxxxxxxxxxxx>
> ---
> lib/bootconfig.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/lib/bootconfig.c b/lib/bootconfig.c
> index 995c2ec94cbe..7296df003459 100644
> --- a/lib/bootconfig.c
> +++ b/lib/bootconfig.c
> @@ -412,7 +412,7 @@ const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
>
> static int __init xbc_init_node(struct xbc_node *node, char *data, uint16_t flag)
> {
> - unsigned long offset = data - xbc_data;
> + unsigned int offset = (unsigned int)(data - xbc_data);
>
> if (WARN_ON(offset >= XBC_DATA_MAX))

OK, then this can be changed to

long offset = data - xbc_data;

if (WARN_ON(offset < 0 || offset >= XBC_DATA_MAX))

The original code is to handle data < xbc_data case (in that
case, the offset is over LONG_MAX, so offset >= XBC_DATA_MAX
is also true.) Note that this is for catching broken pointer
to find program bug (WARN_ON is used for such case).

Thank you,

> return -EINVAL;
> --
> 2.34.1
>


--
Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>