qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH] fixup! qapi: Simplify error cleanup in test-qmp-*


From: Eric Blake
Subject: [Qemu-devel] [PATCH] fixup! qapi: Simplify error cleanup in test-qmp-*
Date: Fri, 6 Nov 2015 10:04:35 -0700

[As part of the fixup, replace the old commit message with this]

We have several tests that perform multiple sub-actions that are
expected to fail.  Asserting that an error occurred, then clearing
it up to prepare for the next action, turned into enough
boilerplate that it was sometimes forgotten (for example, a number
of tests added to test-qmp-input-visitor.c in d88f5fd leaked err).
Worse, if an error is not reset to NULL, we risk invalidating
later use of that error (passing a non-NULL err into a function
is generally a bad idea).  Encapsulate the boilerplate into a
single helper function error_free_or_abort(), and consistently
use it.

The new function is added into error.c for use everywhere,
although it is anticipated that testsuites will be the main
client.

Signed-off-by: Eric Blake <address@hidden>

---
Drop test-qmp-common.h, and move to error.h instead
---
 include/qapi/error.h           |  9 +++++++++
 tests/test-qmp-commands.c      |  5 ++---
 tests/test-qmp-common.h        | 22 ----------------------
 tests/test-qmp-event.c         |  1 -
 tests/test-qmp-input-strict.c  |  1 -
 tests/test-qmp-input-visitor.c |  1 -
 util/error.c                   |  7 +++++++
 7 files changed, 18 insertions(+), 28 deletions(-)
 delete mode 100644 tests/test-qmp-common.h

diff --git a/include/qapi/error.h b/include/qapi/error.h
index c69dddb..faad466 100644
--- a/include/qapi/error.h
+++ b/include/qapi/error.h
@@ -30,6 +30,10 @@
  * Handle an error without reporting it (just for completeness):
  *     error_free(err);
  *
+ * Assert than an expected error occurred, but clean it up without
+ * reporting it (primarily useful in testsuites):
+ *     error_free_or_abort(&err);
+ *
  * Pass an existing error to the caller:
  *     error_propagate(errp, err);
  * where Error **errp is a parameter, by convention the last one.
@@ -190,6 +194,11 @@ Error *error_copy(const Error *err);
 void error_free(Error *err);

 /*
+ * Convenience function to assert that address@hidden is set, then silently 
free it.
+ */
+void error_free_or_abort(Error **errp);
+
+/*
  * Convenience function to error_report() and free @err.
  */
 void error_report_err(Error *);
diff --git a/tests/test-qmp-commands.c b/tests/test-qmp-commands.c
index 9f65fc2..888fb5f 100644
--- a/tests/test-qmp-commands.c
+++ b/tests/test-qmp-commands.c
@@ -1,13 +1,12 @@
 #include <glib.h>
 #include "qemu-common.h"
-#include "test-qmp-common.h"
 #include "qapi/qmp/types.h"
 #include "test-qmp-commands.h"
 #include "qapi/qmp/dispatch.h"
 #include "qemu/module.h"
 #include "qapi/qmp-input-visitor.h"
-#include "test-qapi-types.h"
-#include "test-qapi-visit.h"
+#include "tests/test-qapi-types.h"
+#include "tests/test-qapi-visit.h"

 void qmp_user_def_cmd(Error **errp)
 {
diff --git a/tests/test-qmp-common.h b/tests/test-qmp-common.h
deleted file mode 100644
index 043f49c..0000000
--- a/tests/test-qmp-common.h
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- * Code common to qmp/qapi unit-tests.
- *
- * Copyright (C) 2015 Red Hat, Inc.
- *
- * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
- * See the COPYING.LIB file in the top-level directory.
- *
- */
-
-#ifndef TEST_QMP_COMMON_H__
-#define TEST_QMP_COMMON_H__
-
-/* Expect an error, abort() if there is none. */
-static inline void error_free_or_abort(Error **errp)
-{
-    g_assert(*errp);
-    error_free(*errp);
-    *errp = NULL;
-}
-
-#endif
diff --git a/tests/test-qmp-event.c b/tests/test-qmp-event.c
index c0ac3ea..035c65c 100644
--- a/tests/test-qmp-event.c
+++ b/tests/test-qmp-event.c
@@ -22,7 +22,6 @@
 #include "qapi/qmp/qint.h"
 #include "qapi/qmp/qobject.h"
 #include "qapi/qmp-event.h"
-#include "test-qmp-common.h"

 typedef struct TestEventData {
     QDict *expect;
diff --git a/tests/test-qmp-input-strict.c b/tests/test-qmp-input-strict.c
index 7e15b09..201221c 100644
--- a/tests/test-qmp-input-strict.c
+++ b/tests/test-qmp-input-strict.c
@@ -15,7 +15,6 @@
 #include <stdarg.h>

 #include "qemu-common.h"
-#include "test-qmp-common.h"
 #include "qapi/qmp-input-visitor.h"
 #include "test-qapi-types.h"
 #include "test-qapi-visit.h"
diff --git a/tests/test-qmp-input-visitor.c b/tests/test-qmp-input-visitor.c
index 2b59b6f..ff4e7c9 100644
--- a/tests/test-qmp-input-visitor.c
+++ b/tests/test-qmp-input-visitor.c
@@ -14,7 +14,6 @@
 #include <stdarg.h>

 #include "qemu-common.h"
-#include "test-qmp-common.h"
 #include "qapi/qmp-input-visitor.h"
 #include "test-qapi-types.h"
 #include "test-qapi-visit.h"
diff --git a/util/error.c b/util/error.c
index 8b86490..80c89a2 100644
--- a/util/error.c
+++ b/util/error.c
@@ -220,6 +220,13 @@ void error_free(Error *err)
     }
 }

+void error_free_or_abort(Error **errp)
+{
+    assert(errp && *errp);
+    error_free(*errp);
+    *errp = NULL;
+}
+
 void error_propagate(Error **dst_errp, Error *local_err)
 {
     if (!local_err) {
-- 
2.4.3




reply via email to

[Prev in Thread] Current Thread [Next in Thread]