All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Cc: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
	Greg Kurz <groug@kaod.org>, Paul Durrant <paul@xen.org>
Subject: [PULL 46/53] error: New macro ERRP_GUARD()
Date: Tue,  7 Jul 2020 23:24:56 +0200	[thread overview]
Message-ID: <20200707212503.1495927-47-armbru@redhat.com> (raw)
In-Reply-To: <20200707212503.1495927-1-armbru@redhat.com>

From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>

Introduce a new ERRP_GUARD() macro, to be used at start of functions
with an errp OUT parameter.

It has three goals:

1. Fix issue with error_fatal and error_prepend/error_append_hint: the
user can't see this additional information, because exit() happens in
error_setg earlier than information is added. [Reported by Greg Kurz]

2. Fix issue with error_abort and error_propagate: when we wrap
error_abort by local_err+error_propagate, the resulting coredump will
refer to error_propagate and not to the place where error happened.
(the macro itself doesn't fix the issue, but it allows us to [3.] drop
the local_err+error_propagate pattern, which will definitely fix the
issue) [Reported by Kevin Wolf]

3. Drop local_err+error_propagate pattern, which is used to workaround
void functions with errp parameter, when caller wants to know resulting
status. (Note: actually these functions could be merely updated to
return int error code).

To achieve these goals, later patches will add invocations
of this macro at the start of functions with either use
error_prepend/error_append_hint (solving 1) or which use
local_err+error_propagate to check errors, switching those
functions to use *errp instead (solving 2 and 3).

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: Paul Durrant <paul@xen.org>
Reviewed-by: Greg Kurz <groug@kaod.org>
Reviewed-by: Eric Blake <eblake@redhat.com>
[Merge comments properly with recent commit "error: Document Error API
usage rules", and edit for clarity.  Put ERRP_AUTO_PROPAGATE() before
its helpers, and touch up style.  Tweak commit message.]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20200707165037.1026246-2-armbru@redhat.com>
[Rename ERRP_AUTO_PROPAGATE() to ERRP_GUARD(), tweak commit message
again]
---
 include/qapi/error.h | 158 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 139 insertions(+), 19 deletions(-)

diff --git a/include/qapi/error.h b/include/qapi/error.h
index 2c189abb04..85df875a3a 100644
--- a/include/qapi/error.h
+++ b/include/qapi/error.h
@@ -30,6 +30,10 @@
  *   job.  Since the value of @errp is about handling the error, the
  *   function should not examine it.
  *
+ * - The function may pass @errp to functions it calls to pass on
+ *   their errors to its caller.  If it dereferences @errp to check
+ *   for errors, it must use ERRP_GUARD().
+ *
  * - On success, the function should not touch *errp.  On failure, it
  *   should set a new error, e.g. with error_setg(errp, ...), or
  *   propagate an existing one, e.g. with error_propagate(errp, ...).
@@ -45,15 +49,17 @@
  * = Creating errors =
  *
  * Create an error:
- *     error_setg(&err, "situation normal, all fouled up");
+ *     error_setg(errp, "situation normal, all fouled up");
+ * where @errp points to the location to receive the error.
  *
  * Create an error and add additional explanation:
- *     error_setg(&err, "invalid quark");
- *     error_append_hint(&err, "Valid quarks are up, down, strange, "
+ *     error_setg(errp, "invalid quark");
+ *     error_append_hint(errp, "Valid quarks are up, down, strange, "
  *                       "charm, top, bottom.\n");
+ * This may require use of ERRP_GUARD(); more on that below.
  *
  * Do *not* contract this to
- *     error_setg(&err, "invalid quark\n" // WRONG!
+ *     error_setg(errp, "invalid quark\n" // WRONG!
  *                "Valid quarks are up, down, strange, charm, top, bottom.");
  *
  * = Reporting and destroying errors =
@@ -107,18 +113,6 @@
  * Errors get passed to the caller through the conventional @errp
  * parameter.
  *
- * Pass an existing error to the caller:
- *     error_propagate(errp, err);
- * where Error **errp is a parameter, by convention the last one.
- *
- * Pass an existing error to the caller with the message modified:
- *     error_propagate_prepend(errp, err,
- *                             "Could not frobnicate '%s': ", name);
- * This is more concise than
- *     error_propagate(errp, err); // don't do this
- *     error_prepend(errp, "Could not frobnicate '%s': ", name);
- * and works even when @errp is &error_fatal.
- *
  * Create a new error and pass it to the caller:
  *     error_setg(errp, "situation normal, all fouled up");
  *
@@ -129,18 +123,26 @@
  *         handle the error...
  *     }
  * - when it does not, say because it is a void function:
+ *     ERRP_GUARD();
+ *     foo(arg, errp);
+ *     if (*errp) {
+ *         handle the error...
+ *     }
+ * More on ERRP_GUARD() below.
+ *
+ * Code predating ERRP_GUARD() still exists, and looks like this:
  *     Error *err = NULL;
  *     foo(arg, &err);
  *     if (err) {
  *         handle the error...
- *         error_propagate(errp, err);
+ *         error_propagate(errp, err); // deprecated
  *     }
- * Do *not* "optimize" this to
+ * Avoid in new code.  Do *not* "optimize" it to
  *     foo(arg, errp);
  *     if (*errp) { // WRONG!
  *         handle the error...
  *     }
- * because errp may be NULL!
+ * because errp may be NULL without the ERRP_GUARD() guard.
  *
  * But when all you do with the error is pass it on, please use
  *     foo(arg, errp);
@@ -160,6 +162,19 @@
  *         handle the error...
  *     }
  *
