Skip to contentSkip to conversation

tests: three easy Coverity fixes

PATCH In review Peter Maydell 5 participants 12 messages May 3, 2021 – May 4, 2021

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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.

Fixes Coverity CID 1432379, CID 1432350

---
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
index b70cc32d600..3a40ff3f96c 100644
@@ -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
Reviewed-by on v1 1/3
#2 lore raw

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 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.

Fixes Coverity CID 1432379, CID 1432350

---
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 +0 -0
index b70cc32d600..3a40ff3f96c 100644

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);
  }
Reviewed-by on v1 1/3
#3 lore raw

Re: [PATCH 1/3] tests/qtest/tpm-util.c: Free memory with correct free function

In reply to Peter Maydell
Quoted 18 lines · Peter Maydell

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.

Fixes Coverity CID 1432379, CID 1432350

---
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 +0 -0
index b70cc32d600..3a40ff3f96c 100644

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
#4 lore raw

PATCH 2/3 tests/qtest/rtc-test: Remove pointless NULL check

In reply to Peter Maydell

In 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.

Fixes Coverity CID 1432353
---
 tests/qtest/rtc-test.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tests/qtest/rtc-test.c +2 -4
index 402ce2c6090..8126ab1bdb8 100644
@@ -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
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] tests/qtest/rtc-test: Remove pointless NULL check

Quoted 27 lines · Peter Maydell

In 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.

Fixes Coverity CID 1432353
---
  tests/qtest/rtc-test.c | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
tests/qtest/rtc-test.c +1 -1
index 402ce2c6090..8126ab1bdb8 100644
@@ -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;
  }
Reviewed-by on v1 2/3
#6 lore raw

Re: [PATCH 2/3] tests/qtest/rtc-test: Remove pointless NULL check

In reply to Peter Maydell
Quoted 8 lines · Peter Maydell

In 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.

Fixes Coverity CID 1432353
Signature · 1 line
Alex Bennée
#7 lore raw

PATCH 3/3 tests: Avoid side effects inside g_assert() arguments

In reply to 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 1432291
---
 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
index a42207d416f..8492f02a9c3 100644
@@ -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
index fc0a918c8d1..afc24dd3e46 100644
@@ -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
Reviewed-by on v1 3/3
#8 lore raw

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 1432291
---
 tests/qtest/ipmi-bt-test.c  | 6 ++++--
 tests/qtest/ipmi-kcs-test.c | 3 ++-
 2 files changed, 6 insertions(+), 3 deletions(-)
#9 lore raw

Re: [PATCH 3/3] tests: Avoid side effects inside g_assert() arguments

In reply to Peter Maydell
Quoted 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 1432291
---
  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
index a42207d416f..8492f02a9c3 100644
@@ -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
index fc0a918c8d1..afc24dd3e46 100644

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

#10 lore raw

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 1432291
tests/qtest/ipmi-kcs-test.c +1 -1
index fc0a918c8d1..afc24dd3e46 100644
@@ -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-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?

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

Reviewed-by on v1 3/3
#11 lore raw

Re: [PATCH 3/3] tests: Avoid side effects inside g_assert() arguments

In reply to Peter Maydell
Quoted 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 1432291
---
 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
index a42207d416f..8492f02a9c3 100644

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 -1
         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
index fc0a918c8d1..afc24dd3e46 100644
@@ -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