Skip to contentSkip to conversation

tests: Fix some minor Coverity issues

PATCH In review Peter Maydell 3 participants 17 messages May 25, 2021

lore
Share & tools
lore
#0 lore raw

cover tests: Fix some minor Coverity issues

We have a backlog of Coverity issues in tests (because we only started running the scan on test code when we switched to meson and started building the test code in "make" rather than only in "make check"). This series fixes some of the easier ones. There's nothing exciting here, it's all little nits.

thanks
-- PMM

Peter Maydell (6):
tests/qtest/bios-tables-test: Check for dup2() failure tests/qtest/e1000e-test: Check qemu_recv() succeeded tests/qtest/hd-geo-test: Fix checks on mkstemp() return value tests/qtest/pflash-cfi02-test: Avoid potential integer overflow tests/qtest/tpm-tests: Remove unnecessary NULL checks tests/unit/test-vmstate: Assert that dup() and mkstemp() succeed

 tests/qtest/bios-tables-test.c  |  8 ++++++--
 tests/qtest/e1000e-test.c       |  3 ++-
 tests/qtest/hd-geo-test.c       |  4 ++--
 tests/qtest/pflash-cfi02-test.c |  2 +-
 tests/qtest/tpm-tests.c         | 12 ++++--------
 tests/unit/test-vmstate.c       |  2 ++
 6 files changed, 17 insertions(+), 14 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/6 tests/qtest/bios-tables-test: Check for dup2() failure

Coverity notes that we don't check for dup2() failing. Add some assertions so that if it does ever happen we get some indication. (This is similar to how we handle other "don't expect this syscall to fail" checks in this test code.)

Fixes Coverity CID 1432346
---
 tests/qtest/bios-tables-test.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tests/qtest/bios-tables-test.c +6 -2
index 156d4174aa3..51d3a4e2390 100644
@@ -489,10 +489,14 @@ static void test_acpi_asl(test_data *data)
                                                  exp_sdt->asl_file, sdt->asl_file);
                     int out = dup(STDOUT_FILENO);
                     int ret G_GNUC_UNUSED;
+                    int dupret;
 
-                    dup2(STDERR_FILENO, STDOUT_FILENO);
+                    g_assert(out >= 0);
+                    dupret = dup2(STDERR_FILENO, STDOUT_FILENO);
+                    g_assert(dupret >= 0);
                     ret = system(diff) ;
-                    dup2(out, STDOUT_FILENO);
+                    dupret = dup2(out, STDOUT_FILENO);
+                    g_assert(dupret >= 0);
                     close(out);
                     g_free(diff);
                 }
Signature · 1 line
2.20.1
Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] tests/qtest/bios-tables-test: Check for dup2() failure

Quoted 7 lines · Peter Maydell

Coverity notes that we don't check for dup2() failing. Add some assertions so that if it does ever happen we get some indication. (This is similar to how we handle other "don't expect this syscall to fail" checks in this test code.)

Fixes Coverity CID 1432346
Quoted 25 lines
---
  tests/qtest/bios-tables-test.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
tests/qtest/bios-tables-test.c +6 -2
index 156d4174aa3..51d3a4e2390 100644
@@ -489,10 +489,14 @@ static void test_acpi_asl(test_data *data)
                                                   exp_sdt->asl_file, sdt->asl_file);
                      int out = dup(STDOUT_FILENO);
                      int ret G_GNUC_UNUSED;
+                    int dupret;
 
-                    dup2(STDERR_FILENO, STDOUT_FILENO);
+                    g_assert(out >= 0);
+                    dupret = dup2(STDERR_FILENO, STDOUT_FILENO);
+                    g_assert(dupret >= 0);
                      ret = system(diff) ;
-                    dup2(out, STDOUT_FILENO);
+                    dupret = dup2(out, STDOUT_FILENO);
+                    g_assert(dupret >= 0);
                      close(out);
                      g_free(diff);
                  }
#3 lore raw

PATCH 2/6 tests/qtest/e1000e-test: Check qemu_recv() succeeded

In reply to Peter Maydell

The e1000e_send_verify() test calls qemu_recv() but doesn't check that the call succeeded, which annoys Coverity. Add an explicit test check for the length of the data.