+ * Pass an existing error to the caller:
+ *     error_propagate(errp, err);
+ * This is rarely needed.  When @err is a local variable, use of
+ * ERRP_GUARD() commonly results in more readable code.
+ *
+ * Pass an existing error to the caller with the message modified:
+ *     error_propagate_prepend(errp, err,
+ *                             "Could not frobnicate '%s': ", name);
+ * This is more concise than
+ *     error_propagate(errp, err); // don't do this
+ *     error_prepend(errp, "Could not frobnicate '%s': ", name);
+ * and works even when @errp is &error_fatal.
+ *
  * Receive and accumulate multiple errors (first one wins):
  *     Error *err = NULL, *local_err = NULL;
  *     foo(arg, &err);
@@ -187,6 +202,69 @@
  *         error_setg(&err, ...); // WRONG!
  *     }
  * because this may pass a non-null err to error_setg().
+ *
+ * = Why, when and how to use ERRP_GUARD() =
+ *
+ * Without ERRP_GUARD(), use of the @errp parameter is restricted:
+ * - It must not be dereferenced, because it may be null.
+ * - It should not be passed to error_prepend() or
+ *   error_append_hint(), because that doesn't work with &error_fatal.
+ * ERRP_GUARD() lifts these restrictions.
+ *
+ * To use ERRP_GUARD(), add it right at the beginning of the function.
+ * @errp can then be used without worrying about the argument being
+ * NULL or &error_fatal.
+ *
+ * Using it when it's not needed is safe, but please avoid cluttering
+ * the source with useless code.
+ *
+ * = Converting to ERRP_GUARD() =
+ *
+ * To convert a function to use ERRP_GUARD():
+ *
+ * 0. If the Error ** parameter is not named @errp, rename it to
+ *    @errp.
+ *
+ * 1. Add an ERRP_GUARD() invocation, by convention right at the
+ *    beginning of the function.  This makes @errp safe to use.
+ *
+ * 2. Replace &err by errp, and err by *errp.  Delete local variable
+ *    @err.
+ *
+ * 3. Delete error_propagate(errp, *errp), replace
+ *    error_propagate_prepend(errp, *errp, ...) by error_prepend(errp, ...)
+ *
+ * 4. Ensure @errp is valid at return: when you destroy *errp, set
+ *    errp = NULL.
+ *
+ * Example:
+ *
+ *     bool fn(..., Error **errp)
+ *     {
+ *         Error *err = NULL;
+ *
+ *         foo(arg, &err);
+ *         if (err) {
+ *             handle the error...
+ *             error_propagate(errp, err);
+ *             return false;
+ *         }
+ *         ...
+ *     }
+ *
+ * becomes
+ *
+ *     bool fn(..., Error **errp)
+ *     {
+ *         ERRP_GUARD();
+ *
+ *         foo(arg, errp);
+ *         if (*errp) {
+ *             handle the error...
+ *             return false;
+ *         }
+ *         ...
+ *     }
  */
 
 #ifndef ERROR_H
