Skip to contentSkip to conversation

block: Miscellaneous minor Coverity fixes

PATCH Superseded Peter Maydell 5 participants 26 messages Jul 31, 2024 – Aug 6, 2024 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover block: Miscellaneous minor Coverity fixes

This patchset is a collection of fixes for minor Coverity reported issues. In all cases, there isn't a user-visible problem, but the Coverity issue pointed up somewhere where we could clean up the code or make it a bit more obvious to a human reader what the intent was.

Only lightly tested (with "make check" and "make check-avocado").

thanks
-- PMM

Peter Maydell (7):
block/vdi.c: Avoid potential overflow when calculating size of write block/gluster: Use g_autofree for string in qemu_gluster_parse_json() hw/block/pflash_cfi01: Don't decrement pfl->counter below 0 hw/ide/atapi: Be explicit that assigning to s->lcyl truncates hw/block/fdc-isa: Assert that isa_fdc_get_drive_max_chs() found
something
hw/ide/pci.c: Remove dead code from bmdma_prepare_buf() block/ssh.c: Don't double-check that characters are hex digits

 block/gluster.c         |  6 +-----
 block/ssh.c             | 10 ++++++----
 block/vdi.c             |  2 +-
 hw/block/fdc-isa.c      |  2 ++
 hw/block/pflash_cfi01.c |  1 +
 hw/ide/atapi.c          |  2 +-
 hw/ide/pci.c            |  4 ----
 7 files changed, 12 insertions(+), 15 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/7 block/vdi.c: Avoid potential overflow when calculating size of write

In vdi_co_pwritev() we multiply a sector count by SECTOR_SIZE to get the size to write in bytes. Coverity notes that this means that we do the multiply as a 32x32->32 multiply before converting to 64 bits, which has the potential to overflow.

This is very unlikely to happen, since the block map has 4 bytes per block and the maximum number of blocks in the image must fit into a 32-bit integer. But we can keep Coverity happy by including a cast so we do a 64-bit multiply here.

Resolves: Coverity CID 1508076

---
 block/vdi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/vdi.c +1 -1
index 6363da08cee..27c60ba18d0 100644
@@ -728,7 +728,7 @@ nonallocating_write:
         logout("will write %u block map sectors starting from entry %u\n",
                n_sectors, bmap_first);
         ret = bdrv_co_pwrite(bs->file, bmap_offset * SECTOR_SIZE,
-                             n_sectors * SECTOR_SIZE, base, 0);
+                             n_sectors * (uint64_t)SECTOR_SIZE, base, 0);
     }
 
     return ret;
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/7] block/vdi.c: Avoid potential overflow when calculating size of write

Am 31.07.2024 um 16:36 hat Peter Maydell geschrieben:

Quoted 21 lines

In vdi_co_pwritev() we multiply a sector count by SECTOR_SIZE to get the size to write in bytes. Coverity notes that this means that we do the multiply as a 32x32->32 multiply before converting to 64 bits, which has the potential to overflow.

This is very unlikely to happen, since the block map has 4 bytes per block and the maximum number of blocks in the image must fit into a 32-bit integer. But we can keep Coverity happy by including a cast so we do a 64-bit multiply here.

Resolves: Coverity CID 1508076

---
 block/vdi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/vdi.c +0 -0
index 6363da08cee..27c60ba18d0 100644

block/vdi.c nonallocating_write: · line 732

Quoted 6 lines
block/vdi.c +1 -1
@@ -728,7 +728,7 @@ nonallocating_write:
         logout("will write %u block map sectors starting from entry %u\n",
                n_sectors, bmap_first);
         ret = bdrv_co_pwrite(bs->file, bmap_offset * SECTOR_SIZE,
-                             n_sectors * SECTOR_SIZE, base, 0);
+                             n_sectors * (uint64_t)SECTOR_SIZE, base, 0);
     }

I wonder if we shouldn't just make VDI's SECTOR_SIZE 64 bits like BDRV_SECTOR_SIZE. It's easy to miss the cast in individual places.

Kevin

Reviewed-by Stefan Weil on v1 1/7
#3 lore raw

Re: [PATCH 1/7] block/vdi.c: Avoid potential overflow when calculating size of write

In reply to Peter Maydell

Am 31.07.24 um 16:36 schrieb Peter Maydell:

Quoted 21 lines

