From nobody Mon Feb 9 07:23:47 2026 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 1487069161362173.57898754252676; Tue, 14 Feb 2017 02:46:01 -0800 (PST) Received: from localhost ([::1]:33796 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cdacS-0007OO-0a for importer@patchew.org; Tue, 14 Feb 2017 05:46:00 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38769) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cdaJP-0005qn-Ay for qemu-devel@nongnu.org; Tue, 14 Feb 2017 05:26:23 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cdaJM-0003MM-FE for qemu-devel@nongnu.org; Tue, 14 Feb 2017 05:26:19 -0500 Received: from mx1.redhat.com ([209.132.183.28]:53132) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cdaJM-0003LP-5j for qemu-devel@nongnu.org; Tue, 14 Feb 2017 05:26:16 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 597FA3D956 for ; Tue, 14 Feb 2017 10:26:16 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-50.ams2.redhat.com [10.36.116.50]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1EAQEMB024036 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO) for ; Tue, 14 Feb 2017 05:26:15 -0500 Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 9412811384B4; Tue, 14 Feb 2017 11:26:11 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Tue, 14 Feb 2017 11:25:55 +0100 Message-Id: <1487067971-10443-9-git-send-email-armbru@redhat.com> In-Reply-To: <1487067971-10443-1-git-send-email-armbru@redhat.com> References: <1487067971-10443-1-git-send-email-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 14 Feb 2017 10:26:16 +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] [PATCH 08/24] util/cutils: Clean up control flow around qemu_strtol() a bit 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: , 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" Reorder check_strtox_error() to make it obvious that we always store through a non-null @endptr. Transform if (some error) { error case ... err =3D value for error case; } else { normal case ... err =3D value for normal case; } return err; to if (some error) { error case ... return value for error case; } normal case ... return value for normal case; Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- util/cutils.c | 89 ++++++++++++++++++++++++++++++-------------------------= ---- 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/util/cutils.c b/util/cutils.c index 7d165bc..7442d46 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -265,15 +265,20 @@ int64_t qemu_strtosz(const char *nptr, char **end) static int check_strtox_error(const char *nptr, char *ep, const char **endptr, int eno) { - if (eno =3D=3D 0 && ep =3D=3D nptr) { - eno =3D EINVAL; - } - if (!endptr && *ep) { - return -EINVAL; - } if (endptr) { *endptr =3D ep; } + + /* Turn "no conversion" into an error */ + if (eno =3D=3D 0 && ep =3D=3D nptr) { + return -EINVAL; + } + + /* Fail when we're expected to consume the string, but didn't */ + if (!endptr && *ep) { + return -EINVAL; + } + return -eno; } =20 @@ -305,18 +310,17 @@ int qemu_strtol(const char *nptr, const char **endptr= , int base, long *result) { char *ep; - int err =3D 0; + if (!nptr) { if (endptr) { *endptr =3D nptr; } - err =3D -EINVAL; - } else { - errno =3D 0; - *result =3D strtol(nptr, &ep, base); - err =3D check_strtox_error(nptr, ep, endptr, errno); + return -EINVAL; } - return err; + + errno =3D 0; + *result =3D strtol(nptr, &ep, base); + return check_strtox_error(nptr, ep, endptr, errno); } =20 /** @@ -348,22 +352,21 @@ int qemu_strtoul(const char *nptr, const char **endpt= r, int base, unsigned long *result) { char *ep; - int err =3D 0; + if (!nptr) { if (endptr) { *endptr =3D nptr; } - err =3D -EINVAL; - } else { - errno =3D 0; - *result =3D strtoul(nptr, &ep, base); - /* Windows returns 1 for negative out-of-range values. */ - if (errno =3D=3D ERANGE) { - *result =3D -1; - } - err =3D check_strtox_error(nptr, ep, endptr, errno); + return -EINVAL; } - return err; + + errno =3D 0; + *result =3D strtoul(nptr, &ep, base); + /* Windows returns 1 for negative out-of-range values. */ + if (errno =3D=3D ERANGE) { + *result =3D -1; + } + return check_strtox_error(nptr, ep, endptr, errno); } =20 /** @@ -376,19 +379,18 @@ int qemu_strtoi64(const char *nptr, const char **endp= tr, int base, int64_t *result) { char *ep; - int err =3D 0; + if (!nptr) { if (endptr) { *endptr =3D nptr; } - err =3D -EINVAL; - } else { - errno =3D 0; - /* FIXME This assumes int64_t is long long */ - *result =3D strtoll(nptr, &ep, base); - err =3D check_strtox_error(nptr, ep, endptr, errno); + return -EINVAL; } - return err; + + errno =3D 0; + /* FIXME This assumes int64_t is long long */ + *result =3D strtoll(nptr, &ep, base); + return check_strtox_error(nptr, ep, endptr, errno); } =20 /** @@ -400,23 +402,22 @@ int qemu_strtou64(const char *nptr, const char **endp= tr, int base, uint64_t *result) { char *ep; - int err =3D 0; + if (!nptr) { if (endptr) { *endptr =3D nptr; } - err =3D -EINVAL; - } else { - errno =3D 0; - /* FIXME This assumes uint64_t is unsigned long long */ - *result =3D strtoull(nptr, &ep, base); - /* Windows returns 1 for negative out-of-range values. */ - if (errno =3D=3D ERANGE) { - *result =3D -1; - } - err =3D check_strtox_error(nptr, ep, endptr, errno); + return -EINVAL; } - return err; + + errno =3D 0; + /* FIXME This assumes uint64_t is unsigned long long */ + *result =3D strtoull(nptr, &ep, base); + /* Windows returns 1 for negative out-of-range values. */ + if (errno =3D=3D ERANGE) { + *result =3D -1; + } + return check_strtox_error(nptr, ep, endptr, errno); } =20 /** --=20 2.7.4