@@ -287,6 +365,7 @@ void error_setg_win32_internal(Error **errp,
  * the error object.
  * Else, move the error object from @local_err to *@dst_errp.
  * On return, @local_err is invalid.
+ * Please use ERRP_GUARD() instead when possible.
  * Please don't error_propagate(&error_fatal, ...), use
  * error_report_err() and exit(), because that's more obvious.
  */
@@ -298,6 +377,7 @@ void error_propagate(Error **dst_errp, Error *local_err);
  * Behaves like
  *     error_prepend(&local_err, fmt, ...);
  *     error_propagate(dst_errp, local_err);
+ * Please use ERRP_GUARD() and error_prepend() instead when possible.
  */
 void error_propagate_prepend(Error **dst_errp, Error *local_err,
                              const char *fmt, ...);
@@ -395,6 +475,46 @@ void error_set_internal(Error **errp,
                         ErrorClass err_class, const char *fmt, ...)
     GCC_FMT_ATTR(6, 7);
 
+/*
+ * Make @errp parameter easier to use regardless of argument value
+ *
+ * This macro is for use right at the beginning of a function that
+ * takes an Error **errp parameter to pass errors to its caller.  The
+ * parameter must be named @errp.
+ *
+ * It must be used when the function dereferences @errp or passes
+ * @errp to error_prepend(), error_vprepend(), or error_append_hint().
+ * It is safe to use even when it's not needed, but please avoid
+ * cluttering the source with useless code.
+ *
+ * If @errp is NULL or &error_fatal, rewrite it to point to a local
+ * Error variable, which will be automatically propagated to the
+ * original @errp on function exit.
+ *
+ * Note: &error_abort is not rewritten, because that would move the
+ * abort from the place where the error is created to the place where
+ * it's propagated.
+ */
+#define ERRP_GUARD()                                            \
+    g_auto(ErrorPropagator) _auto_errp_prop = {.errp = errp};   \
+    do {                                                        \
+        if (!errp || errp == &error_fatal) {                    \
+            errp = &_auto_errp_prop.local_err;                  \
+        }                                                       \
+    } while (0)
+
+typedef struct ErrorPropagator {
+    Error *local_err;
+    Error **errp;
+} ErrorPropagator;
+
+static inline void error_propagator_cleanup(ErrorPropagator *prop)
+{
+    error_propagate(prop->errp, prop->local_err);
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
+
 /*
  * Special error destination to abort on error.
  * See error_setg() and error_propagate() for details.
-- 
2.26.2



  parent reply	other threads:[~2020-07-08 21:40 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-07 21:24 [PULL 00/53] Error reporting patches patches for 2020-07-07 Markus Armbruster
2020-07-07 21:24 ` [PULL 01/53] error: Fix examples in error.h's big comment Markus Armbruster
2020-07-07 21:24 ` [PULL 02/53] error: Improve " Markus Armbruster
2020-07-07 21:24 ` [PULL 03/53] error: Document Error API usage rules Markus Armbruster
2020-07-07 21:24 ` [PULL 04/53] qdev: Use returned bool to check for qdev_realize() etc. failure Markus Armbruster
2020-07-07 21:24 ` [PULL 05/53] macio: Tidy up error handling in macio_newworld_realize() Markus Armbruster
2020-07-07 21:24 ` [PULL 06/53] virtio-crypto-pci: Tidy up virtio_crypto_pci_realize() Markus Armbruster
2020-07-07 21:24 ` [PULL 07/53] qemu-option: Check return value instead of @err where convenient Markus Armbruster
2020-07-07 21:24 ` [PULL 08/53] qemu-option: Make uses of find_desc_by_name() more similar Markus Armbruster
2020-07-07 21:24 ` [PULL 09/53] qemu-option: Factor out helper find_default_by_name() Markus Armbruster
2020-07-07 21:24 ` [PULL 10/53] qemu-option: Simplify around find_default_by_name() Markus Armbruster
2020-07-07 21:24 ` [PULL 11/53] qemu-option: Factor out helper opt_create() Markus Armbruster
2020-07-07 21:24 ` [PULL 12/53] qemu-option: Replace opt_set() by cleaner opt_validate() Markus Armbruster
2020-07-07 21:24 ` [PULL 13/53] qemu-option: Make functions taking Error ** return bool, not void Markus Armbruster
2020-07-07 21:24 ` [PULL 14/53] qemu-option: Use returned bool to check for failure Markus Armbruster
2020-07-07 21:24 ` [PULL 15/53] block: Avoid error accumulation in bdrv_img_create() Markus Armbruster
2020-07-07 21:24 ` [PULL 16/53] hmp: Eliminate a variable in hmp_migrate_set_parameter() Markus Armbruster
2020-07-07 21:24 ` [PULL 17/53] qapi: Make visitor functions taking Error ** return bool, not void Markus Armbruster
2020-07-07 21:24 ` [PULL 18/53] qapi: Use returned bool to check for failure, Coccinelle part Markus Armbruster
2020-07-07 21:24 ` [PULL 19/53] qapi: Use returned bool to check for failure, manual part Markus Armbruster
2020-07-07 21:24 ` [PULL 20/53] s390x/pci: Fix harmless mistake in zpci's property fid's setter Markus Armbruster
2020-07-07 21:24 ` [PULL 21/53] qom: Use error_reportf_err() instead of g_printerr() in examples Markus Armbruster
2020-07-07 21:24 ` [PULL 22/53] qom: Rename qdev_get_type() to object_get_type() Markus Armbruster
2020-07-07 21:24 ` [PULL 23/53] qom: Crash more nicely on object_property_get_link() failure Markus Armbruster
2020-07-07 21:24 ` [PULL 24/53] qom: Don't handle impossible " Markus Armbruster
2020-07-07 21:24 ` [PULL 25/53] qom: Use return values to check for error where that's simpler Markus Armbruster
2020-07-07 21:24 ` [PULL 26/53] qom: Put name parameter before value / visitor parameter Markus Armbruster
2020-07-07 21:24 ` [PULL 27/53] qom: Make functions taking Error ** return bool, not void Markus Armbruster
2020-07-07 21:24 ` [PULL 28/53] qom: Use returned bool to check for failure, Coccinelle part Markus Armbruster
2020-07-07 21:24 ` [PULL 29/53] qom: Use returned bool to check for failure, manual part Markus Armbruster
2020-07-07 21:24 ` [PULL 30/53] qom: Make functions taking Error ** return bool, not 0/-1 Markus Armbruster
2020-07-07 21:24 ` [PULL 31/53] qdev: Make functions taking Error ** return bool, not void Markus Armbruster
2020-07-07 21:24 ` [PULL 32/53] qdev: Use returned bool to check for failure, Coccinelle part Markus Armbruster
2020-07-07 21:24 ` [PULL 33/53] error: Avoid unnecessary error_propagate() after error_setg() Markus Armbruster
2020-07-07 21:24 ` [PULL 34/53] error: Eliminate error_propagate() with Coccinelle, part 1 Markus Armbruster
2020-07-07 21:24 ` [PULL 35/53] error: Eliminate error_propagate() with Coccinelle, part 2 Markus Armbruster
2020-07-07 21:24 ` [PULL 36/53] error: Eliminate error_propagate() manually Markus Armbruster
2020-07-07 21:24 ` [PULL 37/53] error: Reduce unnecessary error propagation Markus Armbruster
2020-07-07 21:24 ` [PULL 38/53] block/parallels: Simplify parallels_open() after previous commit Markus Armbruster
2020-07-07 21:24 ` [PULL 39/53] qapi: Smooth another visitor error checking pattern Markus Armbruster
2020-07-07 21:24 ` [PULL 40/53] qapi: Smooth visitor error checking in generated code Markus Armbruster
2020-07-07 21:24 ` [PULL 41/53] qapi: Purge error_propagate() from QAPI core Markus Armbruster
2020-07-07 21:24 ` [PULL 42/53] error: Avoid error_propagate() after migrate_add_blocker() Markus Armbruster
2020-07-07 21:24 ` [PULL 43/53] qemu-img: Ignore Error objects where the return value suffices Markus Armbruster
2020-07-07 21:24 ` [PULL 44/53] qdev: " Markus Armbruster
2020-07-07 21:24 ` [PULL 45/53] hmp: " Markus Armbruster
2020-07-07 21:24 ` Markus Armbruster [this message]
2020-07-07 21:24 ` [PULL 47/53] scripts: Coccinelle script to use ERRP_GUARD() Markus Armbruster
2021-03-11 19:21   ` Philippe Mathieu-Daudé
2021-03-12  8:36     ` Markus Armbruster
2021-03-12 10:17       ` Philippe Mathieu-Daudé
2020-07-07 21:24 ` [PULL 48/53] sd: Use ERRP_GUARD() Markus Armbruster
2020-07-07 21:24 ` [PULL 49/53] pflash: " Markus Armbruster
2020-07-07 21:25 ` [PULL 50/53] fw_cfg: " Markus Armbruster
2020-07-07 21:25 ` [PULL 51/53] virtio-9p: " Markus Armbruster
2020-07-07 21:25 ` [PULL 52/53] nbd: " Markus Armbruster
2020-07-07 21:25 ` [PULL 53/53] xen: " Markus Armbruster
2020-07-10 12:47 ` [PULL 00/53] Error reporting patches patches for 2020-07-07 Markus Armbruster

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200707212503.1495927-47-armbru@redhat.com \
    --to=armbru@redhat.com \
    --cc=groug@kaod.org \
    --cc=paul@xen.org \
    --cc=qemu-devel@nongnu.org \
    --cc=vsementsov@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.