From nobody Thu Nov 6 19:28:02 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.zohomail.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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1542818790358933.2348473157585; Wed, 21 Nov 2018 08:46:30 -0800 (PST) Received: from localhost ([::1]:40173 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gPVdp-0008MY-0A for importer@patchew.org; Wed, 21 Nov 2018 11:46:17 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51617) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gPVc6-0007Q6-25 for qemu-devel@nongnu.org; Wed, 21 Nov 2018 11:44:30 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gPVc4-0006YA-Dj for qemu-devel@nongnu.org; Wed, 21 Nov 2018 11:44:30 -0500 Received: from mx1.redhat.com ([209.132.183.28]:40300) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gPVc4-0006Xd-6D for qemu-devel@nongnu.org; Wed, 21 Nov 2018 11:44:28 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8A613307C943; Wed, 21 Nov 2018 16:44:27 +0000 (UTC) Received: from t460s.redhat.com (ovpn-116-177.ams2.redhat.com [10.36.116.177]) by smtp.corp.redhat.com (Postfix) with ESMTP id 30E035C21E; Wed, 21 Nov 2018 16:44:26 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Wed, 21 Nov 2018 17:44:13 +0100 Message-Id: <20181121164421.20780-2-david@redhat.com> In-Reply-To: <20181121164421.20780-1-david@redhat.com> References: <20181121164421.20780-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.46]); Wed, 21 Nov 2018 16:44:27 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 for-4.0 1/9] cutils: Add qemu_strtod() and qemu_strtod_finite() 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: Paolo Bonzini , David Hildenbrand , Markus Armbruster , Michael Roth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Let's provide a wrapper for strtod(). Reviewed-by: Markus Armbruster Signed-off-by: David Hildenbrand Reviewed-by: Eric Blake --- include/qemu/cutils.h | 2 ++ util/cutils.c | 65 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h index 7071bfe2d4..756b41c193 100644 --- a/include/qemu/cutils.h +++ b/include/qemu/cutils.h @@ -146,6 +146,8 @@ int qemu_strtoi64(const char *nptr, const char **endptr= , int base, int64_t *result); int qemu_strtou64(const char *nptr, const char **endptr, int base, uint64_t *result); +int qemu_strtod(const char *nptr, const char **endptr, double *result); +int qemu_strtod_finite(const char *nptr, const char **endptr, double *resu= lt); =20 int parse_uint(const char *s, unsigned long long *value, char **endptr, int base); diff --git a/util/cutils.c b/util/cutils.c index 698bd315bd..173407d2fd 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -544,6 +544,71 @@ int qemu_strtou64(const char *nptr, const char **endpt= r, int base, return check_strtox_error(nptr, ep, endptr, errno); } =20 +/** + * Convert string @nptr to a double. + * + * This is a wrapper around strtod() that is harder to misuse. + * Semantics of @nptr and @endptr match strtod() with differences + * noted below. + * + * @nptr may be null, and no conversion is performed then. + * + * If no conversion is performed, store @nptr in *@endptr and return + * -EINVAL. + * + * If @endptr is null, and the string isn't fully converted, return + * -EINVAL. This is the case when the pointer that would be stored in + * a non-null @endptr points to a character other than '\0'. + * + * If the conversion overflows, store +/-HUGE_VAL in @result, depending + * on the sign, and return -ERANGE. + * + * If the conversion underflows, store +/-0.0 in @result, depending on the + * sign, and return -ERANGE. + * + * Else store the converted value in @result, and return zero. + */ +int qemu_strtod(const char *nptr, const char **endptr, double *result) +{ + char *ep; + + if (!nptr) { + if (endptr) { + *endptr =3D nptr; + } + return -EINVAL; + } + + errno =3D 0; + *result =3D strtod(nptr, &ep); + return check_strtox_error(nptr, ep, endptr, errno); +} + +/** + * Convert string @nptr to a finite double. + * + * Works like qemu_strtod(), except that "NaN" and "inf" are rejected + * with -EINVAL and no conversion is performed. + */ +int qemu_strtod_finite(const char *nptr, const char **endptr, double *resu= lt) +{ + double tmp; + int ret; + + ret =3D qemu_strtod(nptr, endptr, &tmp); + if (!ret && !isfinite(tmp)) { + if (endptr) { + *endptr =3D nptr; + } + ret =3D -EINVAL; + } + + if (ret !=3D -EINVAL) { + *result =3D tmp; + } + return ret; +} + /** * Searches for the first occurrence of 'c' in 's', and returns a pointer * to the trailing null byte if none was found. --=20 2.17.2