In vdi_co_pwritev() we multiply a sector count by SECTOR_SIZE to get the size to write in bytes. Coverity notes that this means that we do the multiply as a 32x32->32 multiply before converting to 64 bits, which has the potential to overflow.

This is very unlikely to happen, since the block map has 4 bytes per block and the maximum number of blocks in the image must fit into a 32-bit integer. But we can keep Coverity happy by including a cast so we do a 64-bit multiply here.

Resolves: Coverity CID 1508076

---
  block/vdi.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
block/vdi.c +0 -0
index 6363da08cee..27c60ba18d0 100644

block/vdi.c nonallocating_write: · line 734

Quoted 8 lines
block/vdi.c +1 -1
@@ -728,7 +728,7 @@ nonallocating_write:
          logout("will write %u block map sectors starting from entry %u\n",
                 n_sectors, bmap_first);
          ret = bdrv_co_pwrite(bs->file, bmap_offset * SECTOR_SIZE,
-                             n_sectors * SECTOR_SIZE, base, 0);
+                             n_sectors * (uint64_t)SECTOR_SIZE, base, 0);
      }
 
      return ret;

Thanks.

#4 lore raw

PATCH 2/7 block/gluster: Use g_autofree for string in qemu_gluster_parse_json()

In reply to Peter Maydell

In the loop in qemu_gluster_parse_json() we do:

    char *str = NULL;
    for(...) {
        str = g_strdup_printf(...);
        ...
        if (various errors) {
            goto out;
        }
        ...
        g_free(str);
        str = NULL;
    }
    return 0;
out:
    various cleanups;
    g_free(str);
    ...
    return -errno;

Coverity correctly complains that the assignment "str = NULL" at the end of the loop is unnecessary, because we will either go back to the top of the loop and overwrite it, or else we will exit the loop and then exit the function without ever reading str again. The assignment is there as defensive coding to ensure that str is only non-NULL if it's a live allocation, so this is intentional.

We can make Coverity happier and simplify the code here by using g_autofree, since we never need 'str' outside the loop.

Resolves: Coverity CID 1527385

---
 block/gluster.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
block/gluster.c +1 -5
index f8b415f3812..61ded95e660 100644
@@ -514,7 +514,6 @@ static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,
     SocketAddressList **tail;
     QDict *backing_options = NULL;
     Error *local_err = NULL;
-    char *str = NULL;
     const char *ptr;
     int i, type, num_servers;
 
@@ -547,7 +546,7 @@ static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,
     tail = &gconf->server;
 
     for (i = 0; i < num_servers; i++) {
-        str = g_strdup_printf(GLUSTER_OPT_SERVER_PATTERN"%d.", i);
+        g_autofree char *str = g_strdup_printf(GLUSTER_OPT_SERVER_PATTERN"%d.", i);
         qdict_extract_subqdict(options, &backing_options, str);
 
         /* create opts info from runtime_type_opts list */
@@ -658,8 +657,6 @@ static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,
 
         qobject_unref(backing_options);
         backing_options = NULL;
-        g_free(str);
-        str = NULL;
     }
 
     return 0;
@@ -668,7 +665,6 @@ out:
     error_propagate(errp, local_err);
     qapi_free_SocketAddress(gsconf);
     qemu_opts_del(opts);
-    g_free(str);
     qobject_unref(backing_options);
     errno = EINVAL;
     return -errno;
Signature · 1 line
2.34.1
Reviewed-by on v1 2/7
#5 lore raw

Re: [PATCH 2/7] block/gluster: Use g_autofree for string in qemu_gluster_parse_json()

Am 31.07.2024 um 16:36 hat Peter Maydell geschrieben:

Quoted 49 lines

In the loop in qemu_gluster_parse_json() we do:

    char *str = NULL;
    for(...) {
        str = g_strdup_printf(...);
        ...
        if (various errors) {
            goto out;
        }
        ...
        g_free(str);
        str = NULL;
    }
    return 0;
out:
    various cleanups;
    g_free(str);
    ...
    return -errno;

Coverity correctly complains that the assignment "str = NULL" at the end of the loop is unnecessary, because we will either go back to the top of the loop and overwrite it, or else we will exit the loop and then exit the function without ever reading str again. The assignment is there as defensive coding to ensure that str is only non-NULL if it's a live allocation, so this is intentional.

We can make Coverity happier and simplify the code here by using g_autofree, since we never need 'str' outside the loop.

Resolves: Coverity CID 1527385

---
 block/gluster.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