(This is a test check, not a "we assume this syscall always succeeds", so we use g_assert_cmpint() rather than g_assert().)

Fixes Coverity CID 1432324
---
 tests/qtest/e1000e-test.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tests/qtest/e1000e-test.c +2 -1
index fc226fdfeb5..0273fe4c156 100644
@@ -93,7 +93,8 @@ static void e1000e_send_verify(QE1000E *d, int *test_sockets, QGuestAllocator *a
     /* Check data sent to the backend */
     ret = qemu_recv(test_sockets[0], &recv_len, sizeof(recv_len), 0);
     g_assert_cmpint(ret, == , sizeof(recv_len));
-    qemu_recv(test_sockets[0], buffer, 64, 0);
+    ret = qemu_recv(test_sockets[0], buffer, 64, 0);
+    g_assert_cmpint(ret, >=, 5);
     g_assert_cmpstr(buffer, == , "TEST");
 
     /* Free test data buffer */
Signature · 1 line
2.20.1
Reviewed-by on v1 2/6
#4 lore raw

Re: [PATCH 2/6] tests/qtest/e1000e-test: Check qemu_recv() succeeded

Quoted 9 lines · Peter Maydell

The e1000e_send_verify() test calls qemu_recv() but doesn't check that the call succeeded, which annoys Coverity. Add an explicit test check for the length of the data.

(This is a test check, not a "we assume this syscall always succeeds", so we use g_assert_cmpint() rather than g_assert().)

Fixes Coverity CID 1432324
Quoted 18 lines
---
  tests/qtest/e1000e-test.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
tests/qtest/e1000e-test.c +2 -1
index fc226fdfeb5..0273fe4c156 100644
@@ -93,7 +93,8 @@ static void e1000e_send_verify(QE1000E *d, int *test_sockets, QGuestAllocator *a
      /* Check data sent to the backend */
      ret = qemu_recv(test_sockets[0], &recv_len, sizeof(recv_len), 0);
      g_assert_cmpint(ret, == , sizeof(recv_len));
-    qemu_recv(test_sockets[0], buffer, 64, 0);
+    ret = qemu_recv(test_sockets[0], buffer, 64, 0);
+    g_assert_cmpint(ret, >=, 5);
      g_assert_cmpstr(buffer, == , "TEST");
 
      /* Free test data buffer */
#5 lore raw

PATCH 3/6 tests/qtest/hd-geo-test: Fix checks on mkstemp() return value

In reply to Peter Maydell

Coverity notices that the checks against mkstemp() failing in create_qcow2_with_mbr() are wrong: mkstemp returns -1 on failure but the check is just "g_assert(fd)". Fix to use "g_assert(fd >= 0)", matching the correct check in create_test_img().

Fixes Coverity CID 1432274
---
 tests/qtest/hd-geo-test.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/qtest/hd-geo-test.c +2 -2
index f7b7cfbc2d1..113126ae06c 100644
@@ -464,7 +464,7 @@ static char *create_qcow2_with_mbr(MBRpartitions mbr, uint64_t sectors)
     }
 
     fd = mkstemp(raw_path);
-    g_assert(fd);
+    g_assert(fd >= 0);
     close(fd);
 
     fd = open(raw_path, O_WRONLY);
@@ -474,7 +474,7 @@ static char *create_qcow2_with_mbr(MBRpartitions mbr, uint64_t sectors)
     close(fd);
 
     fd = mkstemp(qcow2_path);
-    g_assert(fd);
+    g_assert(fd >= 0);
     close(fd);
 
     qemu_img_path = getenv("QTEST_QEMU_IMG");
Signature · 1 line
2.20.1
Reviewed-by on v1 3/6
#6 lore raw

Re: [PATCH 3/6] tests/qtest/hd-geo-test: Fix checks on mkstemp() return value

Quoted 10 lines · Peter Maydell

Coverity notices that the checks against mkstemp() failing in create_qcow2_with_mbr() are wrong: mkstemp returns -1 on failure but the check is just "g_assert(fd)". Fix to use "g_assert(fd >= 0)", matching the correct check in create_test_img().

Fixes Coverity CID 1432274
---
 tests/qtest/hd-geo-test.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Reviewed-by on v1 3/6
#7 lore raw

Re: [PATCH 3/6] tests/qtest/hd-geo-test: Fix checks on mkstemp() return value

In reply to Peter Maydell
Quoted 7 lines · Peter Maydell

Coverity notices that the checks against mkstemp() failing in create_qcow2_with_mbr() are wrong: mkstemp returns -1 on failure but the check is just "g_assert(fd)". Fix to use "g_assert(fd >= 0)", matching the correct check in create_test_img().

Fixes Coverity CID 1432274
Quoted 26 lines
---
  tests/qtest/hd-geo-test.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tests/qtest/hd-geo-test.c +2 -2
index f7b7cfbc2d1..113126ae06c 100644
@@ -464,7 +464,7 @@ static char *create_qcow2_with_mbr(MBRpartitions mbr, uint64_t sectors)
      }
 
      fd = mkstemp(raw_path);
