Skip to contentSkip to conversation

hw/openrisc/openrisc_sim: Add assertion to silent GCC warning

PATCH Superseded Philippe Mathieu-Daudé 4 participants 6 messages Jun 8, 2020 – Jun 9, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/openrisc/openrisc_sim: Add assertion to silent GCC warning

When compiling with GCC 10 (Fedora 32) using CFLAGS=-O2 we get:

CC or1k-softmmu/hw/openrisc/openrisc_sim.o
hw/openrisc/openrisc_sim.c: In function ‘openrisc_sim_init’:
hw/openrisc/openrisc_sim.c:87:42: error: ‘cpu_irqs[0]’ may be used uninitialized in this function [-Werror=maybe-uninitialized]

     87 |         sysbus_connect_irq(s, i, cpu_irqs[i][irq_pin]);
        |                                  ~~~~~~~~^~~

While humans can tell smp_cpus will always be in the [1, 2] range, (openrisc_sim_machine_init sets mc->max_cpus = 2), the compiler can't.

Add an assertion to give the compiler a hint there's no use of uninitialized data.

---
 hw/openrisc/openrisc_sim.c | 1 +
 1 file changed, 1 insertion(+)
hw/openrisc/openrisc_sim.c +1 -0
index d08ce61811..02f5259e5e 100644
@@ -134,6 +134,7 @@ static void openrisc_sim_init(MachineState *machine)
     int n;
     unsigned int smp_cpus = machine->smp.cpus;
 
+    assert(smp_cpus >= 1 && smp_cpus <= 2);
     for (n = 0; n < smp_cpus; n++) {
         cpu = OPENRISC_CPU(cpu_create(machine->cpu_type));
         if (cpu == NULL) {
Signature · 1 line
2.21.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 29 lines · Philippe Mathieu-Daudé

When compiling with GCC 10 (Fedora 32) using CFLAGS=-O2 we get:

CC or1k-softmmu/hw/openrisc/openrisc_sim.o
hw/openrisc/openrisc_sim.c: In function ‘openrisc_sim_init’:
hw/openrisc/openrisc_sim.c:87:42: error: ‘cpu_irqs[0]’ may be used uninitialized in this function [-Werror=maybe-uninitialized]

     87 |         sysbus_connect_irq(s, i, cpu_irqs[i][irq_pin]);
        |                                  ~~~~~~~~^~~

While humans can tell smp_cpus will always be in the [1, 2] range, (openrisc_sim_machine_init sets mc->max_cpus = 2), the compiler can't.

Add an assertion to give the compiler a hint there's no use of uninitialized data.

---
 hw/openrisc/openrisc_sim.c | 1 +
 1 file changed, 1 insertion(+)
hw/openrisc/openrisc_sim.c +0 -0
index d08ce61811..02f5259e5e 100644

hw/openrisc/openrisc_sim.c static void openrisc_sim_init(MachineState *machine) · line 140

Quoted 7 lines
hw/openrisc/openrisc_sim.c +1 -0
@@ -134,6 +134,7 @@ static void openrisc_sim_init(MachineState *machine)
     int n;
     unsigned int smp_cpus = machine->smp.cpus;
 
+    assert(smp_cpus >= 1 && smp_cpus <= 2);
     for (n = 0; n < smp_cpus; n++) {
         cpu = OPENRISC_CPU(cpu_create(machine->cpu_type));
         if (cpu == NULL) {
Tested-by Reviewed-by on v1 1/1
#2 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 1 line · Philippe Mathieu-Daudé

When compiling with GCC 10 (Fedora 32) using CFLAGS=-O2 we get:

In the subject: s/silent/silence/

Quoted 14 lines

CC or1k-softmmu/hw/openrisc/openrisc_sim.o
hw/openrisc/openrisc_sim.c: In function ‘openrisc_sim_init’:

   hw/openrisc/openrisc_sim.c:87:42: error: ‘cpu_irqs[0]’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
      87 |         sysbus_connect_irq(s, i, cpu_irqs[i][irq_pin]);
         |                                  ~~~~~~~~^~~

While humans can tell smp_cpus will always be in the [1, 2] range, (openrisc_sim_machine_init sets mc->max_cpus = 2), the compiler can't.

Add an assertion to give the compiler a hint there's no use of uninitialized data.

Quoted 7 lines
---
  hw/openrisc/openrisc_sim.c | 1 +
  1 file changed, 1 insertion(+)

With the typo fixed,

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#3 lore raw
Quoted 27 lines · Eric Blake
Quoted 1 line · Philippe Mathieu-Daudé

When compiling with GCC 10 (Fedora 32) using CFLAGS=-O2 we get:

In the subject: s/silent/silence/

Quoted 14 lines

CC or1k-softmmu/hw/openrisc/openrisc_sim.o
hw/openrisc/openrisc_sim.c: In function ‘openrisc_sim_init’:

   hw/openrisc/openrisc_sim.c:87:42: error: ‘cpu_irqs[0]’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
      87 |         sysbus_connect_irq(s, i, cpu_irqs[i][irq_pin]);
         |                                  ~~~~~~~~^~~

While humans can tell smp_cpus will always be in the [1, 2] range, (openrisc_sim_machine_init sets mc->max_cpus = 2), the compiler can't.

Add an assertion to give the compiler a hint there's no use of uninitialized data.

Quoted 7 lines
---
  hw/openrisc/openrisc_sim.c | 1 +
  1 file changed, 1 insertion(+)
Quoted 4 lines

With the typo fixed,

I see there are now two patches for this, I kind of like this assert fix.

Shall I queue it for OpenRISC pulling? Or can someone else pick this up?

-Stafford

#4 lore raw
Quoted 30 lines · Stafford Horne
Quoted 29 lines · Eric Blake
Quoted 1 line · Philippe Mathieu-Daudé

When compiling with GCC 10 (Fedora 32) using CFLAGS=-O2 we get:

In the subject: s/silent/silence/

Quoted 14 lines

CC or1k-softmmu/hw/openrisc/openrisc_sim.o
hw/openrisc/openrisc_sim.c: In function ‘openrisc_sim_init’:

   hw/openrisc/openrisc_sim.c:87:42: error: ‘cpu_irqs[0]’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
      87 |         sysbus_connect_irq(s, i, cpu_irqs[i][irq_pin]);
         |                                  ~~~~~~~~^~~

While humans can tell smp_cpus will always be in the [1, 2] range, (openrisc_sim_machine_init sets mc->max_cpus = 2), the compiler can't.

Add an assertion to give the compiler a hint there's no use of uninitialized data.

Quoted 7 lines
---
  hw/openrisc/openrisc_sim.c | 1 +
  1 file changed, 1 insertion(+)
Quoted 8 lines
Quoted 2 lines

With the typo fixed,

I see there are now two patches for this, I kind of like this assert fix.

Shall I queue it for OpenRISC pulling? Or can someone else pick this up?

Hello,
Sorry, I see v2 of this patch has already been picked up.

-Stafford

#5 lore raw
In reply to Stafford Horne
Quoted 8 lines · Stafford Horne
Quoted 7 lines
Quoted 7 lines

While humans can tell smp_cpus will always be in the [1, 2] range, (openrisc_sim_machine_init sets mc->max_cpus = 2), the compiler can't.

Add an assertion to give the compiler a hint there's no use of uninitialized data.

Quoted 5 lines

I see there are now two patches for this, I kind of like this assert fix.

Shall I queue it for OpenRISC pulling? Or can someone else pick this up?

Looks like Laurent already volunteered to queue it through the trivial patches tree.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org