block/gluster.c +0 -1
index f8b415f3812..61ded95e660 100644
@@ -514,7 +514,6 @@ static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,
     SocketAddressList **tail;
     QDict *backing_options = NULL;
     Error *local_err = NULL;
-    char *str = NULL;
     const char *ptr;
     int i, type, num_servers;
 

block/gluster.c static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf, · line 549

Quoted 5 lines
block/gluster.c +1 -1
@@ -547,7 +546,7 @@ static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,
     tail = &gconf->server;
 
     for (i = 0; i < num_servers; i++) {
-        str = g_strdup_printf(GLUSTER_OPT_SERVER_PATTERN"%d.", i);
+        g_autofree char *str = g_strdup_printf(GLUSTER_OPT_SERVER_PATTERN"%d.", i);

This line is too long now.

With this fixed:

Reviewed-by on v1 2/7
#6 lore raw

Re: [PATCH 2/7] block/gluster: Use g_autofree for string in qemu_gluster_parse_json()

In reply to Peter Maydell
Quoted 27 lines · Peter Maydell

In the loop in qemu_gluster_parse_json() we do:

     char *str = NULL;
     for(...) {
         str = g_strdup_printf(...);
         ...
         if (various errors) {
             goto out;
         }
         ...
         g_free(str);
         str = NULL;
     }
     return 0;
out:
     various cleanups;
     g_free(str);
     ...
     return -errno;

Coverity correctly complains that the assignment "str = NULL" at the end of the loop is unnecessary, because we will either go back to the top of the loop and overwrite it, or else we will exit the loop and then exit the function without ever reading str again. The assignment is there as defensive coding to ensure that str is only non-NULL if it's a live allocation, so this is intentional.

Quoted 8 lines

We can make Coverity happier and simplify the code here by using g_autofree, since we never need 'str' outside the loop.

Resolves: Coverity CID 1527385

---
  block/gluster.c | 6 +-----
  1 file changed, 1 insertion(+), 5 deletions(-)
#7 lore raw

PATCH 3/7 hw/block/pflash_cfi01: Don't decrement pfl->counter below 0

In reply to Peter Maydell

In pflash_write() Coverity points out that we can decrement the unsigned pfl->counter below zero, which makes it wrap around. In fact this is harmless, because if pfl->counter is 0 at this point we also increment pfl->wcycle to 3, and the wcycle == 3 handling doesn't look at counter; the only way back into code which looks at the counter value is via wcycle == 1, which will reinitialize the counter. But it's arguably a little clearer to break early in the "counter == 0" if(), to avoid the decrement-below-zero.

Resolves: Coverity CID 1547611

---
 hw/block/pflash_cfi01.c | 1 +
 1 file changed, 1 insertion(+)
hw/block/pflash_cfi01.c +1 -0
index c8f1cf5a872..2f3d1dd509c 100644
@@ -614,6 +614,7 @@ static void pflash_write(PFlashCFI01 *pfl, hwaddr offset,
             if (!pfl->counter) {
                 trace_pflash_write(pfl->name, "block write finished");
                 pfl->wcycle++;
+                break;
             }
 
             pfl->counter--;
Signature · 1 line
2.34.1
Reviewed-by on v1 3/7
#8 lore raw

Re: [PATCH 3/7] hw/block/pflash_cfi01: Don't decrement pfl->counter below 0

Am 31.07.2024 um 16:36 hat Peter Maydell geschrieben:

Quoted 11 lines

In pflash_write() Coverity points out that we can decrement the unsigned pfl->counter below zero, which makes it wrap around. In fact this is harmless, because if pfl->counter is 0 at this point we also increment pfl->wcycle to 3, and the wcycle == 3 handling doesn't look at counter; the only way back into code which looks at the counter value is via wcycle == 1, which will reinitialize the counter. But it's arguably a little clearer to break early in the "counter == 0" if(), to avoid the decrement-below-zero.

Resolves: Coverity CID 1547611

Reviewed-by on v1 3/7
#9 lore raw

Re: [PATCH 3/7] hw/block/pflash_cfi01: Don't decrement pfl->counter below 0

In reply to Peter Maydell
Quoted 9 lines · Peter Maydell

In pflash_write() Coverity points out that we can decrement the unsigned pfl->counter below zero, which makes it wrap around. In fact this is harmless, because if pfl->counter is 0 at this point we also increment pfl->wcycle to 3, and the wcycle == 3 handling doesn't look at counter; the only way back into code which looks at the counter value is via wcycle == 1, which will reinitialize the counter. But it's arguably a little clearer to break early in the "counter == 0" if(), to avoid the decrement-below-zero.

Quoted 5 lines

Resolves: Coverity CID 1547611

---
  hw/block/pflash_cfi01.c | 1 +
  1 file changed, 1 insertion(+)
#10 lore raw

PATCH 4/7 hw/ide/atapi: Be explicit that assigning to s->lcyl truncates

In reply to Peter Maydell

In ide_atapi_cmd_reply_end() we calculate a 16-bit size, and then assign its two halves to s->lcyl and s->hcyl like this:

           s->lcyl = size;
           s->hcyl = size >> 8;

Coverity warns that the first line here can overflow the 8-bit s->lcyl variable. This is true, and in this case we're deliberately only after the low 8 bits of the value. The code is clearer to both humans and Coverity if we're explicit that we only wanted the low 8 bits, though.

---
 hw/ide/atapi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ide/atapi.c +1 -1
index fcb6cca1573..e82959dc2d3 100644
@@ -265,7 +265,7 @@ void ide_atapi_cmd_reply_end(IDEState *s)
                     byte_count_limit--;
                 size = byte_count_limit;
             }
-            s->lcyl = size;
+            s->lcyl = size & 0xff;
             s->hcyl = size >> 8;
             s->elementary_transfer_size = size;
             /* we cannot transmit more than one sector at a time */
Signature · 1 line
2.34.1
Reviewed-by on v1 4/7
#11 lore raw

Re: [PATCH 4/7] hw/ide/atapi: Be explicit that assigning to s->lcyl truncates

Quoted 12 lines · Peter Maydell

In ide_atapi_cmd_reply_end() we calculate a 16-bit size, and then assign its two halves to s->lcyl and s->hcyl like this:

           s->lcyl = size;
           s->hcyl = size >> 8;

Coverity warns that the first line here can overflow the 8-bit s->lcyl variable. This is true, and in this case we're deliberately only after the low 8 bits of the value. The code is clearer to both humans and Coverity if we're explicit that we only wanted the low 8 bits, though.

Resolves?

hw/ide/atapi.c void ide_atapi_cmd_reply_end(IDEState *s)

Quoted 10 lines
---
 hw/ide/atapi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ide/atapi.c +0 -0
index fcb6cca1573..e82959dc2d3 100644
@@ -265,7 +265,7 @@ void ide_atapi_cmd_reply_end(IDEState *s)
               /* a new transfer is needed */
               s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
               ide_bus_set_irq(s->bus);
               byte_count_limit = atapi_byte_count_limit(s);
               trace_ide_atapi_cmd_reply_end_bcl(s, byte_count_limit);
               size = s->packet_transfer_size;
               if (size > byte_count_limit) {
                   /* byte count limit must be even if this case */
                   if (byte_count_limit & 1)

hw/ide/atapi.c void ide_atapi_cmd_reply_end(IDEState *s) · line 271

Quoted 8 lines
hw/ide/atapi.c +1 -1
                     byte_count_limit--;
                 size = byte_count_limit;
             }
-            s->lcyl = size;
+            s->lcyl = size & 0xff;
             s->hcyl = size >> 8;
             s->elementary_transfer_size = size;
             /* we cannot transmit more than one sector at a time */

@size is int. I wonder why it's fine with size >> 8... ah, atapi_byte_count_limit() returns uint16_t, and Coverity is smart enough to data-flow this via @byte_count_limit into @size.

#12 lore raw

Re: [PATCH 4/7] hw/ide/atapi: Be explicit that assigning to s->lcyl truncates

Quoted 9 lines · Markus Armbruster
Quoted 6 lines · Peter Maydell

In ide_atapi_cmd_reply_end() we calculate a 16-bit size, and then assign its two halves to s->lcyl and s->hcyl like this:

           s->lcyl = size;
           s->hcyl = size >> 8;
Quoted 8 lines
Quoted 6 lines

Coverity warns that the first line here can overflow the 8-bit s->lcyl variable. This is true, and in this case we're deliberately only after the low 8 bits of the value. The code is clearer to both humans and Coverity if we're explicit that we only wanted the low 8 bits, though.

Resolves?

Whoops.

Resolves: Coverity CID 1547621

Quoted 28 lines

hw/ide/atapi.c void ide_atapi_cmd_reply_end(IDEState *s)

Quoted 10 lines
---
 hw/ide/atapi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ide/atapi.c +0 -0
index fcb6cca1573..e82959dc2d3 100644
@@ -265,7 +265,7 @@ void ide_atapi_cmd_reply_end(IDEState *s)
               /* a new transfer is needed */
               s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
               ide_bus_set_irq(s->bus);
               byte_count_limit = atapi_byte_count_limit(s);
               trace_ide_atapi_cmd_reply_end_bcl(s, byte_count_limit);
               size = s->packet_transfer_size;
               if (size > byte_count_limit) {
                   /* byte count limit must be even if this case */
                   if (byte_count_limit & 1)

hw/ide/atapi.c void ide_atapi_cmd_reply_end(IDEState *s) · line 271

Quoted 8 lines
hw/ide/atapi.c +1 -1
                     byte_count_limit--;
                 size = byte_count_limit;
             }
-            s->lcyl = size;
+            s->lcyl = size & 0xff;
             s->hcyl = size >> 8;
             s->elementary_transfer_size = size;
             /* we cannot transmit more than one sector at a time */
Quoted 5 lines

@size is int. I wonder why it's fine with size >> 8... ah, atapi_byte_count_limit() returns uint16_t, and Coverity is smart enough to data-flow this via @byte_count_limit into @size.

thanks
-- PMM

Reviewed-by on v1 4/7
#13 lore raw

Re: [PATCH 4/7] hw/ide/atapi: Be explicit that assigning to s->lcyl truncates

In reply to Peter Maydell

Am 31.07.2024 um 16:36 hat Peter Maydell geschrieben:

Quoted 6 lines

In ide_atapi_cmd_reply_end() we calculate a 16-bit size, and then assign its two halves to s->lcyl and s->hcyl like this:

           s->lcyl = size;
           s->hcyl = size >> 8;
Quoted 7 lines

Coverity warns that the first line here can overflow the 8-bit s->lcyl variable. This is true, and in this case we're deliberately only after the low 8 bits of the value. The code is clearer to both humans and Coverity if we're explicit that we only wanted the low 8 bits, though.

#14 lore raw

PATCH 5/7 hw/block/fdc-isa: Assert that isa_fdc_get_drive_max_chs() found something

In reply to Peter Maydell

Coverity complains about an overflow in isa_fdc_get_drive_max_chs() that can happen if the loop over fd_formats never finds a match, because we initialize *maxc to 0 and then at the end of the function decrement it.

This can't ever actually happen because fd_formats has at least one entry for each FloppyDriveType, so we must at least once find a match and update *maxc, *maxh and *maxs. Assert that we did find a match, which should keep Coverity happy and will also detect possible bugs in the data in fd_formats.

Resolves: Coverity CID 1547663

---
 hw/block/fdc-isa.c | 2 ++
 1 file changed, 2 insertions(+)
hw/block/fdc-isa.c +2 -0
index e43dc532af8..796835f57b3 100644
@@ -147,6 +147,8 @@ static void isa_fdc_get_drive_max_chs(FloppyDriveType type, uint8_t *maxc,
             *maxs = fdf->last_sect;
         }
     }
