Skip to contentSkip to conversation

misc: Trivial static code analyzer fixes

PATCH Applied Philippe Mathieu-Daudé 3 participants 14 messages Apr 22, 2020 – May 4, 2020

Subsystems (2)
lore
Share & tools
lore
#3 lore raw

PATCH v3 2/9 blockdev: Remove dead assignment

In reply to Philippe Mathieu-Daudé

Fix warning reported by Clang static code analyzer:

CC blockdev.o
blockdev.c:2744:5: warning: Value stored to 'ret' is never read

      ret = blk_truncate(blk, size, false, PREALLOC_MODE_OFF, errp);
      ^     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 blockdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
blockdev.c +1 -1
index fa8630cb41..6effd5afaa 100644
@@ -2741,7 +2741,7 @@ void qmp_block_resize(bool has_device, const char *device,
     }
 
     bdrv_drained_begin(bs);
-    ret = blk_truncate(blk, size, false, PREALLOC_MODE_OFF, errp);
+    blk_truncate(blk, size, false, PREALLOC_MODE_OFF, errp);
     bdrv_drained_end(bs);
 
 out:
Signature · 1 line
2.21.1
#0 lore raw

v3 cover misc: Trivial static code analyzer fixes

Fix trivial warnings reported by the Clang static code analyzer.

Only patch #2 'blockdev: Remove dead assignment' misses review.

The official Clang static code analyzer documentation is on:
https://clang-analyzer.llvm.org/

On Fedora I simply used it as:

  $ sudo dnf install clang-analyzer
  $ ../configure
  $ scan-build make

Since v2:
- Based on lvivier/trivial-patches-for-5.1
- Removed dup patches from Kuhn Chenqun Since v1:
- Addressed Markus/Zoltan/Aleksandar review comments

Philippe Mathieu-Daudé (9):
block: Avoid dead assignment
blockdev: Remove dead assignment
hw/i2c/pm_smbus: Remove dead assignment
hw/input/adb-kbd: Remove dead assignment
hw/ide/sii3112: Remove dead assignment
hw/isa/i82378: Remove dead assignment
hw/gpio/aspeed_gpio: Remove dead assignment hw/timer/stm32f2xx_timer: Remove dead assignment hw/timer/pxa2xx_timer: Add assertion to silent static analyzer warning

 block.c                    | 2 +-
 blockdev.c                 | 2 +-
 hw/gpio/aspeed_gpio.c      | 2 +-
 hw/i2c/pm_smbus.c          | 1 -
 hw/ide/sii3112.c           | 5 +++--
 hw/input/adb-kbd.c         | 6 +-----
 hw/isa/i82378.c            | 8 ++++----
 hw/timer/pxa2xx_timer.c    | 1 +
 hw/timer/stm32f2xx_timer.c | 1 -
 9 files changed, 12 insertions(+), 16 deletions(-)
Signature · 1 line
2.21.1
#1 lore raw

PATCH v3 1/9 block: Avoid dead assignment

Fix warning reported by Clang static code analyzer:

block.c:3167:5: warning: Value stored to 'ret' is never read

      ret = bdrv_fill_options(&options, filename, &flags, &local_err);
      ^     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Fixes 462f5bcf6
Reported-by Clang Static Analyzer
---
 block.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block.c +1 -1
index 2e3905c99e..27e7e0e3ab 100644
@@ -3171,7 +3171,7 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,
     }
 
     ret = bdrv_fill_options(&options, filename, &flags, &local_err);
-    if (local_err) {
+    if (ret < 0) {
         goto fail;
     }
 
Signature · 1 line
2.21.1
#5 lore raw

PATCH v3 3/9 hw/i2c/pm_smbus: Remove dead assignment

In reply to Philippe Mathieu-Daudé

Fix warning reported by Clang static code analyzer:

CC hw/i2c/pm_smbus.o
hw/i2c/pm_smbus.c:187:17: warning: Value stored to 'ret' is never read

                  ret = 0;
                  ^     ~
Reported-by Clang Static Analyzer
---
 hw/i2c/pm_smbus.c | 1 -
 1 file changed, 1 deletion(-)
hw/i2c/pm_smbus.c +0 -1
index 36994ff585..4728540c37 100644
@@ -184,7 +184,6 @@ static void smb_transaction(PMSMBus *s)
                 s->smb_stat |= STS_HOST_BUSY | STS_BYTE_DONE;
                 s->smb_data[0] = s->smb_blkdata;
                 s->smb_index = 0;
-                ret = 0;
             }
             goto out;
         }
Signature · 1 line
2.21.1
#6 lore raw

