tests: three easy Coverity fixes
cover tests: three easy Coverity fixes
We recently started running Coverity on our tests/ code, which has revealed a pile of issues that we were previously blissfully unaware of. This series fixes three easy ones:
* use of free() where g_free() was meant
* unnecessary NULL check
* side effect inside g_assert() argument
thanks
-- PMM
Peter Maydell (3):
tests/qtest/tpm-util.c: Free memory with correct free function tests/qtest/rtc-test: Remove pointless NULL check
tests: Avoid side effects inside g_assert() arguments
tests/qtest/ipmi-bt-test.c | 6 ++++-- tests/qtest/ipmi-kcs-test.c | 3 ++- tests/qtest/rtc-test.c | 6 ++---- tests/qtest/tpm-util.c | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-)
Signature · 1 line
2.20.1
PATCH 1/3 tests/qtest/tpm-util.c: Free memory with correct free function
tpm_util_migration_start_qemu() allocates memory with g_strdup_printf() but frees it with free() rather than g_free(), which provokes Coverity complaints (CID 1432379, 1432350). Use the correct free function.
---
With newer glib (2.46 and up) g_free() is guaranteed to be the same as free(), but matching things up is neater anyway.
--- tests/qtest/tpm-util.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tests/qtest/tpm-util.c +2 -2
@@ -289,6 +289,6 @@ void tpm_util_migration_start_qemu(QTestState **src_qemu, *dst_qemu =qtest_init (dst_qemu_args); -free (src_qemu_args); -free (dst_qemu_args); +g_free (src_qemu_args); +g_free (dst_qemu_args); }
Signature · 1 line
2.20.1
Re: [PATCH 1/3] tests/qtest/tpm-util.c: Free memory with correct free function
Quoted 18 lines · Peter Maydell
tpm_util_migration_start_qemu()allocates memory withg_strdup_printf()but frees it withfree()rather thang_free(), which provokes Coverity complaints (CID 1432379, 1432350). Use the correct free function.Fixes Coverity CID 1432379, CID 1432350Signed-off-by Peter Maydell <[email protected]>---
With newer glib (2.46 and up)g_free()is guaranteed to be the same asfree(), but matching things up is neater anyway.--- tests/qtest/tpm-util.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tests/qtest/tpm-util.c +0
-0
tests/qtest/tpm-util.c void tpm_util_migration_start_qemu(QTestState **src_qemu, · line 294
Quoted 8 lines
tests/qtest/tpm-util.c +2
-2@@ -289,6 +289,6 @@ void tpm_util_migration_start_qemu(QTestState **src_qemu, *dst_qemu =qtest_init (dst_qemu_args); -free (src_qemu_args); -free (dst_qemu_args); +g_free (src_qemu_args); +g_free (dst_qemu_args); }
Re: [PATCH 1/3] tests/qtest/tpm-util.c: Free memory with correct free function
In reply to Peter MaydellQuoted 18 lines · Peter Maydell
tpm_util_migration_start_qemu()allocates memory withg_strdup_printf()but frees it withfree()rather thang_free(), which provokes Coverity complaints (CID 1432379, 1432350). Use the correct free function.Fixes Coverity CID 1432379, CID 1432350Signed-off-by Peter Maydell <[email protected]>---
With newer glib (2.46 and up)g_free()is guaranteed to be the same asfree(), but matching things up is neater anyway.--- tests/qtest/tpm-util.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tests/qtest/tpm-util.c +0
-0
tests/qtest/tpm-util.c void tpm_util_migration_start_qemu(QTestState **src_qemu, · line 294
Quoted 8 lines
tests/qtest/tpm-util.c +2
-2@@ -289,6 +289,6 @@ void tpm_util_migration_start_qemu(QTestState **src_qemu, *dst_qemu =qtest_init (dst_qemu_args); -free (src_qemu_args); -free (dst_qemu_args); +g_free (src_qemu_args); +g_free (dst_qemu_args); }
The more modern approach would be to use g_autofree but this works given we aren't dealing with multiple exits:
Signature · 1 line
Alex Bennée
PATCH 2/3 tests/qtest/rtc-test: Remove pointless NULL check
In reply to Peter MaydellIn rtc-test.c we know that s is non-NULL because qtest_start() will return a non-NULL value, and we assume this when we pass s to qtest_irq_intercept_in(). So we can drop the initial assignment of NULL and the "if (s)" condition at the end of the function.
--- tests/qtest/rtc-test.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
tests/qtest/rtc-test.c +2 -4
@@ -686,7 +686,7 @@ static void periodic_timer(void)int main (int argc,char **argv) { -QTestState *s =NULL ; +QTestState *s;int ret;g_test_init (&argc, &argv,NULL );
@@ -712,9 +712,7 @@ int main(int argc, char **argv) ret =g_test_run (); -if (s) { -qtest_quit (s); - } +qtest_quit (s);return ret; }
Signature · 1 line
2.20.1
Re: [PATCH 2/3] tests/qtest/rtc-test: Remove pointless NULL check
Quoted 27 lines · Peter Maydell
In
rtc-test.cwe know that s is non-NULL becauseqtest_start()will return a non-NULL value, and we assume this when we pass s toqtest_irq_intercept_in(). So we can drop the initial assignment of NULL and the "if (s)" condition at the end of the function.Fixes Coverity CID 1432353Signed-off-by Peter Maydell <[email protected]>--- tests/qtest/rtc-test.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)tests/qtest/rtc-test.c +1
-1@@ -686,7 +686,7 @@ static void periodic_timer(void)int main (int argc,char **argv) { -QTestState *s =NULL ; +QTestState *s;int ret;g_test_init (&argc, &argv,NULL );
tests/qtest/rtc-test.c int main(int argc, char **argv) · line 718
Quoted 10 lines
tests/qtest/rtc-test.c +1
-3@@ -712,9 +712,7 @@ int main(int argc, char **argv) ret =g_test_run (); -if (s) { -qtest_quit (s); - } +qtest_quit (s);return ret; }
Re: [PATCH 2/3] tests/qtest/rtc-test: Remove pointless NULL check
In reply to Peter MaydellQuoted 8 lines · Peter Maydell
In
rtc-test.cwe know that s is non-NULL becauseqtest_start()will return a non-NULL value, and we assume this when we pass s toqtest_irq_intercept_in(). So we can drop the initial assignment of NULL and the "if (s)" condition at the end of the function.Fixes Coverity CID 1432353Signed-off-by Peter Maydell <[email protected]>
Signature · 1 line
Alex Bennée
PATCH 3/3 tests: Avoid side effects inside g_assert() arguments
In reply to Peter MaydellFor us, assertions are always enabled, but side-effect expressions inside the argument to g_assert() are bad style anyway. Fix three occurrences in IPMI related tests, which will silence some Coverity nits.
--- tests/qtest/ipmi-bt-test.c | 6 ++++-- tests/qtest/ipmi-kcs-test.c | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-)
tests/qtest/ipmi-bt-test.c +4 -2
@@ -98,7 +98,8 @@ static void bt_wait_b_busy(void) {unsigned int count =1000 ;while (IPMI_BT_CTLREG_GET_B_BUSY () !=0 ) { -g_assert (--count !=0 ); + --count; +g_assert (count !=0 );usleep (100 ); } }
@@ -107,7 +108,8 @@ static void bt_wait_b2h_atn(void) {unsigned int count =1000 ;while (IPMI_BT_CTLREG_GET_B2H_ATN () ==0 ) { -g_assert (--count !=0 ); + --count; +g_assert (count !=0 );usleep (100 ); } }
tests/qtest/ipmi-kcs-test.c +2 -1
@@ -73,7 +73,8 @@ static void kcs_wait_ibf(void) {unsigned int count =1000 ;while (IPMI_KCS_CMDREG_GET_IBF () !=0 ) { -g_assert (--count !=0 ); + --count; +g_assert (count !=0 ); } }
Signature · 1 line
2.20.1
Re: [PATCH 3/3] tests: Avoid side effects inside g_assert() arguments
Quoted 11 lines · Peter Maydell
For us, assertions are always enabled, but side-effect expressions inside the argument to
g_assert()are bad style anyway. Fix three occurrences in IPMI related tests, which will silence some Coverity nits.Fixes CID 1432322, CID 1432287, CID 1432291Signed-off-by Peter Maydell <[email protected]>--- tests/qtest/ipmi-bt-test.c | 6 ++++-- tests/qtest/ipmi-kcs-test.c | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-)
Re: [PATCH 3/3] tests: Avoid side effects inside g_assert() arguments
In reply to Peter MaydellQuoted 41 lines · Peter Maydell
For us, assertions are always enabled, but side-effect expressions inside the argument to
g_assert()are bad style anyway. Fix three occurrences in IPMI related tests, which will silence some Coverity nits.Fixes CID 1432322, CID 1432287, CID 1432291Signed-off-by Peter Maydell <[email protected]>--- tests/qtest/ipmi-bt-test.c | 6 ++++-- tests/qtest/ipmi-kcs-test.c | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-)tests/qtest/ipmi-bt-test.c +4
-2@@ -98,7 +98,8 @@ static void bt_wait_b_busy(void) {unsigned int count =1000 ;while (IPMI_BT_CTLREG_GET_B_BUSY () !=0 ) { -g_assert (--count !=0 ); + --count; +g_assert (count !=0 );usleep (100 ); } }@@ -107,7 +108,8 @@ static void bt_wait_b2h_atn(void) {unsigned int count =1000 ;while (IPMI_BT_CTLREG_GET_B2H_ATN () ==0 ) { -g_assert (--count !=0 ); + --count; +g_assert (count !=0 );usleep (100 ); } }tests/qtest/ipmi-kcs-test.c +0
-0
tests/qtest/ipmi-kcs-test.c static void kcs_wait_ibf(void) · line 79
Quoted 8 lines
tests/qtest/ipmi-kcs-test.c +2
-1@@ -73,7 +73,8 @@ static void kcs_wait_ibf(void) {unsigned int count =1000 ;while (IPMI_KCS_CMDREG_GET_IBF () !=0 ) { -g_assert (--count !=0 ); + --count; +g_assert (count !=0 ); } }
According to https://developer.gnome.org/glib/unstable/glib-Testing.html#g-assert g_assert() should be avoided in unit tests and g_assert_true() should be used instead. So I think it might be nicer to use g_assert_true() here?
Thomas
Re: [PATCH 3/3] tests: Avoid side effects inside g_assert() arguments
Quoted 20 lines · Thomas Huth
Quoted 18 lines · Peter Maydell
For us, assertions are always enabled, but side-effect expressions inside the argument to
g_assert()are bad style anyway. Fix three occurrences in IPMI related tests, which will silence some Coverity nits.Fixes CID 1432322, CID 1432287, CID 1432291Signed-off-by Peter Maydell <[email protected]>tests/qtest/ipmi-kcs-test.c +1
-1@@ -73,7 +73,8 @@ static void kcs_wait_ibf(void) {unsigned int count =1000 ;while (IPMI_KCS_CMDREG_GET_IBF () !=0 ) { -g_assert (--count !=0 ); + --count;
Quoted 8 lines
tests/qtest/ipmi-kcs-test.c static void kcs_wait_ibf(void) · line 79
Quoted 3 lines
tests/qtest/ipmi-kcs-test.c +1
-0+g_assert (count !=0 ); } }According to
https://developer.gnome.org/glib/unstable/glib-Testing.html#g-assertg_assert()should be avoided in unit tests andg_assert_true()should be used instead. So I think it might be nicer to useg_assert_true()here?
That probably depends on what we decide about whether we want to use glib-testing-style "assert but this might not stop execution" in our
tests: see this thread:
https://lore.kernel.org/qemu-devel/CAFEAcA9juOChqrh5orybJQwpQsyEZ5z3Dvmy7fjX0DW4Nbgmrg@mail.gmail.com/
(I should have cc'd you and Laurent on that as qtest maintainers; sorry.)
-- PMM
Re: [PATCH 3/3] tests: Avoid side effects inside g_assert() arguments
In reply to Peter MaydellQuoted 17 lines · Peter Maydell
For us, assertions are always enabled, but side-effect expressions inside the argument to
g_assert()are bad style anyway. Fix three occurrences in IPMI related tests, which will silence some Coverity nits.Fixes CID 1432322, CID 1432287, CID 1432291Signed-off-by Peter Maydell <[email protected]>--- tests/qtest/ipmi-bt-test.c | 6 ++++-- tests/qtest/ipmi-kcs-test.c | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-)tests/qtest/ipmi-bt-test.c +0
-0
tests/qtest/ipmi-bt-test.c static void bt_wait_b_busy(void) · line 102
Quoted 6 lines
tests/qtest/ipmi-bt-test.c +2
-1@@ -98,7 +98,8 @@ static void bt_wait_b_busy(void) {unsigned int count =1000 ;while (IPMI_BT_CTLREG_GET_B_BUSY () !=0 ) { -g_assert (--count !=0 ); + --count; +g_assert (count !=0 );
This does seem a little weird - we are not asserting an interface violation just that the read should have cleared in 1000 * 100 usec. If it doesn't is that really a theoretically impossible situation or just an example of a failed test.
That said looking at how deeply buried in the test these helpers are an assert is probably better than a convoluted attempt to return out and exit the test with a failure.
Quoted 26 lines
tests/qtest/ipmi-bt-test.c +2
-1usleep (100 ); } }@@ -107,7 +108,8 @@ static void bt_wait_b2h_atn(void) {unsigned int count =1000 ;while (IPMI_BT_CTLREG_GET_B2H_ATN () ==0 ) { -g_assert (--count !=0 ); + --count; +g_assert (count !=0 );usleep (100 ); } }tests/qtest/ipmi-kcs-test.c +2
-1@@ -73,7 +73,8 @@ static void kcs_wait_ibf(void) {unsigned int count =1000 ;while (IPMI_KCS_CMDREG_GET_IBF () !=0 ) { -g_assert (--count !=0 ); + --count; +g_assert (count !=0 ); } }
Signature · 1 line
Alex Bennée