+    /* fd_formats must contain at least one entry per FloppyDriveType */
+    assert(*maxc);
     (*maxc)--;
 }
 
Signature · 1 line
2.34.1
Reviewed-by on v1 5/7
#15 lore raw

Re: [PATCH 5/7] hw/block/fdc-isa: Assert that isa_fdc_get_drive_max_chs() found something

Quoted 22 lines · Peter Maydell

Coverity complains about an overflow in isa_fdc_get_drive_max_chs() that can happen if the loop over fd_formats never finds a match, because we initialize *maxc to 0 and then at the end of the function decrement it.

This can't ever actually happen because fd_formats has at least one entry for each FloppyDriveType, so we must at least once find a match and update *maxc, *maxh and *maxs. Assert that we did find a match, which should keep Coverity happy and will also detect possible bugs in the data in fd_formats.

Resolves: Coverity CID 1547663

---
 hw/block/fdc-isa.c | 2 ++
 1 file changed, 2 insertions(+)
hw/block/fdc-isa.c +0 -0
index e43dc532af8..796835f57b3 100644

hw/block/fdc-isa.c static void isa_fdc_get_drive_max_chs(FloppyDriveType type, uint8_t *maxc, · line 153

Quoted 7 lines
hw/block/fdc-isa.c +2 -0
@@ -147,6 +147,8 @@ static void isa_fdc_get_drive_max_chs(FloppyDriveType type, uint8_t *maxc,
             *maxs = fdf->last_sect;
         }
     }
