From nobody Wed Nov 5 15:55:28 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1497373355522222.8346157817366; Tue, 13 Jun 2017 10:02:35 -0700 (PDT) Received: from localhost ([::1]:44342 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dKpD8-0003Z3-3G for importer@patchew.org; Tue, 13 Jun 2017 13:02:34 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50914) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dKp4Z-0004TA-Ta for qemu-devel@nongnu.org; Tue, 13 Jun 2017 12:53:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dKp4Y-0000TD-Vr for qemu-devel@nongnu.org; Tue, 13 Jun 2017 12:53:43 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40538) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dKp4Y-0000Sm-Ml for qemu-devel@nongnu.org; Tue, 13 Jun 2017 12:53:42 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A0CDC61E64; Tue, 13 Jun 2017 16:53:41 +0000 (UTC) Received: from localhost (ovpn-116-27.gru2.redhat.com [10.97.116.27]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2EDF6978CF; Tue, 13 Jun 2017 16:53:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com A0CDC61E64 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=ehabkost@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com A0CDC61E64 From: Eduardo Habkost To: qemu-devel@nongnu.org Date: Tue, 13 Jun 2017 13:53:10 -0300 Message-Id: <20170613165313.20954-13-ehabkost@redhat.com> In-Reply-To: <20170613165313.20954-1-ehabkost@redhat.com> References: <20170613165313.20954-1-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Tue, 13 Jun 2017 16:53:41 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC 12/15] error: Make IGNORED_ERRORS not a NULL pointer X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Markus Armbruster , Michael Roth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Use a trick to make we have a non-NULL errp if IGNORED_ERRORS is used: instead of passing NULL, pass an existing pointer that points to a special &error_ignored_unset value. This will let the error API to flag error state on *errp but won't require error_free() to be called later. This will allow us to simplify the documented method to propagate errors from: Error *err =3D NULL; foo(arg, &err); if (err) { handle the error... error_propagate(errp, err); } to: foo(arg, errp); if (ERR_IS_SET(errp)) { handle the error... } This will allow us to stop using local_err variables and error_propagate() on hundreds of cases. * TODO: API documentation needs to be updated. * TODO: probably we should add assert(errp) lines to the ERR_IS_* macros, as err_set*() will now break if errp is NULL. Signed-off-by: Eduardo Habkost --- include/qapi/error.h | 20 +++++++++++++++++--- tests/test-qapi-util.c | 14 +++++++------- util/error.c | 13 ++++++++++++- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/include/qapi/error.h b/include/qapi/error.h index 1000853051..d1378396dd 100644 --- a/include/qapi/error.h +++ b/include/qapi/error.h @@ -117,10 +117,24 @@ =20 #include "qapi-types.h" =20 -#define IGNORE_ERRORS (NULL) =20 -#define ERR_IS_SET(e) (!!*(e)) -#define ERR_IS_IGNORED(e) (!(e)) +/* + * Special error value to indicate errors will be ignored, and no + * error was set. + */ +extern Error ignored_error_unset; + +/* + * Special error destination to indicate errors will be ignored, + * but an error value was set. + */ +extern Error ignored_error_set; + + +#define IGNORE_ERRORS (& (Error *) { &ignored_error_unset }) + +#define ERR_IS_SET(e) (*(e) && *(e) !=3D &ignored_error_unset) +#define ERR_IS_IGNORED(e) (!(e) || *(e) =3D=3D &ignored_error_unset || *(e= ) =3D=3D &ignored_error_set) =20 /* * Overall category of an error. diff --git a/tests/test-qapi-util.c b/tests/test-qapi-util.c index fe232ef9ed..0305b9201b 100644 --- a/tests/test-qapi-util.c +++ b/tests/test-qapi-util.c @@ -77,25 +77,25 @@ static void test_parse_qapi_name(void) =20 static void successfn(Error **errp) { - g_assert(ERR_IS_IGNORED(errp) || !ERR_IS_SET(errp)); + g_assert(!ERR_IS_SET(errp)); } =20 static void fail1(Error **errp) { - g_assert(ERR_IS_IGNORED(errp) || !ERR_IS_SET(errp)); + g_assert(!ERR_IS_SET(errp)); =20 error_setg(errp, "error1"); =20 - g_assert(ERR_IS_IGNORED(errp) || ERR_IS_SET(errp)); + g_assert(ERR_IS_SET(errp)); } =20 static void fail2(Error **errp) { - g_assert(ERR_IS_IGNORED(errp) || !ERR_IS_SET(errp)); + g_assert(!ERR_IS_SET(errp)); =20 error_setg(errp, "error2"); =20 - g_assert(ERR_IS_IGNORED(errp) || ERR_IS_SET(errp)); + g_assert(ERR_IS_SET(errp)); } =20 static void multifn(Error **errp) @@ -124,13 +124,13 @@ static void test_propagate(void (*fn)(Error **), Erro= r **errp) bool failed; Error *local_err =3D NULL; =20 - g_assert(ERR_IS_IGNORED(errp) || !ERR_IS_SET(errp)); + g_assert(!ERR_IS_SET(errp)); =20 fn(&local_err); failed =3D !!local_err; error_propagate(errp, local_err); =20 - g_assert(ERR_IS_IGNORED(errp) || (failed =3D=3D !!ERR_IS_SET(errp))); + g_assert((failed =3D=3D !!ERR_IS_SET(errp))); } =20 static void test_error_api(void) diff --git a/util/error.c b/util/error.c index 4e804287eb..2e16ad371c 100644 --- a/util/error.c +++ b/util/error.c @@ -28,6 +28,8 @@ struct Error =20 Error *error_abort; Error *error_fatal; +Error ignored_error_unset; +Error ignored_error_set; =20 static void error_handle_fatal(Error **errp, Error *err) { @@ -52,6 +54,7 @@ static void error_setv(Error **errp, int saved_errno =3D errno; =20 if (ERR_IS_IGNORED(errp)) { + *errp =3D &ignored_error_set; return; } assert(!ERR_IS_SET(errp)); @@ -104,6 +107,7 @@ void error_setg_errno_internal(Error **errp, int saved_errno =3D errno; =20 if (ERR_IS_IGNORED(errp)) { + *errp =3D &ignored_error_set; return; } =20 @@ -127,6 +131,8 @@ void error_vprepend(Error **errp, const char *fmt, va_l= ist ap) { GString *newmsg; =20 + assert(ERR_IS_SET(errp)); + if (ERR_IS_IGNORED(errp)) { return; } @@ -153,6 +159,8 @@ void error_append_hint(Error **errp, const char *fmt, .= ..) int saved_errno =3D errno; Error *err; =20 + assert(ERR_IS_SET(errp)); + if (ERR_IS_IGNORED(errp)) { return; } @@ -179,6 +187,7 @@ void error_setg_win32_internal(Error **errp, char *suffix =3D NULL; =20 if (ERR_IS_IGNORED(errp)) { + *errp =3D &ignored_error_set; return; } =20 @@ -266,7 +275,9 @@ void error_propagate(Error **dst_errp, Error *local_err) return; } error_handle_fatal(dst_errp, local_err); - if (!ERR_IS_IGNORED(dst_errp) && !ERR_IS_SET(dst_errp)) { + if (ERR_IS_IGNORED(dst_errp)) { + *dst_errp =3D &ignored_error_set; + } else if (!ERR_IS_SET(dst_errp)) { *dst_errp =3D local_err; } else { error_free(local_err); --=20 2.11.0.259.g40922b1