-    g_assert(fd);
+    g_assert(fd >= 0);
      close(fd);
 
      fd = open(raw_path, O_WRONLY);
@@ -474,7 +474,7 @@ static char *create_qcow2_with_mbr(MBRpartitions mbr, uint64_t sectors)
      close(fd);
 
      fd = mkstemp(qcow2_path);
-    g_assert(fd);
+    g_assert(fd >= 0);
      close(fd);
 
      qemu_img_path = getenv("QTEST_QEMU_IMG");
#8 lore raw

PATCH 4/6 tests/qtest/pflash-cfi02-test: Avoid potential integer overflow

In reply to Peter Maydell

Coverity points out that we calculate a 64-bit value using 32-bit arithmetic; add the cast to force the multiply to be done as 64-bits. (The overflow will never happen with the current test data.)

Fixes Coverity CID 1432320
---
 tests/qtest/pflash-cfi02-test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/qtest/pflash-cfi02-test.c +1 -1
index 60db81a3a2b..6168edc821a 100644
@@ -406,7 +406,7 @@ static void test_geometry(const void *opaque)
 
     for (int region = 0; region < nb_erase_regions; ++region) {
         for (uint32_t i = 0; i < c->nb_blocs[region]; ++i) {
-            uint64_t byte_addr = i * c->sector_len[region];
+            uint64_t byte_addr = (uint64_t)i * c->sector_len[region];
             g_assert_cmphex(flash_read(c, byte_addr), ==, bank_mask(c));
         }
     }
Signature · 1 line
2.20.1
Reviewed-by on v1 4/6
#9 lore raw

Re: [PATCH 4/6] tests/qtest/pflash-cfi02-test: Avoid potential integer overflow

Quoted 9 lines · Peter Maydell

Coverity points out that we calculate a 64-bit value using 32-bit arithmetic; add the cast to force the multiply to be done as 64-bits. (The overflow will never happen with the current test data.)

Fixes Coverity CID 1432320
---
 tests/qtest/pflash-cfi02-test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Reviewed-by on v1 4/6
#10 lore raw

Re: [PATCH 4/6] tests/qtest/pflash-cfi02-test: Avoid potential integer overflow

In reply to Peter Maydell
Quoted 6 lines · Peter Maydell

Coverity points out that we calculate a 64-bit value using 32-bit arithmetic; add the cast to force the multiply to be done as 64-bits. (The overflow will never happen with the current test data.)

Fixes Coverity CID 1432320
Quoted 17 lines
---
  tests/qtest/pflash-cfi02-test.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tests/qtest/pflash-cfi02-test.c +1 -1
index 60db81a3a2b..6168edc821a 100644
@@ -406,7 +406,7 @@ static void test_geometry(const void *opaque)
 
      for (int region = 0; region < nb_erase_regions; ++region) {
          for (uint32_t i = 0; i < c->nb_blocs[region]; ++i) {
-            uint64_t byte_addr = i * c->sector_len[region];
+            uint64_t byte_addr = (uint64_t)i * c->sector_len[region];
              g_assert_cmphex(flash_read(c, byte_addr), ==, bank_mask(c));
          }
      }
#11 lore raw

PATCH 5/6 tests/qtest/tpm-tests: Remove unnecessary NULL checks

In reply to Peter Maydell