+    /* fd_formats must contain at least one entry per FloppyDriveType */
+    assert(*maxc);
     (*maxc)--;
 }
Reviewed-by on v1 5/7
#16 lore raw

Re: [PATCH 5/7] hw/block/fdc-isa: Assert that isa_fdc_get_drive_max_chs() found something

In reply to Peter Maydell

Am 31.07.2024 um 16:36 hat Peter Maydell geschrieben:

Quoted 5 lines

Coverity complains about an overflow in isa_fdc_get_drive_max_chs() that can happen if the loop over fd_formats never finds a match, because we initialize *maxc to 0 and then at the end of the function decrement it.

Quoted 8 lines

This can't ever actually happen because fd_formats has at least one entry for each FloppyDriveType, so we must at least once find a match and update *maxc, *maxh and *maxs. Assert that we did find a match, which should keep Coverity happy and will also detect possible bugs in the data in fd_formats.

Resolves: Coverity CID 1547663

Reviewed-by on v1 5/7
#17 lore raw

Re: [PATCH 5/7] hw/block/fdc-isa: Assert that isa_fdc_get_drive_max_chs() found something

In reply to Peter Maydell
Quoted 11 lines · Peter Maydell

Coverity complains about an overflow in isa_fdc_get_drive_max_chs() that can happen if the loop over fd_formats never finds a match, because we initialize *maxc to 0 and then at the end of the function decrement it.

