Skip to contentSkip to conversation

hw/ide/ahci: silence a compiler warning

PATCH In review Alex Bennée 2 participants 2 messages Oct 6, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/ide/ahci: silence a compiler warning

When built with -Og -ggdb with gcc 8.3 the compiler gets confused reporting:

../../hw/ide/ahci.c: In function ‘ahci_populate_sglist’:
../../hw/ide/ahci.c:965:58: error: ‘tbl_entry_size’ may be used uninitialized in this function [-Werror=maybe-uninitialized]

As prdtl being > 0 is a prerequisite for running the code which guarantees we execute the loop at least once although not necessarily resetting off_pos/off_idx which should short-circuit the test anyway.

To save grey hair initialise the value to zero and move the variable to the local block so future changes can't accidentally use it.

---
 hw/ide/ahci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ide/ahci.c +1 -1
index 680304a24c..f5c20c7d55 100644
@@ -925,7 +925,6 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist,
     uint64_t sum = 0;
     int off_idx = -1;
     int64_t off_pos = -1;
-    int tbl_entry_size;
     IDEBus *bus = &ad->port;
     BusState *qbus = BUS(bus);
 
@@ -952,6 +951,7 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist,
     /* Get entries in the PRDT, init a qemu sglist accordingly */
     if (prdtl > 0) {
         AHCI_SG *tbl = (AHCI_SG *)prdt;
+        int tbl_entry_size = 0;
         sum = 0;
         for (i = 0; i < prdtl; i++) {
             tbl_entry_size = prdt_tbl_entry_size(&tbl[i]);
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Alex Bennée

When built with -Og -ggdb with gcc 8.3 the compiler gets confused reporting:

../../hw/ide/ahci.c: In function ‘ahci_populate_sglist’:
../../hw/ide/ahci.c:965:58: error: ‘tbl_entry_size’ may be used uninitialized in this function [-Werror=maybe-uninitialized]

Quoted 6 lines

As prdtl being > 0 is a prerequisite for running the code which guarantees we execute the loop at least once although not necessarily resetting off_pos/off_idx which should short-circuit the test anyway.

To save grey hair initialise the value to zero and move the variable to the local block so future changes can't accidentally use it.

Paolo fixed it yesterday but forgot to Cc you:
https://www.mail-archive.com/[email protected]/msg75131.html

As your patch is also valid (and simpler):

Quoted 27 lines
---
 hw/ide/ahci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ide/ahci.c +1 -1
index 680304a24c..f5c20c7d55 100644
@@ -925,7 +925,6 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist,
     uint64_t sum = 0;
     int off_idx = -1;
     int64_t off_pos = -1;
-    int tbl_entry_size;
     IDEBus *bus = &ad->port;
     BusState *qbus = BUS(bus);
 
@@ -952,6 +951,7 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist,
     /* Get entries in the PRDT, init a qemu sglist accordingly */
     if (prdtl > 0) {
         AHCI_SG *tbl = (AHCI_SG *)prdt;
+        int tbl_entry_size = 0;
         sum = 0;
         for (i = 0; i < prdtl; i++) {
             tbl_entry_size = prdt_tbl_entry_size(&tbl[i]);