PATCH v3 4/9 hw/input/adb-kbd: Remove dead assignment

In reply to Philippe Mathieu-Daudé

Since commit 5a1f49718 the 'olen' variable is not really used. Remove it to fix a warning reported by Clang static code analyzer:

CC hw/input/adb-kbd.o
hw/input/adb-kbd.c:200:5: warning: Value stored to 'olen' is never read

      olen = 0;
      ^      ~
Fixes 5a1f49718 (adb: add support for QKeyCode)
Reported-by Clang Static Analyzer
---
 hw/input/adb-kbd.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
hw/input/adb-kbd.c +1 -5
index 0ba8207589..a6d5c9b7c9 100644
@@ -195,9 +195,7 @@ static int adb_kbd_poll(ADBDevice *d, uint8_t *obuf)
 {
     KBDState *s = ADB_KEYBOARD(d);
     int keycode;
-    int olen;
 
-    olen = 0;
     if (s->count == 0) {
         return 0;
     }
@@ -216,7 +214,6 @@ static int adb_kbd_poll(ADBDevice *d, uint8_t *obuf)
     if (keycode == 0x7f) {
         obuf[0] = 0x7f;
         obuf[1] = 0x7f;
-        olen = 2;
     } else {
         obuf[0] = keycode;
         /* NOTE: the power key key-up is the two byte sequence 0xff 0xff;
@@ -224,10 +221,9 @@ static int adb_kbd_poll(ADBDevice *d, uint8_t *obuf)
          * byte, but choose not to bother.
          */
         obuf[1] = 0xff;
-        olen = 2;
     }
 
-    return olen;
+    return 2;
 }
 
 static int adb_kbd_request(ADBDevice *d, uint8_t *obuf,
Signature · 1 line
2.21.1
#7 lore raw

PATCH v3 5/9 hw/ide/sii3112: Remove dead assignment

In reply to Philippe Mathieu-Daudé

Fix warning reported by Clang static code analyzer:

    CC      hw/ide/sii3112.o
  hw/ide/sii3112.c:204:9: warning: Value stored to 'val' is never read
          val = 0;
          ^     ~
Fixes a9dd6604
Reported-by Clang Static Analyzer
---
 hw/ide/sii3112.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/ide/sii3112.c +3 -2
index d69079c3d9..94d2b57f95 100644
@@ -42,7 +42,7 @@ static uint64_t sii3112_reg_read(void *opaque, hwaddr addr,
                                 unsigned int size)
 {
     SiI3112PCIState *d = opaque;
-    uint64_t val = 0;
+    uint64_t val;
 
     switch (addr) {
     case 0x00:
@@ -126,6 +126,7 @@ static uint64_t sii3112_reg_read(void *opaque, hwaddr addr,
         break;
     default:
         val = 0;
+        break;
     }
     trace_sii3112_read(size, addr, val);
     return val;
@@ -201,7 +202,7 @@ static void sii3112_reg_write(void *opaque, hwaddr addr,
         d->regs[1].sien = (val >> 16) & 0x3eed;
         break;
     default:
-        val = 0;
+        break;
     }
 }
 
Signature · 1 line
2.21.1
#8 lore raw

PATCH v3 6/9 hw/isa/i82378: Remove dead assignment

In reply to Philippe Mathieu-Daudé

Rename the unique variable assigned as 'pit' which better represents what it holds, to fix a warning reported by the Clang static code analyzer:

    CC      hw/isa/i82378.o
  hw/isa/i82378.c:108:5: warning: Value stored to 'isa' is never read
      isa = isa_create_simple(isabus, "i82374");
      ^     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 hw/isa/i82378.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/isa/i82378.c +4 -4
index dcb6b479ea..d9e6c7fa00 100644
@@ -67,7 +67,7 @@ static void i82378_realize(PCIDevice *pci, Error **errp)
     I82378State *s = I82378(dev);
     uint8_t *pci_conf;
     ISABus *isabus;
-    ISADevice *isa;
+    ISADevice *pit;
 
     pci_conf = pci->config;
     pci_set_word(pci_conf + PCI_COMMAND,
@@ -99,13 +99,13 @@ static void i82378_realize(PCIDevice *pci, Error **errp)
     isa_bus_irqs(isabus, s->i8259);
 
     /* 1 82C54 (pit) */
-    isa = i8254_pit_init(isabus, 0x40, 0, NULL);
+    pit = i8254_pit_init(isabus, 0x40, 0, NULL);
 
     /* speaker */
-    pcspk_init(isabus, isa);
+    pcspk_init(isabus, pit);
 
     /* 2 82C37 (dma) */
-    isa = isa_create_simple(isabus, "i82374");
+    isa_create_simple(isabus, "i82374");
 }
 
 static void i82378_init(Object *obj)
Signature · 1 line
2.21.1
#9 lore raw

PATCH v3 7/9 hw/gpio/aspeed_gpio: Remove dead assignment

In reply to Philippe Mathieu-Daudé

Fix warning reported by Clang static code analyzer:

hw/gpio/aspeed_gpio.c:717:18: warning: Value stored to 'g_idx' during its initialization is never read

      int set_idx, g_idx = *group_idx;
                   ^~~~~   ~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 hw/gpio/aspeed_gpio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/gpio/aspeed_gpio.c +1 -1
index 41e11ea9b0..bd19db31f4 100644
@@ -714,7 +714,7 @@ static void aspeed_gpio_write(void *opaque, hwaddr offset, uint64_t data,
 static int get_set_idx(AspeedGPIOState *s, const char *group, int *group_idx)
 {
     AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
-    int set_idx, g_idx = *group_idx;
+    int set_idx, g_idx;
 
     for (set_idx = 0; set_idx < agc->nr_gpio_sets; set_idx++) {
         const GPIOSetProperties *set_props = &agc->props[set_idx];
Signature · 1 line
2.21.1
#11 lore raw

PATCH v3 9/9 hw/timer/pxa2xx_timer: Add assertion to silent static analyzer warning

In reply to Philippe Mathieu-Daudé

pxa2xx_timer_tick4() takes an opaque pointer, then calls pxa2xx_timer_update4(), so the static analyzer can not verify that the 'n < 8':

  425 static void pxa2xx_timer_tick4(void *opaque)
  426 {
  427     PXA2xxTimer4 *t = (PXA2xxTimer4 *) opaque;
  428     PXA2xxTimerInfo *i = (PXA2xxTimerInfo *) t->tm.info;
  429
  430     pxa2xx_timer_tick(&t->tm);
  433     if (t->control & (1 << 6))
  434         pxa2xx_timer_update4(i, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), t->tm.num - 4);

  135 static void pxa2xx_timer_update4(void *opaque, uint64_t now_qemu, int n)
  136 {
  137     PXA2xxTimerInfo *s = (PXA2xxTimerInfo *) opaque;
  140     static const int counters[8] = { 0, 0, 0, 0, 4, 4, 6, 6 };
  142
  143     if (s->tm4[n].control & (1 << 7))
  144         counter = n;
  145     else
  146         counter = counters[n];

Add an assert() to give the static analyzer a hint, this fixes a warning reported by Clang static code analyzer:

CC hw/timer/pxa2xx_timer.o
hw/timer/pxa2xx_timer.c:146:17: warning: Assigned value is garbage or undefined

          counter = counters[n];
                  ^ ~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 hw/timer/pxa2xx_timer.c | 1 +
 1 file changed, 1 insertion(+)
hw/timer/pxa2xx_timer.c +1 -0
index cd172cc1e9..944c165889 100644
@@ -140,6 +140,7 @@ static void pxa2xx_timer_update4(void *opaque, uint64_t now_qemu, int n)
     static const int counters[8] = { 0, 0, 0, 0, 4, 4, 6, 6 };
     int counter;
 
+    assert(n < ARRAY_SIZE(counters));
     if (s->tm4[n].control & (1 << 7))
         counter = n;
     else
Signature · 1 line
2.21.1
#10 lore raw

PATCH v3 8/9 hw/timer/stm32f2xx_timer: Remove dead assignment

In reply to Philippe Mathieu-Daudé

Fix warning reported by Clang static code analyzer:

CC hw/timer/stm32f2xx_timer.o
hw/timer/stm32f2xx_timer.c:225:9: warning: Value stored to 'value' is never read

          value = timer_val;
          ^       ~~~~~~~~~
Reported-by Clang Static Analyzer
---
 hw/timer/stm32f2xx_timer.c | 1 -
 1 file changed, 1 deletion(-)
hw/timer/stm32f2xx_timer.c +0 -1
index 06ec8a02c2..ba8694dcd3 100644
@@ -222,7 +222,6 @@ static void stm32f2xx_timer_write(void *opaque, hwaddr offset,
     case TIM_PSC:
         timer_val = stm32f2xx_ns_to_ticks(s, now) - s->tick_offset;
         s->tim_psc = value & 0xFFFF;
-        value = timer_val;
         break;
     case TIM_CNT:
         timer_val = value;
Signature · 1 line
2.21.1
Reviewed-by on v3 2/9
#4 lore raw

Re: [PATCH v3 2/9] blockdev: Remove dead assignment

In reply to Philippe Mathieu-Daudé
Quoted 4 lines · Philippe Mathieu-Daudé

Fix warning reported by Clang static code analyzer:

CC blockdev.o
blockdev.c:2744:5: warning: Value stored to 'ret' is never read

Quoted 8 lines
      ret = blk_truncate(blk, size, false, PREALLOC_MODE_OFF, errp);
      ^     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 blockdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Reviewed-by on v3 1/9
#2 lore raw

Re: [PATCH v3 1/9] block: Avoid dead assignment

In reply to Philippe Mathieu-Daudé
Quoted 6 lines · Philippe Mathieu-Daudé

Fix warning reported by Clang static code analyzer:

block.c:3167:5: warning: Value stored to 'ret' is never read

      ret = bdrv_fill_options(&options, filename, &flags, &local_err);
      ^     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 8 lines
Fixes 462f5bcf6
Reported-by Clang Static Analyzer
---
 block.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#12 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 3 lines · Philippe Mathieu-Daudé

Fix trivial warnings reported by the Clang static code analyzer.

Only patch #2 'blockdev: Remove dead assignment' misses review.

Thanks to Max this series is now fully reviewed, so... ping?

Quoted 38 lines

The official Clang static code analyzer documentation is on:
https://clang-analyzer.llvm.org/

On Fedora I simply used it as:

   $ sudo dnf install clang-analyzer
   $ ../configure
   $ scan-build make

Since v2:
- Based on lvivier/trivial-patches-for-5.1
- Removed dup patches from Kuhn Chenqun Since v1:
- Addressed Markus/Zoltan/Aleksandar review comments

Philippe Mathieu-Daudé (9):
block: Avoid dead assignment
blockdev: Remove dead assignment
hw/i2c/pm_smbus: Remove dead assignment
hw/input/adb-kbd: Remove dead assignment
hw/ide/sii3112: Remove dead assignment
hw/isa/i82378: Remove dead assignment
hw/gpio/aspeed_gpio: Remove dead assignment hw/timer/stm32f2xx_timer: Remove dead assignment hw/timer/pxa2xx_timer: Add assertion to silent static analyzer warning

  block.c                    | 2 +-
  blockdev.c                 | 2 +-
  hw/gpio/aspeed_gpio.c      | 2 +-
  hw/i2c/pm_smbus.c          | 1 -
  hw/ide/sii3112.c           | 5 +++--
  hw/input/adb-kbd.c         | 6 +-----
  hw/isa/i82378.c            | 8 ++++----
  hw/timer/pxa2xx_timer.c    | 1 +
  hw/timer/stm32f2xx_timer.c | 1 -
  9 files changed, 12 insertions(+), 16 deletions(-)
#13 lore raw
In reply to Philippe Mathieu-Daudé

Le 22/04/2020 à 15:31, Philippe Mathieu-Daudé a écrit :

Quoted 30 lines

Fix trivial warnings reported by the Clang static code analyzer.

Only patch #2 'blockdev: Remove dead assignment' misses review.

The official Clang static code analyzer documentation is on:
https://clang-analyzer.llvm.org/

On Fedora I simply used it as:

  $ sudo dnf install clang-analyzer
  $ ../configure
  $ scan-build make

Since v2:
- Based on lvivier/trivial-patches-for-5.1
- Removed dup patches from Kuhn Chenqun Since v1:
- Addressed Markus/Zoltan/Aleksandar review comments

Philippe Mathieu-Daudé (9):
block: Avoid dead assignment
blockdev: Remove dead assignment
hw/i2c/pm_smbus: Remove dead assignment
hw/input/adb-kbd: Remove dead assignment
hw/ide/sii3112: Remove dead assignment
hw/isa/i82378: Remove dead assignment
hw/gpio/aspeed_gpio: Remove dead assignment hw/timer/stm32f2xx_timer: Remove dead assignment hw/timer/pxa2xx_timer: Add assertion to silent static analyzer warning

Quoted 11 lines
 block.c                    | 2 +-
 blockdev.c                 | 2 +-
 hw/gpio/aspeed_gpio.c      | 2 +-
 hw/i2c/pm_smbus.c          | 1 -
 hw/ide/sii3112.c           | 5 +++--
 hw/input/adb-kbd.c         | 6 +-----
 hw/isa/i82378.c            | 8 ++++----
 hw/timer/pxa2xx_timer.c    | 1 +
 hw/timer/stm32f2xx_timer.c | 1 -
 9 files changed, 12 insertions(+), 16 deletions(-)

Applied to my trivial-patches branch.

Thanks,
Laurent