This can't ever actually happen because fd_formats has at least one entry for each FloppyDriveType, so we must at least once find a match and update *maxc, *maxh and *maxs. Assert that we did find a match, which should keep Coverity happy and will also detect possible bugs in the data in fd_formats.

Quoted 5 lines

Resolves: Coverity CID 1547663

---
  hw/block/fdc-isa.c | 2 ++
  1 file changed, 2 insertions(+)
#18 lore raw

PATCH 6/7 hw/ide/pci.c: Remove dead code from bmdma_prepare_buf()

In reply to Peter Maydell

Coverity notes that the code at the end of the loop in bmdma_prepare_buf() is unreachable. This is because in commit 9fbf0fa81fca8f527 ("ide: remove hardcoded 2GiB transactional limit") we removed the only codepath in the loop which could "break" out of it, but didn't notice that this meant we should also remove the code at the end of the loop.

Remove the dead code.

Resolves: Coverity CID 1547772

---
 hw/ide/pci.c | 4 ----
 1 file changed, 4 deletions(-)
hw/ide/pci.c +0 -4
index 4675d079a17..f2cb500a94f 100644
@@ -266,10 +266,6 @@ static int32_t bmdma_prepare_buf(const IDEDMA *dma, int32_t limit)
             s->io_buffer_size += l;
         }
     }
-
-    qemu_sglist_destroy(&s->sg);
-    s->io_buffer_size = 0;
-    return -1;
 }
 
 /* return 0 if buffer completed */
Signature · 1 line
2.34.1
Reviewed-by on v1 6/7
#19 lore raw

Re: [PATCH 6/7] hw/ide/pci.c: Remove dead code from bmdma_prepare_buf()

Am 31.07.2024 um 16:36 hat Peter Maydell geschrieben:

Quoted 20 lines

Coverity notes that the code at the end of the loop in bmdma_prepare_buf() is unreachable. This is because in commit 9fbf0fa81fca8f527 ("ide: remove hardcoded 2GiB transactional limit") we removed the only codepath in the loop which could "break" out of it, but didn't notice that this meant we should also remove the code at the end of the loop.

Remove the dead code.

Resolves: Coverity CID 1547772

---
 hw/ide/pci.c | 4 ----
 1 file changed, 4 deletions(-)
hw/ide/pci.c +0 -0
index 4675d079a17..f2cb500a94f 100644

hw/ide/pci.c static int32_t bmdma_prepare_buf(const IDEDMA *dma, int32_t limit) · line 269

Quoted 8 lines
hw/ide/pci.c +0 -4
@@ -266,10 +266,6 @@ static int32_t bmdma_prepare_buf(const IDEDMA *dma, int32_t limit)
             s->io_buffer_size += l;
         }
     }
