diff mbox series

hw/ide/ahci: silence a compiler warning

Message ID 20201006154256.17392-1-alex.bennee@linaro.org
State New
Headers show
Series hw/ide/ahci: silence a compiler warning | expand

Commit Message

Alex Bennée Oct. 6, 2020, 3:42 p.m. UTC
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.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
 hw/ide/ahci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Philippe Mathieu-Daudé Oct. 6, 2020, 4:11 p.m. UTC | #1
On 10/6/20 5:42 PM, Alex Bennée wrote:
> 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.

Paolo fixed it yesterday but forgot to Cc you:
https://www.mail-archive.com/qemu-block@nongnu.org/msg75131.html

As your patch is also valid (and simpler):
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

> 
> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
> ---
>  hw/ide/ahci.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
> index 680304a24c..f5c20c7d55 100644
> --- a/hw/ide/ahci.c
> +++ b/hw/ide/ahci.c
> @@ -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]);
>
diff mbox series

Patch

diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
index 680304a24c..f5c20c7d55 100644
--- a/hw/ide/ahci.c
+++ b/hw/ide/ahci.c
@@ -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]);