[Spice-devel] [spice-common v3 7/7] log: Introduce spice_assert_if_fail

Christophe Fergeau cfergeau at redhat.com
Wed Dec 16 05:27:13 PST 2015


They can be used in spice-server to replace spice_return_if_fail.
Currently spice_return_if_fail aborts in spice-server, and it's not
always clear whether using a non-aborting g_return_if_fail is acceptable
or not. Having a spice_assert_if_fail alternative makes it clearer that
this is not going to return, while having a name distinct from assert()
so that places which needs reviewing can be spotted more easily.

Acked-by: Jonathon Jongsma <jjongsma at redhat.com>
---
 common/log.h         |  3 +++
 tests/test-logging.c | 12 ++++++++++++
 2 files changed, 15 insertions(+)

diff --git a/common/log.h b/common/log.h
index 0e03f59..b83b0e0 100644
--- a/common/log.h
+++ b/common/log.h
@@ -95,6 +95,9 @@ void spice_log(const char *log_domain,
     }                                                   \
 } G_STMT_END
 
+#define spice_assert_val_if_fail(cond, val) spice_assert(cond)
+#define spice_assert_if_fail(cond) spice_assert(cond)
+
 #define spice_assert(x) G_STMT_START {                  \
     if G_LIKELY(x) { } else {                           \
         spice_error("assertion `%s' failed", #x);       \
diff --git a/tests/test-logging.c b/tests/test-logging.c
index cf05016..e56172a 100644
--- a/tests/test-logging.c
+++ b/tests/test-logging.c
@@ -175,6 +175,17 @@ static void test_spice_non_fatal_g_return_if_fail(void)
     g_free(pattern);
 }
 
+/* Checks that spice_assert_if_fail() aborts by default */
+static void test_spice_fatal_assert_if_fail(void)
+{
+    if (g_test_subprocess()) {
+        spice_assert_if_fail(FALSE);
+        return;
+    }
+    g_test_trap_subprocess(NULL, 0, 0);
+    g_test_trap_assert_failed();
+}
+
 /* Checks that spice_*, g_* and other_* (different log domain as g_*) all
  * go through g_log() with the correct domain/log level. This then checks
  * that only logs with level 'message' or higher are shown by default.
@@ -403,6 +414,7 @@ int main(int argc, char **argv)
     g_test_add_func("/spice-common/spice-fatal-critical", test_spice_fatal_critical);
     g_test_add_func("/spice-common/spice-non-fatal-gcritical", test_spice_non_fatal_g_critical);
     g_test_add_func("/spice-common/spice-fatal-return-if-fail", test_spice_fatal_return_if_fail);
+    g_test_add_func("/spice-common/spice-fatal-assert-if-fail", test_spice_fatal_assert_if_fail);
     g_test_add_func("/spice-common/spice-non-fatal-greturn-if-fail", test_spice_non_fatal_g_return_if_fail);
     g_test_add_func("/spice-common/spice-fatal-warning", test_spice_fatal_warning);
 
-- 
2.5.0



More information about the Spice-devel mailing list