-
-    qemu_sglist_destroy(&s->sg);
-    s->io_buffer_size = 0;
-    return -1;
 }

Should we put a g_assert_not_reached() here instead to make it easier for the reader to understand how this function works?

Either way:

#20 lore raw

Re: [PATCH 6/7] hw/ide/pci.c: Remove dead code from bmdma_prepare_buf()

Quoted 16 lines · Kevin Wolf

Am 31.07.2024 um 16:36 hat Peter Maydell geschrieben:

Quoted 15 lines

Coverity notes that the code at the end of the loop in bmdma_prepare_buf() is unreachable. This is because in commit 9fbf0fa81fca8f527 ("ide: remove hardcoded 2GiB transactional limit") we removed the only codepath in the loop which could "break" out of it, but didn't notice that this meant we should also remove the code at the end of the loop.

Remove the dead code.

Resolves: Coverity CID 1547772

---
  hw/ide/pci.c | 4 ----
  1 file changed, 4 deletions(-)
Quoted 16 lines

hw/ide/pci.c static int32_t bmdma_prepare_buf(const IDEDMA *dma, int32_t limit) · line 269

Quoted 13 lines
hw/ide/pci.c +0 -4
index 4675d079a17..f2cb500a94f 100644
@@ -266,10 +266,6 @@ static int32_t bmdma_prepare_buf(const IDEDMA *dma, int32_t limit)
              s->io_buffer_size += l;
          }
      }
-
-    qemu_sglist_destroy(&s->sg);
-    s->io_buffer_size = 0;
-    return -1;
  }

Should we put a g_assert_not_reached() here instead to make it easier for the reader to understand how this function works?

Or break and keep returning at EOF:

-- >8 --

hw/ide/pci.c +2 -5
index 4675d079a1..8386c4fe42 100644
@@ -237,7 +237,7 @@ static int32_t bmdma_prepare_buf(const IDEDMA *dma, int32_t limit)
             /* end of table (with a fail safe of one page) */
             if (bm->cur_prd_last ||
                 (bm->cur_addr - bm->addr) >= BMDMA_PAGE_SIZE) {
-                return s->sg.size;
+                break;
             }
             pci_dma_read(pci_dev, bm->cur_addr, &prd, 8);
             bm->cur_addr += 8;
@@ -267,9 +267,7 @@ static int32_t bmdma_prepare_buf(const IDEDMA *dma, int32_t limit)
         }
     }
 
-    qemu_sglist_destroy(&s->sg);
-    s->io_buffer_size = 0;
-    return -1;
+    return s->sg.size;
 }
 
---
Quoted 4 lines

Either way:

#21 lore raw

PATCH 7/7 block/ssh.c: Don't double-check that characters are hex digits

In reply to Peter Maydell

In compare_fingerprint() we effectively check whether the characters in the fingerprint are valid hex digits twice: first we do so with qemu_isxdigit(), but then the hex2decimal() function also has a code path where it effectively detects an invalid digit and returns -1. This causes Coverity to complain because it thinks that we might use that -1 value in an expression where it would be an integer overflow.

Avoid the double-check of hex digit validity by testing the return values from hex2decimal() rather than doing separate calls to qemu_isxdigit().

---
Could alternatively have put a g_assert_non_reached() in hex2decimal(), but this seemed better to me.

---
 block/ssh.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
