[PATCH] hw/openrisc/openrisc_sim: Avoid false-positive overflow warning

Jan Kiszka posted 1 patch 1 month ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/859f333e-37f9-4e76-bf88-a69d0641626a@siemens.com
Maintainers: Jia Liu <proljc@gmail.com>, Stafford Horne <shorne@gmail.com>
hw/openrisc/openrisc_sim.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] hw/openrisc/openrisc_sim: Avoid false-positive overflow warning
Posted by Jan Kiszka 1 month ago
From: Jan Kiszka <jan.kiszka@siemens.com>

Resolves this build breakage:

../hw/openrisc/openrisc_sim.c: In function ‘openrisc_sim_init’:
../hw/openrisc/openrisc_sim.c:284:45: error: ‘__builtin___snprintf_chk’ output may be truncated before the last format character [-Werror=format-truncation=]
     snprintf(alias, sizeof(alias), "serial%d", uart_idx);
                                             ^
In file included from /usr/include/stdio.h:964:0,
                 from /data/qemu/include/qemu/osdep.h:114,
                 from ../hw/openrisc/openrisc_sim.c:21:
/usr/include/bits/stdio2.h:54:10: note: ‘__builtin___snprintf_chk’ output between 8 and 9 bytes into a destination of size 8
   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        __glibc_objsize (__s), __fmt,
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        __va_arg_pack ());
        ~~~~~~~~~~~~~~~~~

This is actually a false positive because uart_idx is 0..3, never larger
or even negative.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
 hw/openrisc/openrisc_sim.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/hw/openrisc/openrisc_sim.c b/hw/openrisc/openrisc_sim.c
index 880c8ebbb8..dcb1a7a00b 100644
--- a/hw/openrisc/openrisc_sim.c
+++ b/hw/openrisc/openrisc_sim.c
@@ -281,7 +281,7 @@ static void openrisc_sim_serial_init(Or1ksimState *state, hwaddr base,
         /* The /chosen node is created during fdt creation. */
         qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename);
     }
-    snprintf(alias, sizeof(alias), "serial%d", uart_idx);
+    snprintf(alias, sizeof(alias), "serial%u", uart_idx);
     qemu_fdt_setprop_string(fdt, "/aliases", alias, nodename);
 
     g_free(nodename);
-- 
2.51.0

Re: [PATCH] hw/openrisc/openrisc_sim: Avoid false-positive overflow warning
Posted by Peter Maydell 1 month ago
On Tue, 14 Oct 2025 at 17:03, Jan Kiszka <jan.kiszka@siemens.com> wrote:
>
> From: Jan Kiszka <jan.kiszka@siemens.com>
>
> Resolves this build breakage:
>
> ../hw/openrisc/openrisc_sim.c: In function ‘openrisc_sim_init’:
> ../hw/openrisc/openrisc_sim.c:284:45: error: ‘__builtin___snprintf_chk’ output may be truncated before the last format character [-Werror=format-truncation=]
>      snprintf(alias, sizeof(alias), "serial%d", uart_idx);
>                                              ^
> In file included from /usr/include/stdio.h:964:0,
>                  from /data/qemu/include/qemu/osdep.h:114,
>                  from ../hw/openrisc/openrisc_sim.c:21:
> /usr/include/bits/stdio2.h:54:10: note: ‘__builtin___snprintf_chk’ output between 8 and 9 bytes into a destination of size 8
>    return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
>           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>         __glibc_objsize (__s), __fmt,
>         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>         __va_arg_pack ());
>         ~~~~~~~~~~~~~~~~~
>
> This is actually a false positive because uart_idx is 0..3, never larger
> or even negative.
>
> Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
> ---
>  hw/openrisc/openrisc_sim.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/hw/openrisc/openrisc_sim.c b/hw/openrisc/openrisc_sim.c
> index 880c8ebbb8..dcb1a7a00b 100644
> --- a/hw/openrisc/openrisc_sim.c
> +++ b/hw/openrisc/openrisc_sim.c
> @@ -281,7 +281,7 @@ static void openrisc_sim_serial_init(Or1ksimState *state, hwaddr base,
>          /* The /chosen node is created during fdt creation. */
>          qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename);
>      }
> -    snprintf(alias, sizeof(alias), "serial%d", uart_idx);
> +    snprintf(alias, sizeof(alias), "serial%u", uart_idx);
>      qemu_fdt_setprop_string(fdt, "/aliases", alias, nodename);

If we're going to change this I think I'd prefer
   g_autofree char *alias = g_strdup_printf("serial%d", uart_idx);

(declaration at top of function as usual for our style)

thanks
-- PMM