Coverity points out that in tpm_test_swtpm_migration_test() we assume that src_tpm_addr and dst_tpm_addr are non-NULL (we pass them to tpm_util_migration_start_qemu() which will unconditionally dereference them) but then later explicitly check them for NULL. Remove the pointless checks.

Fixes Coverity CID 1432367, 1432359
---
 tests/qtest/tpm-tests.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
tests/qtest/tpm-tests.c +4 -8
index 0da3a8a4df5..25073d1f9e9 100644
@@ -123,14 +123,10 @@ void tpm_test_swtpm_migration_test(const char *src_tpm_path,
     qtest_quit(src_qemu);
 
     tpm_util_swtpm_kill(dst_tpm_pid);
-    if (dst_tpm_addr) {
-        g_unlink(dst_tpm_addr->u.q_unix.path);
-        qapi_free_SocketAddress(dst_tpm_addr);
-    }
+    g_unlink(dst_tpm_addr->u.q_unix.path);
+    qapi_free_SocketAddress(dst_tpm_addr);
 
     tpm_util_swtpm_kill(src_tpm_pid);
-    if (src_tpm_addr) {
-        g_unlink(src_tpm_addr->u.q_unix.path);
-        qapi_free_SocketAddress(src_tpm_addr);
-    }
+    g_unlink(src_tpm_addr->u.q_unix.path);
+    qapi_free_SocketAddress(src_tpm_addr);
 }
Signature · 1 line
2.20.1
Reviewed-by on v1 5/6
#12 lore raw

Re: [PATCH 5/6] tests/qtest/tpm-tests: Remove unnecessary NULL checks

Quoted 6 lines · Peter Maydell

Coverity points out that in tpm_test_swtpm_migration_test() we assume that src_tpm_addr and dst_tpm_addr are non-NULL (we pass them to tpm_util_migration_start_qemu() which will unconditionally dereference them) but then later explicitly check them for NULL. Remove the pointless checks.

Quoted 6 lines
Fixes Coverity CID 1432367, 1432359
---
 tests/qtest/tpm-tests.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
Reviewed-by on v1 5/6
#13 lore raw

Re: [PATCH 5/6] tests/qtest/tpm-tests: Remove unnecessary NULL checks

In reply to Peter Maydell
Quoted 9 lines · Peter Maydell

Coverity points out that in tpm_test_swtpm_migration_test() we assume that src_tpm_addr and dst_tpm_addr are non-NULL (we pass them to tpm_util_migration_start_qemu() which will unconditionally dereference them) but then later explicitly check them for NULL. Remove the pointless checks.

Fixes Coverity CID 1432367, 1432359
Quoted 27 lines
---
  tests/qtest/tpm-tests.c | 12 ++++--------
  1 file changed, 4 insertions(+), 8 deletions(-)
tests/qtest/tpm-tests.c +4 -8
index 0da3a8a4df5..25073d1f9e9 100644
@@ -123,14 +123,10 @@ void tpm_test_swtpm_migration_test(const char *src_tpm_path,
      qtest_quit(src_qemu);
 
      tpm_util_swtpm_kill(dst_tpm_pid);
-    if (dst_tpm_addr) {
-        g_unlink(dst_tpm_addr->u.q_unix.path);
-        qapi_free_SocketAddress(dst_tpm_addr);
-    }
+    g_unlink(dst_tpm_addr->u.q_unix.path);
+    qapi_free_SocketAddress(dst_tpm_addr);
 
      tpm_util_swtpm_kill(src_tpm_pid);
-    if (src_tpm_addr) {
-        g_unlink(src_tpm_addr->u.q_unix.path);
-        qapi_free_SocketAddress(src_tpm_addr);
-    }
+    g_unlink(src_tpm_addr->u.q_unix.path);
+    qapi_free_SocketAddress(src_tpm_addr);
  }
#14 lore raw

PATCH 6/6 tests/unit/test-vmstate: Assert that dup() and mkstemp() succeed

In reply to Peter Maydell

Coverity complains that we don't check for failures from dup() and mkstemp(); add asserts that these syscalls succeeded.

Fixes Coverity CID 1432516, 1432574
---
 tests/unit/test-vmstate.c | 2 ++
 1 file changed, 2 insertions(+)
