tests: Fix some minor Coverity issues
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
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.)
--- tests/qtest/bios-tables-test.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
tests/qtest/bios-tables-test.c +6 -2
@@ -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
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 1432346Signed-off-by Peter Maydell <[email protected]>
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@@ -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); }
PATCH 2/6 tests/qtest/e1000e-test: Check qemu_recv() succeeded
In reply to Peter MaydellThe 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().)
--- tests/qtest/e1000e-test.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tests/qtest/e1000e-test.c +2 -1
@@ -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
Re: [PATCH 2/6] tests/qtest/e1000e-test: Check qemu_recv() succeeded
Quoted 9 lines · Peter Maydell
The
e1000e_send_verify()test callsqemu_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 thang_assert().)Fixes Coverity CID 1432324Signed-off-by Peter Maydell <[email protected]>
Quoted 18 lines
--- tests/qtest/e1000e-test.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)tests/qtest/e1000e-test.c +2
-1@@ -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 */
PATCH 3/6 tests/qtest/hd-geo-test: Fix checks on mkstemp() return value
In reply to Peter MaydellCoverity 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().
--- tests/qtest/hd-geo-test.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tests/qtest/hd-geo-test.c +2 -2
@@ -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
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 increate_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 increate_test_img().Fixes Coverity CID 1432274Signed-off-by Peter Maydell <[email protected]>--- tests/qtest/hd-geo-test.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Re: [PATCH 3/6] tests/qtest/hd-geo-test: Fix checks on mkstemp() return value
In reply to Peter MaydellQuoted 7 lines · Peter Maydell
Coverity notices that the checks against
mkstemp()failing increate_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 increate_test_img().Fixes Coverity CID 1432274Signed-off-by Peter Maydell <[email protected]>
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@@ -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" );
PATCH 4/6 tests/qtest/pflash-cfi02-test: Avoid potential integer overflow
In reply to Peter MaydellCoverity 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.)
--- tests/qtest/pflash-cfi02-test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tests/qtest/pflash-cfi02-test.c +1 -1
@@ -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
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 1432320Signed-off-by Peter Maydell <[email protected]>--- tests/qtest/pflash-cfi02-test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Re: [PATCH 4/6] tests/qtest/pflash-cfi02-test: Avoid potential integer overflow
In reply to Peter MaydellQuoted 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 1432320Signed-off-by Peter Maydell <[email protected]>
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@@ -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)); } }
PATCH 5/6 tests/qtest/tpm-tests: Remove unnecessary NULL checks
In reply to Peter MaydellCoverity 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.
--- tests/qtest/tpm-tests.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
tests/qtest/tpm-tests.c +4 -8
@@ -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
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 thatsrc_tpm_addranddst_tpm_addrare non-NULL (we pass them totpm_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, 1432359Signed-off-by Peter Maydell <[email protected]>--- tests/qtest/tpm-tests.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
Re: [PATCH 5/6] tests/qtest/tpm-tests: Remove unnecessary NULL checks
In reply to Peter MaydellQuoted 9 lines · Peter Maydell
Coverity points out that in
tpm_test_swtpm_migration_test()we assume thatsrc_tpm_addranddst_tpm_addrare non-NULL (we pass them totpm_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, 1432359Signed-off-by Peter Maydell <[email protected]>
Quoted 27 lines
--- tests/qtest/tpm-tests.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)tests/qtest/tpm-tests.c +4
-8@@ -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); }
PATCH 6/6 tests/unit/test-vmstate: Assert that dup() and mkstemp() succeed
In reply to Peter MaydellCoverity complains that we don't check for failures from dup() and mkstemp(); add asserts that these syscalls succeeded.
--- tests/unit/test-vmstate.c | 2 ++ 1 file changed, 2 insertions(+)
tests/unit/test-vmstate.c +2 -0
@@ -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
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()andmkstemp(); add asserts that these syscalls succeeded.Fixes Coverity CID 1432516, 1432574Signed-off-by Peter Maydell <[email protected]>--- tests/unit/test-vmstate.c | 2 ++ 1 file changed, 2 insertions(+)tests/unit/test-vmstate.c +0
-0
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 );
Re: [PATCH 6/6] tests/unit/test-vmstate: Assert that dup() and mkstemp() succeed
In reply to Peter MaydellQuoted 5 lines · Peter Maydell
Coverity complains that we don't check for failures from
dup()andmkstemp(); add asserts that these syscalls succeeded.Fixes Coverity CID 1432516, 1432574Signed-off-by Peter Maydell <[email protected]>
Quoted 24 lines
--- tests/unit/test-vmstate.c | 2 ++ 1 file changed, 2 insertions(+)tests/unit/test-vmstate.c +2
-0@@ -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 );