Skip to contentSkip to conversation

easy-to-fix clang warnings

PATCH RESEND Superseded Philippe Mathieu-Daudé 5 participants 15 messages Mar 22, 2017 – Apr 7, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover easy-to-fix clang warnings

This patchset fixes three easy-to-fix clang warnings.

Resent adding Marc-André Lureau's Reviewed-by and CC'ing qemu-trivial as suggested by Markus Armbruster.

Philippe Mathieu-Daudé (3):
usb-ccid: make ccid_write_data_block() cope with null buffers
device_tree: fix compiler warnings (clang 5)
qga: fix compiler warnings (clang 5)

 device_tree.c                 | 1 +
 hw/usb/dev-smartcard-reader.c | 8 +++++++-
 qga/commands-posix.c          | 8 +++++---
 3 files changed, 13 insertions(+), 4 deletions(-)
Signature · 1 line
2.11.0
#1 lore raw

PATCH RESEND 1/3 usb-ccid: make ccid_write_data_block() cope with null buffers

static code analyzer complain:

hw/usb/dev-smartcard-reader.c:816:5: warning: Null pointer passed as an argument to a 'nonnull' parameter

    memcpy(p->abData, data, len);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 hw/usb/dev-smartcard-reader.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/usb/dev-smartcard-reader.c +7 -1
