Skip to contentSkip to conversation

hw/intc/i8259: Refactor pic_read_irq() to avoid uninitialized variable

PATCH Superseded Philippe Mathieu-Daudé 3 participants 4 messages Mar 18, 2021

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 hw/intc/i8259: Refactor pic_read_irq() to avoid uninitialized variable

Some compiler versions are smart enough to detect a potentially uninitialized variable, but are not smart enough to detect that this cannot happen due to the code flow:

../hw/intc/i8259.c: In function ‘pic_read_irq’:
../hw/intc/i8259.c:203:13: error: ‘irq2’ may be used uninitialized in this function [-Werror=maybe-uninitialized]

   203 |         irq = irq2 + 8;
       |         ~~~~^~~~~~~~~~

Restrict irq2 variable use to the inner statement.

Fixes 78ef2b6989f ("i8259: Reorder intack in pic_read_irq")

---
Supersedes: <[email protected]>

---
 hw/intc/i8259.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
hw/intc/i8259.c +6 -5
index 344fd04db14..52c039c6c03 100644
@@ -176,10 +176,12 @@ static void pic_intack(PICCommonState *s, int irq)
 int pic_read_irq(DeviceState *d)
 {
     PICCommonState *s = PIC_COMMON(d);
-    int irq, irq2, intno;
+    int irq, intno;
 
     irq = pic_get_irq(s);
     if (irq >= 0) {
+        int irq2;
+
         if (irq == 2) {
             irq2 = pic_get_irq(slave_pic);
             if (irq2 >= 0) {
@@ -189,8 +191,11 @@ int pic_read_irq(DeviceState *d)
                 irq2 = 7;
             }
             intno = slave_pic->irq_base + irq2;
+            pic_intack(s, irq);
+            irq = irq2 + 8;
         } else {
             intno = s->irq_base + irq;
+            pic_intack(s, irq);
         }
         pic_intack(s, irq);
     } else {
@@ -199,10 +204,6 @@ int pic_read_irq(DeviceState *d)
         intno = s->irq_base + irq;
     }
 
-    if (irq == 2) {
-        irq = irq2 + 8;
-    }
-
 #ifdef DEBUG_IRQ_LATENCY
     printf("IRQ%d latency=%0.3fus\n",
            irq,
Signature · 1 line
2.26.2
#1 lore raw
Quoted 39 lines · Philippe Mathieu-Daudé

Some compiler versions are smart enough to detect a potentially uninitialized variable, but are not smart enough to detect that this cannot happen due to the code flow:

../hw/intc/i8259.c: In function ‘pic_read_irq’:
../hw/intc/i8259.c:203:13: error: ‘irq2’ may be used uninitialized in this function [-Werror=maybe-uninitialized]

  203 |         irq = irq2 + 8;
      |         ~~~~^~~~~~~~~~

Restrict irq2 variable use to the inner statement.

Fixes 78ef2b6989f ("i8259: Reorder intack in pic_read_irq")

---
Supersedes: <[email protected]>
---
hw/intc/i8259.c | 11 ++++++-----

1 file changed, 6 insertions(+), 5 deletions(-)
hw/intc/i8259.c +3 -1
index 344fd04db14..52c039c6c03 100644
@@ -176,10 +176,12 @@ static void pic_intack(PICCommonState *s, int irq)
 int pic_read_irq(DeviceState *d)
 {
    PICCommonState *s = PIC_COMMON(d);
-    int irq, irq2, intno;
+    int irq, intno;
 
    irq = pic_get_irq(s);
    if (irq >= 0) {
+        int irq2;
+
        if (irq == 2) {
            irq2 = pic_get_irq(slave_pic);
            if (irq2 >= 0) {

hw/intc/i8259.c int pic_read_irq(DeviceState *d) · line 200

Quoted 10 lines
hw/intc/i8259.c +3 -0
@@ -189,8 +191,11 @@ int pic_read_irq(DeviceState *d)
                irq2 = 7;
            }
            intno = slave_pic->irq_base + irq2;
+            pic_intack(s, irq);
+            irq = irq2 + 8;
        } else {
            intno = s->irq_base + irq;
+            pic_intack(s, irq);
        }
        pic_intack(s, irq);

Do you still need this pic_intack() here or did you intend to move it in the if above?

Regards,
BALATON Zoltan

Quoted 13 lines
hw/intc/i8259.c +0 -4
    } else {
@@ -199,10 +204,6 @@ int pic_read_irq(DeviceState *d)
        intno = s->irq_base + irq;
    }
 
-    if (irq == 2) {
-        irq = irq2 + 8;
-    }
-
 #ifdef DEBUG_IRQ_LATENCY
    printf("IRQ%d latency=%0.3fus\n",
           irq,
#2 lore raw
Quoted 46 lines · BALATON Zoltan
Quoted 45 lines · Philippe Mathieu-Daudé

Some compiler versions are smart enough to detect a potentially uninitialized variable, but are not smart enough to detect that this cannot happen due to the code flow:

../hw/intc/i8259.c: In function ‘pic_read_irq’:
../hw/intc/i8259.c:203:13: error: ‘irq2’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
  203 |         irq = irq2 + 8;
      |         ~~~~^~~~~~~~~~

Restrict irq2 variable use to the inner statement.

Fixes 78ef2b6989f ("i8259: Reorder intack in pic_read_irq")

---
Supersedes: <[email protected]>
---
hw/intc/i8259.c | 11 ++++++-----

1 file changed, 6 insertions(+), 5 deletions(-)
hw/intc/i8259.c +5 -1
index 344fd04db14..52c039c6c03 100644
@@ -176,10 +176,12 @@ static void pic_intack(PICCommonState *s, int irq)
 int pic_read_irq(DeviceState *d)
 {
     PICCommonState *s = PIC_COMMON(d);
-    int irq, irq2, intno;
+    int irq, intno;
 
     irq = pic_get_irq(s);
     if (irq >= 0) {
+        int irq2;
+
         if (irq == 2) {
             irq2 = pic_get_irq(slave_pic);
             if (irq2 >= 0) {
@@ -189,8 +191,11 @@ int pic_read_irq(DeviceState *d)
                 irq2 = 7;
             }
             intno = slave_pic->irq_base + irq2;
+            pic_intack(s, irq);
+            irq = irq2 + 8;
Quoted 8 lines

hw/intc/i8259.c int pic_read_irq(DeviceState *d) · line 200

Quoted 5 lines
hw/intc/i8259.c +1 -0
         } else {
             intno = s->irq_base + irq;
+            pic_intack(s, irq);
         }
         pic_intack(s, irq);

Do you still need this pic_intack() here or did you intend to move it in the if above?

I forgot to remove it indeed, thanks!

Quoted 13 lines
Quoted 13 lines
hw/intc/i8259.c +0 -4
     } else {
@@ -199,10 +204,6 @@ int pic_read_irq(DeviceState *d)
         intno = s->irq_base + irq;
     }
 
-    if (irq == 2) {
-        irq = irq2 + 8;
-    }
-
 #ifdef DEBUG_IRQ_LATENCY
     printf("IRQ%d latency=%0.3fus\n",
            irq,
#3 lore raw
In reply to Philippe Mathieu-Daudé

hw/intc/i8259.c static void pic_intack(PICCommonState *s, int irq) · line 186

Quoted 4 lines · Philippe Mathieu-Daudé
hw/intc/i8259.c +2 -0
+        int irq2;
+
          if (irq == 2) {
              irq2 = pic_get_irq(slave_pic);

Move the declaration in here:

    int irq2 = ...

r~