block/ssh.c +6 -4
index 27d582e0e3d..510dd208aba 100644
@@ -376,13 +376,15 @@ static int compare_fingerprint(const unsigned char *fingerprint, size_t len,
     unsigned c;
 
     while (len > 0) {
+        unsigned c0, c1;
         while (*host_key_check == ':')
             host_key_check++;
-        if (!qemu_isxdigit(host_key_check[0]) ||
-            !qemu_isxdigit(host_key_check[1]))
+        c0 = hex2decimal(host_key_check[0]);
+        c1 = hex2decimal(host_key_check[1]);
+        if (c0 > 0xf || c1 > 0xf) {
             return 1;
-        c = hex2decimal(host_key_check[0]) * 16 +
-            hex2decimal(host_key_check[1]);
+        }
+        c = c0 * 16 + c1;
         if (c - *fingerprint != 0)
             return c - *fingerprint;
         fingerprint++;
Signature · 1 line
2.34.1
Reviewed-by on v1 7/7
#22 lore raw

Re: [PATCH 7/7] block/ssh.c: Don't double-check that characters are hex digits

Am 31.07.2024 um 16:36 hat Peter Maydell geschrieben:

Quoted 6 lines

In compare_fingerprint() we effectively check whether the characters in the fingerprint are valid hex digits twice: first we do so with qemu_isxdigit(), but then the hex2decimal() function also has a code path where it effectively detects an invalid digit and returns -1. This causes Coverity to complain because it thinks that we might use that -1 value in an expression where it would be an integer overflow.

If it's a Coverity issue, I think you want to mention the CID, too.

Quoted 8 lines

Avoid the double-check of hex digit validity by testing the return values from hex2decimal() rather than doing separate calls to qemu_isxdigit().

---
Could alternatively have put a g_assert_non_reached() in hex2decimal(), but this seemed better to me.

I don't like that hex2decimal() returns -1 when its result is unsigned, which is why you had to write the check as > 0xf rather than < 0. So in this sense, g_assert_non_reached() would look better to me. But we could also just change it to return UINT_MAX instead, which should be the same, just written in a less confusing way.

Quoted 17 lines
 block/ssh.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
block/ssh.c +2 -2
index 27d582e0e3d..510dd208aba 100644
@@ -376,13 +376,15 @@ static int compare_fingerprint(const unsigned char *fingerprint, size_t len,
     unsigned c;
 
     while (len > 0) {
+        unsigned c0, c1;
         while (*host_key_check == ':')
             host_key_check++;
-        if (!qemu_isxdigit(host_key_check[0]) ||
-            !qemu_isxdigit(host_key_check[1]))
+        c0 = hex2decimal(host_key_check[0]);

block/ssh.c static int compare_fingerprint(const unsigned char *fingerprint, size_t len, · line 390

Quoted 10 lines
block/ssh.c +4 -2
+        c1 = hex2decimal(host_key_check[1]);
+        if (c0 > 0xf || c1 > 0xf) {
             return 1;
-        c = hex2decimal(host_key_check[0]) * 16 +
-            hex2decimal(host_key_check[1]);
+        }
+        c = c0 * 16 + c1;
         if (c - *fingerprint != 0)
             return c - *fingerprint;
         fingerprint++;
#23 lore raw

Re: [PATCH 7/7] block/ssh.c: Don't double-check that characters are hex digits

Quoted 10 lines · Kevin Wolf

Am 31.07.2024 um 16:36 hat Peter Maydell geschrieben:

Quoted 6 lines

In compare_fingerprint() we effectively check whether the characters in the fingerprint are valid hex digits twice: first we do so with qemu_isxdigit(), but then the hex2decimal() function also has a code path where it effectively detects an invalid digit and returns -1. This causes Coverity to complain because it thinks that we might use that -1 value in an expression where it would be an integer overflow.

If it's a Coverity issue, I think you want to mention the CID, too.

Yes;

Resolves: Coverity CID 1547813

Quoted 9 lines
Quoted 8 lines

Avoid the double-check of hex digit validity by testing the return values from hex2decimal() rather than doing separate calls to qemu_isxdigit().

---
Could alternatively have put a g_assert_non_reached() in hex2decimal(), but this seemed better to me.

Quoted 5 lines

I don't like that hex2decimal() returns -1 when its result is unsigned, which is why you had to write the check as > 0xf rather than < 0. So in this sense, g_assert_non_reached() would look better to me. But we could also just change it to return UINT_MAX instead, which should be the same, just written in a less confusing way.

I was not super happy with the -1 either. Happy to change that to 'return UINT_MAX'.

-- PMM

#24 lore raw
In reply to Peter Maydell
Quoted 6 lines · Peter Maydell

Peter Maydell (7):
hw/block/pflash_cfi01: Don't decrement pfl->counter below 0 hw/ide/atapi: Be explicit that assigning to s->lcyl truncates hw/block/fdc-isa: Assert that isa_fdc_get_drive_max_chs() found
something
hw/ide/pci.c: Remove dead code from bmdma_prepare_buf()

Patches 3-5 queued, thanks.

#25 lore raw
In reply to Peter Maydell
Quoted 4 lines · Peter Maydell

hw/block/pflash_cfi01: Don't decrement pfl->counter below 0 hw/ide/atapi: Be explicit that assigning to s->lcyl truncates hw/block/fdc-isa: Assert that isa_fdc_get_drive_max_chs() found
something

Patches 3-5 queued, thanks.