index 757b8b3f5a..c38a4e5886 100644
@@ -799,8 +799,14 @@ static void ccid_write_parameters(USBCCIDState *s, CCID_Header *recv)
 static void ccid_write_data_block(USBCCIDState *s, uint8_t slot, uint8_t seq,
                                   const uint8_t *data, uint32_t len)
 {
-    CCID_DataBlock *p = ccid_reserve_recv_buf(s, sizeof(*p) + len);
+    CCID_DataBlock *p;
 
+    if (len == 0) {
+        return;
+    }
+    g_assert(data != NULL);
+
+    p = ccid_reserve_recv_buf(s, sizeof(*p) + len);
     if (p == NULL) {
         return;
     }
Signature · 1 line
2.11.0
#10 lore raw

PATCH RESEND 2/3 device_tree: fix compiler warnings (clang 5)

In reply to Philippe Mathieu-Daudé

static code analyzer complain:

device_tree.c:155:18: warning: Null pointer passed as an argument to a 'nonnull' parameter

    while ((de = readdir(d)) != NULL) {
                 ^~~~~~~~~~
Reported-by Clang Static Analyzer
---
 device_tree.c | 1 +
 1 file changed, 1 insertion(+)
device_tree.c +1 -0
index 6e06320830..a24ddff02b 100644
@@ -148,6 +148,7 @@ static void read_fstree(void *fdt, const char *dirname)
     d = opendir(dirname);
     if (!d) {
         error_setg(&error_fatal, "%s cannot open %s", __func__, dirname);
+        return;
     }
 
     while ((de = readdir(d)) != NULL) {
Signature · 1 line
2.11.0
#12 lore raw

PATCH RESEND 3/3 qga: fix compiler warnings (clang 5)

In reply to Philippe Mathieu-Daudé

static code analyzer complain:

qga/commands-posix.c:2127:9: warning: Null pointer passed as an argument to a 'nonnull' parameter

        closedir(dp);
        ^~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 qga/commands-posix.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
qga/commands-posix.c +5 -3
index 73d93eb5ce..8028141534 100644
@@ -2122,9 +2122,11 @@ static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool sys2memblk,
          * we think this VM does not support online/offline memory block,
          * any other solution?
          */
-        if (!dp && errno == ENOENT) {
-            result->response =
-                GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
+        if (!dp) {
+            if (errno == ENOENT) {
+                result->response =
+                    GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
+            }
             goto out1;
         }
         closedir(dp);
Signature · 1 line
2.11.0
#13 lore raw

Re: [Qemu-devel] [PATCH RESEND 3/3] qga: fix compiler warnings (clang 5)

Quoting Philippe Mathieu-Daudé (2017-03-22 15:48:44)

Quoted 21 lines

static code analyzer complain:

qga/commands-posix.c:2127:9: warning: Null pointer passed as an argument to a 'nonnull' parameter

        closedir(dp);
        ^~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 qga/commands-posix.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
qga/commands-posix.c +0 -0
index 73d93eb5ce..8028141534 100644
@@ -2122,9 +2122,11 @@ static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool sys2memblk,
          * we think this VM does not support online/offline memory block,
          * any other solution?
          */

qga/commands-posix.c static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool sys2memblk, · line 2131

Quoted 10 lines
qga/commands-posix.c +5 -3
-        if (!dp && errno == ENOENT) {
-            result->response =
-                GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
+        if (!dp) {
+            if (errno == ENOENT) {
+                result->response =
+                    GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
+            }
             goto out1;
         }

I think this should be:

    if (!dp) {
        if (errno == ENOENT) {
            result->response =
                GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
        } else {
            result->response =
                GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
        }
        goto out1;
    }

otherwise we might set result->error_code while still indicating success for the operation. That wasn't handled properly before your patch either, it's just more apparent now.

Quoted 4 lines
qga/commands-posix.c +0 -0
         closedir(dp);
Signature · 1 line
2.11.0
Reviewed-by on v1 2/3
#11 lore raw

Re: [Qemu-devel] [PATCH RESEND 2/3] device_tree: fix compiler warnings (clang 5)

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

static code analyzer complain:

device_tree.c:155:18: warning: Null pointer passed as an argument to a 'nonnull' parameter

    while ((de = readdir(d)) != NULL) {
                 ^~~~~~~~~~
Reported-by Clang Static Analyzer
Quoted 20 lines
---
 device_tree.c | 1 +
 1 file changed, 1 insertion(+)
device_tree.c +1 -1
index 6e06320830..a24ddff02b 100644
@@ -148,6 +148,7 @@ static void read_fstree(void *fdt, const char *dirname)
     d = opendir(dirname);
     if (!d) {
         error_setg(&error_fatal, "%s cannot open %s", __func__, dirname);
+        return;
     }
 
     while ((de = readdir(d)) != NULL) {
--

2.11.0

Quoted 1 line

--

Marc-André Lureau

#2 lore raw

Re: [Qemu-devel] [PATCH RESEND 1/3] usb-ccid: make ccid_write_data_block() cope with null buffers

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

static code analyzer complain:

hw/usb/dev-smartcard-reader.c:816:5: warning: Null pointer passed as an argument to a 'nonnull' parameter

    memcpy(p->abData, data, len);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 hw/usb/dev-smartcard-reader.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/usb/dev-smartcard-reader.c +0 -0
index 757b8b3f5a..c38a4e5886 100644

hw/usb/dev-smartcard-reader.c static void ccid_write_parameters(USBCCIDState *s, CCID_Header *recv) · line 805

Quoted 8 lines
hw/usb/dev-smartcard-reader.c +3 -1
@@ -799,8 +799,14 @@ static void ccid_write_parameters(USBCCIDState *s, CCID_Header *recv)
 static void ccid_write_data_block(USBCCIDState *s, uint8_t slot, uint8_t seq,
                                   const uint8_t *data, uint32_t len)
 {
-    CCID_DataBlock *p = ccid_reserve_recv_buf(s, sizeof(*p) + len);
+    CCID_DataBlock *p;
 
+    if (len == 0) {
+        return;

Correct only if messages without data always have the same meaning as no message. Gerd?

hw/usb/dev-smartcard-reader.c static void ccid_write_parameters(USBCCIDState *s, CCID_Header *recv) · line 807

Quoted 2 lines
hw/usb/dev-smartcard-reader.c +2 -0
+    }
+    g_assert(data != NULL);

The assertion feels like noise to me.

Quoted 5 lines
hw/usb/dev-smartcard-reader.c +2 -0
+
+    p = ccid_reserve_recv_buf(s, sizeof(*p) + len);
     if (p == NULL) {
         return;
     }
#3 lore raw

Re: [Qemu-devel] [PATCH RESEND 1/3] usb-ccid: make ccid_write_data_block() cope with null buffers

Hi,

Quoted 5 lines

hw/usb/dev-smartcard-reader.c static void ccid_write_parameters(USBCCIDState *s, CCID_Header *recv) · line 805

Quoted 2 lines
hw/usb/dev-smartcard-reader.c +2 -0
+    if (len == 0) {
+        return;

Correct only if messages without data always have the same meaning as no message. Gerd?

Not a ccid expert, but looking through the code it seems writing a (reply) data block with status and without payload (data = NULL and len = 0) is perfectly fine and can happen in case no (virtual) smartcard is inserted into the card reader. Which this patch breaks. So,

NACK.

cheers,
Gerd

#4 lore raw

Re: [Qemu-devel] [PATCH RESEND 1/3] usb-ccid: make ccid_write_data_block() cope with null buffers

Hi

Quoted 8 lines · Gerd Hoffmann

Hi,

Quoted 5 lines

hw/usb/dev-smartcard-reader.c static void ccid_write_parameters(USBCCIDState *s, CCID_Header *recv) · line 805

Quoted 2 lines
hw/usb/dev-smartcard-reader.c +2 -0
+    if (len == 0) {
+        return;

Correct only if messages without data always have the same meaning as no message. Gerd?

Quoted 7 lines

Not a ccid expert, but looking through the code it seems writing a (reply) data block with status and without payload (data = NULL and len = 0) is perfectly fine and can happen in case no (virtual) smartcard is inserted into the card reader. Which this patch breaks. So,

NACK.

oops, there are hard-coded calls with NULL/0. I suppose to fix clang
warning, it would need to check if data != null for memcpy.

Signature · 1 line
Marc-André Lureau
#5 lore raw

Re: [Qemu-devel] [PATCH RESEND 1/3] usb-ccid: make ccid_write_data_block() cope with null buffers

Hi,

Quoted 2 lines

oops, there are hard-coded calls with NULL/0. I suppose to fix clang
warning, it would need to check if data != null for memcpy.

I'd check for len > 0, and in that if branch we can also assert on data == NULL and thereby check that len and data are consistent.

cheers,
Gerd

#6 lore raw

Re: [Qemu-devel] [PATCH RESEND 1/3] usb-ccid: make ccid_write_data_block() cope with null buffers

Quoted 7 lines · Gerd Hoffmann

Hi,

Quoted 2 lines

oops, there are hard-coded calls with NULL/0. I suppose to fix clang
warning, it would need to check if data != null for memcpy.

I'd check for len > 0, and in that if branch we can also assert on data == NULL and thereby check that len and data are consistent.

If len is non-zero but data is null, memcpy() will crash just fine by itself, so why bother asserting. If len is zero, there's nothing to assert.

I'd simply protect memcpy() with if (len) and call it a day.

#7 lore raw

Re: [Qemu-devel] [PATCH RESEND 1/3] usb-ccid: make ccid_write_data_block() cope with null buffers

Quoted 4 lines · Markus Armbruster
Quoted 2 lines · Gerd Hoffmann

Hi,

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

oops, there are hard-coded calls with NULL/0. I suppose to fix clang
warning, it would need to check if data != null for memcpy.

I'd check for len > 0, and in that if branch we can also assert on data == NULL and thereby check that len and data are consistent.

If len is non-zero but data is null, memcpy() will crash just fine by itself, so why bother asserting.

To make clang happy? But maybe clang is clever enough to figure data can't be null at that point in case we call memcpy with len != 0 only ...

cheers,
Gerd

#8 lore raw

Re: [Qemu-devel] [PATCH RESEND 1/3] usb-ccid: make ccid_write_data_block() cope with null buffers

Quoted 11 lines · Gerd Hoffmann
Quoted 10 lines · Markus Armbruster
Quoted 7 lines · Gerd Hoffmann

Hi,

Quoted 2 lines

oops, there are hard-coded calls with NULL/0. I suppose to fix clang
warning, it would need to check if data != null for memcpy.

I'd check for len > 0, and in that if branch we can also assert on data == NULL and thereby check that len and data are consistent.

Quoted 6 lines
Quoted 2 lines

If len is non-zero but data is null, memcpy() will crash just fine by itself, so why bother asserting.

To make clang happy? But maybe clang is clever enough to figure data can't be null at that point in case we call memcpy with len != 0 only ...

If Clang needs another hint to become happy, then an assertion is a fine way to provide it.

#14 lore raw

Re: [Qemu-devel] [PATCH RESEND 3/3] qga: fix compiler warnings (clang 5)

In reply to Michael Roth

Hi Michael,

Quoted 46 lines · Michael Roth

Quoting Philippe Mathieu-Daudé (2017-03-22 15:48:44)

Quoted 21 lines

static code analyzer complain:

qga/commands-posix.c:2127:9: warning: Null pointer passed as an argument to a 'nonnull' parameter

        closedir(dp);
        ^~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 qga/commands-posix.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
qga/commands-posix.c +0 -0
index 73d93eb5ce..8028141534 100644
@@ -2122,9 +2122,11 @@ static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool sys2memblk,
          * we think this VM does not support online/offline memory block,
          * any other solution?
          */

qga/commands-posix.c static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool sys2memblk, · line 2131

Quoted 10 lines
qga/commands-posix.c +5 -3
-        if (!dp && errno == ENOENT) {
-            result->response =
-                GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
+        if (!dp) {
+            if (errno == ENOENT) {
+                result->response =
+                    GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
+            }
             goto out1;
         }

I think this should be:

    if (!dp) {
        if (errno == ENOENT) {
            result->response =
                GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
        } else {
            result->response =
                GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
        }
        goto out1;
    }
Quoted 3 lines

otherwise we might set result->error_code while still indicating success for the operation. That wasn't handled properly before your patch either, it's just more apparent now.

Indeed, I'll resend it in 2 patches It seems easier to me to understand.

Quoted 7 lines
Quoted 5 lines
qga/commands-posix.c +0 -1
         closedir(dp);
--

2.11.0

#9 lore raw

Re: [Qemu-devel] [PATCH RESEND 1/3] usb-ccid: make ccid_write_data_block() cope with null buffers

In reply to Markus Armbruster

Hi Markus, Gerd.

Quoted 16 lines · Markus Armbruster
Quoted 14 lines · Gerd Hoffmann
Quoted 12 lines · Markus Armbruster
Quoted 7 lines · Gerd Hoffmann

Hi,

Quoted 2 lines

oops, there are hard-coded calls with NULL/0. I suppose to fix clang
warning, it would need to check if data != null for memcpy.

I'd check for len > 0, and in that if branch we can also assert on data == NULL and thereby check that len and data are consistent.

If len is non-zero but data is null, memcpy() will crash just fine by itself, so why bother asserting.

Quoted 6 lines
Quoted 3 lines

To make clang happy? But maybe clang is clever enough to figure data can't be null at that point in case we call memcpy with len != 0 only ...

If Clang needs another hint to become happy, then an assertion is a fine way to provide it.

Apparently Clang isn't clever enough using an assertion.

I'll resend checking len.