[PATCH] target/riscv: Fix build errors

Cédric Le Goater posted 1 patch 3 weeks, 5 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260112161626.1232639-1-clg@redhat.com
Maintainers: Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <dbarboza@ventanamicro.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>
target/riscv/cpu.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] target/riscv: Fix build errors
Posted by Cédric Le Goater 3 weeks, 5 days ago
Newer gcc compiler (version 16.0.0 20260103 (Red Hat 16.0.0-0) (GCC))
detects a truncation error:

  ../target/riscv/cpu.c: In function ‘riscv_isa_write_fdt’:
  ../target/riscv/cpu.c:2916:35: error: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 5 [-Werror=format-truncation=]
   2916 |     snprintf(isa_base, maxlen, "rv%di", xlen);
        |                                   ^~
  ../target/riscv/cpu.c:2916:32: note: directive argument in the range [-2147483648, 2147483632]
   2916 |     snprintf(isa_base, maxlen, "rv%di", xlen);
        |                                ^~~~~~~

Since the xlen variable represents the register width (32, 64, 128) in
the RISC-V base ISA name, mask its value with a 8-bit bitmask to
satisfy the size constraints on the snprintf output.

Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Alistair Francis <alistair.francis@wdc.com>
Cc: Weiwei Li <liwei1518@gmail.com>
Cc: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
Cc: Liu Zhiwei <zhiwei_liu@linux.alibaba.com>
Signed-off-by: Cédric Le Goater <clg@redhat.com>
---
 target/riscv/cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c
index ffd98e8eed468645b8ceb8a7adb45718bdc4444d..e95eea024939d2cbb5747781c291fed0e1b07bb9 100644
--- a/target/riscv/cpu.c
+++ b/target/riscv/cpu.c
@@ -2913,7 +2913,7 @@ void riscv_isa_write_fdt(RISCVCPU *cpu, void *fdt, char *nodename)
     riscv_isa = riscv_isa_string(cpu);
     qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", riscv_isa);
 
-    snprintf(isa_base, maxlen, "rv%di", xlen);
+    snprintf(isa_base, maxlen, "rv%di", xlen & 0xFF);
     qemu_fdt_setprop_string(fdt, nodename, "riscv,isa-base", isa_base);
 
     isa_extensions = riscv_isa_extensions_list(cpu, &count);
-- 
2.52.0


Re: [PATCH] target/riscv: Fix build errors
Posted by Cédric Le Goater 3 weeks, 5 days ago
On 1/12/26 17:16, Cédric Le Goater wrote:
> Newer gcc compiler (version 16.0.0 20260103 (Red Hat 16.0.0-0) (GCC))
> detects a truncation error:
> 
>    ../target/riscv/cpu.c: In function ‘riscv_isa_write_fdt’:
>    ../target/riscv/cpu.c:2916:35: error: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 5 [-Werror=format-truncation=]
>     2916 |     snprintf(isa_base, maxlen, "rv%di", xlen);
>          |                                   ^~
>    ../target/riscv/cpu.c:2916:32: note: directive argument in the range [-2147483648, 2147483632]
>     2916 |     snprintf(isa_base, maxlen, "rv%di", xlen);
>          |                                ^~~~~~~
> 
> Since the xlen variable represents the register width (32, 64, 128) in
> the RISC-V base ISA name, mask its value with a 8-bit bitmask to
> satisfy the size constraints on the snprintf output.
> 
> Cc: Palmer Dabbelt <palmer@dabbelt.com>
> Cc: Alistair Francis <alistair.francis@wdc.com>
> Cc: Weiwei Li <liwei1518@gmail.com>
> Cc: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
> Cc: Liu Zhiwei <zhiwei_liu@linux.alibaba.com>
> Signed-off-by: Cédric Le Goater <clg@redhat.com>
> ---
>   target/riscv/cpu.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c
> index ffd98e8eed468645b8ceb8a7adb45718bdc4444d..e95eea024939d2cbb5747781c291fed0e1b07bb9 100644
> --- a/target/riscv/cpu.c
> +++ b/target/riscv/cpu.c
> @@ -2913,7 +2913,7 @@ void riscv_isa_write_fdt(RISCVCPU *cpu, void *fdt, char *nodename)
>       riscv_isa = riscv_isa_string(cpu);
>       qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", riscv_isa);
>   
> -    snprintf(isa_base, maxlen, "rv%di", xlen);
> +    snprintf(isa_base, maxlen, "rv%di", xlen & 0xFF);
>       qemu_fdt_setprop_string(fdt, nodename, "riscv,isa-base", isa_base);
>   
>       isa_extensions = riscv_isa_extensions_list(cpu, &count);

Applied to vfio-next.

Thanks,

C.


Re: [PATCH] target/riscv: Fix build errors
Posted by Daniel Henrique Barboza 3 weeks, 5 days ago

On 1/12/2026 1:16 PM, Cédric Le Goater wrote:
> Newer gcc compiler (version 16.0.0 20260103 (Red Hat 16.0.0-0) (GCC))
> detects a truncation error:
> 
>    ../target/riscv/cpu.c: In function ‘riscv_isa_write_fdt’:
>    ../target/riscv/cpu.c:2916:35: error: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 5 [-Werror=format-truncation=]
>     2916 |     snprintf(isa_base, maxlen, "rv%di", xlen);
>          |                                   ^~
>    ../target/riscv/cpu.c:2916:32: note: directive argument in the range [-2147483648, 2147483632]
>     2916 |     snprintf(isa_base, maxlen, "rv%di", xlen);
>          |                                ^~~~~~~
> 
> Since the xlen variable represents the register width (32, 64, 128) in
> the RISC-V base ISA name, mask its value with a 8-bit bitmask to
> satisfy the size constraints on the snprintf output.
> 
> Cc: Palmer Dabbelt <palmer@dabbelt.com>
> Cc: Alistair Francis <alistair.francis@wdc.com>
> Cc: Weiwei Li <liwei1518@gmail.com>
> Cc: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
> Cc: Liu Zhiwei <zhiwei_liu@linux.alibaba.com>
> Signed-off-by: Cédric Le Goater <clg@redhat.com>
> ---

Reviewed-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>


>   target/riscv/cpu.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c
> index ffd98e8eed468645b8ceb8a7adb45718bdc4444d..e95eea024939d2cbb5747781c291fed0e1b07bb9 100644
> --- a/target/riscv/cpu.c
> +++ b/target/riscv/cpu.c
> @@ -2913,7 +2913,7 @@ void riscv_isa_write_fdt(RISCVCPU *cpu, void *fdt, char *nodename)
>       riscv_isa = riscv_isa_string(cpu);
>       qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", riscv_isa);
>   
> -    snprintf(isa_base, maxlen, "rv%di", xlen);
> +    snprintf(isa_base, maxlen, "rv%di", xlen & 0xFF);
>       qemu_fdt_setprop_string(fdt, nodename, "riscv,isa-base", isa_base);
>   
>       isa_extensions = riscv_isa_extensions_list(cpu, &count);