tests/unit/test-vmstate.c +2 -0
index a001879585e..8d46af61511 100644
@@ -44,6 +44,7 @@ static QEMUFile *open_test_file(bool write)
     QIOChannel *ioc;
     QEMUFile *f;
 
+    g_assert(fd >= 0);
     lseek(fd, 0, SEEK_SET);
     if (write) {
         g_assert_cmpint(ftruncate(fd, 0), ==, 0);
@@ -1486,6 +1487,7 @@ int main(int argc, char **argv)
     g_autofree char *temp_file = g_strdup_printf("%s/vmst.test.XXXXXX",
                                                  g_get_tmp_dir());
     temp_fd = mkstemp(temp_file);
+    g_assert(temp_fd >= 0);
 
     module_call_init(MODULE_INIT_QOM);
 
Signature · 1 line
2.20.1
Reviewed-by on v1 6/6
#15 lore raw

Re: [PATCH 6/6] tests/unit/test-vmstate: Assert that dup() and mkstemp() succeed

Quoted 14 lines · Peter Maydell

Coverity complains that we don't check for failures from dup() and mkstemp(); add asserts that these syscalls succeeded.

Fixes Coverity CID 1432516, 1432574
---
 tests/unit/test-vmstate.c | 2 ++
 1 file changed, 2 insertions(+)
tests/unit/test-vmstate.c +0 -0
index a001879585e..8d46af61511 100644

tests/unit/test-vmstate.c static QEMUFile *open_test_file(bool write) · line 46

Quoted 3 lines
tests/unit/test-vmstate.c +0 -0
@@ -44,6 +44,7 @@ static QEMUFile *open_test_file(bool write)
     QIOChannel *ioc;
     QEMUFile *f;
 

Eventually move the assignation before the assertion:

-- >8 --
@@ -40,10 +40,12 @@ static int temp_fd;
/* Duplicate temp_fd and seek to the beginning of the file */ static QEMUFile *open_test_file(bool write)
{
- int fd = dup(temp_fd);

+    int fd;
     QIOChannel *ioc;
     QEMUFile *f;

+ fd = dup(temp_fd);

+    g_assert(fd >= 0);
     lseek(fd, 0, SEEK_SET);
     if (write) {
         g_assert_cmpint(ftruncate(fd, 0), ==, 0);

---

Regardless:

Quoted 13 lines
tests/unit/test-vmstate.c +2 -0
+    g_assert(fd >= 0);
     lseek(fd, 0, SEEK_SET);
     if (write) {
         g_assert_cmpint(ftruncate(fd, 0), ==, 0);
@@ -1486,6 +1487,7 @@ int main(int argc, char **argv)
     g_autofree char *temp_file = g_strdup_printf("%s/vmst.test.XXXXXX",
                                                  g_get_tmp_dir());
     temp_fd = mkstemp(temp_file);
+    g_assert(temp_fd >= 0);
 
     module_call_init(MODULE_INIT_QOM);
 
Reviewed-by on v1 6/6
#16 lore raw

Re: [PATCH 6/6] tests/unit/test-vmstate: Assert that dup() and mkstemp() succeed

In reply to Peter Maydell
Quoted 5 lines · Peter Maydell

Coverity complains that we don't check for failures from dup() and mkstemp(); add asserts that these syscalls succeeded.

Fixes Coverity CID 1432516, 1432574
Quoted 24 lines
---
  tests/unit/test-vmstate.c | 2 ++
  1 file changed, 2 insertions(+)
tests/unit/test-vmstate.c +2 -0
index a001879585e..8d46af61511 100644
@@ -44,6 +44,7 @@ static QEMUFile *open_test_file(bool write)
      QIOChannel *ioc;
      QEMUFile *f;
 
+    g_assert(fd >= 0);
      lseek(fd, 0, SEEK_SET);
      if (write) {
          g_assert_cmpint(ftruncate(fd, 0), ==, 0);
@@ -1486,6 +1487,7 @@ int main(int argc, char **argv)
      g_autofree char *temp_file = g_strdup_printf("%s/vmst.test.XXXXXX",
                                                   g_get_tmp_dir());
      temp_fd = mkstemp(temp_file);
+    g_assert(temp_fd >= 0);
 
      module_call_init(MODULE_INIT_QOM);