From nobody Wed Nov 27 03:34:04 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1701983315916113.2223020336312; Thu, 7 Dec 2023 13:08:35 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rBLY4-0002mZ-IC; Thu, 07 Dec 2023 16:04:44 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rBLXz-0002k7-SR for qemu-devel@nongnu.org; Thu, 07 Dec 2023 16:04:40 -0500 Received: from smtp1.lauterbach.com ([62.154.241.196]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rBLXv-0005oG-4U for qemu-devel@nongnu.org; Thu, 07 Dec 2023 16:04:37 -0500 Received: (qmail 10374 invoked by uid 484); 7 Dec 2023 21:04:13 -0000 Received: from nedpc1.intern.lauterbach.com (Authenticated_SSL:neder@[10.2.11.92]) (envelope-sender ) by smtp1.lauterbach.com (qmail-ldap-1.03) with TLS_AES_256_GCM_SHA384 encrypted SMTP for ; 7 Dec 2023 21:04:11 -0000 X-Qmail-Scanner-Diagnostics: from nedpc1.intern.lauterbach.com by smtp1.lauterbach.com (envelope-from , uid 484) with qmail-scanner-2.11 (mhr: 1.0. clamdscan: 0.99/21437. spamassassin: 3.4.0. Clear:RC:1(10.2.11.92):. Processed in 0.072232 secs); 07 Dec 2023 21:04:13 -0000 From: Nicolas Eder To: qemu-devel@nongnu.org Cc: "Nicolas Eder" , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , "Christian Boenig" , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v4 08/17] cutils: qemu_strtou32 function added Date: Thu, 7 Dec 2023 22:03:49 +0100 Message-Id: <20231207210358.7409-9-nicolas.eder@lauterbach.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231207210358.7409-1-nicolas.eder@lauterbach.com> References: <20231207210358.7409-1-nicolas.eder@lauterbach.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Qmail-Scanner-2.11: added fake Content-Type header Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=62.154.241.196; envelope-from=nicolas.eder@lauterbach.com; helo=smtp1.lauterbach.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1701983316292100001 Content-Type: text/plain; charset="utf-8" --- include/qemu/cutils.h | 2 ++ util/cutils.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h index 5ab1a4ffb0..14f492ba61 100644 --- a/include/qemu/cutils.h +++ b/include/qemu/cutils.h @@ -158,6 +158,8 @@ int qemu_strtoul(const char *nptr, const char **endptr,= int base, unsigned long *result); int qemu_strtoi64(const char *nptr, const char **endptr, int base, int64_t *result); +int qemu_strtou32(const char *nptr, const char **endptr, int base, + uint32_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); diff --git a/util/cutils.c b/util/cutils.c index 42364039a5..5e00a4ec14 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -665,6 +665,36 @@ int qemu_strtoi64(const char *nptr, const char **endpt= r, int base, return check_strtox_error(nptr, ep, endptr, *result =3D=3D 0, errno); } =20 +/** + * Convert string @nptr to an uint32_t. + * + * Works like qemu_strtoul(), except it stores UINT32_MAX on overflow. + * (If you want to prohibit negative numbers that wrap around to + * positive, use parse_uint()). + */ +int qemu_strtou32(const char *nptr, const char **endptr, int base, + uint32_t *result) +{ + char *ep; + + assert((unsigned) base <=3D 36 && base !=3D 1); + if (!nptr) { + *result =3D 0; + if (endptr) { + *endptr =3D nptr; + } + return -EINVAL; + } + + 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, *result =3D=3D 0, errno); +} + /** * Convert string @nptr to an uint64_t. * --=20 2.34.1