From nobody Mon Feb 9 06:34:09 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 1487068941991429.93016402361366; Tue, 14 Feb 2017 02:42:21 -0800 (PST) Received: from localhost ([::1]:33776 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cdaYu-0003iF-Kt for importer@patchew.org; Tue, 14 Feb 2017 05:42:20 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39020) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cdaJX-0005y2-Pf for qemu-devel@nongnu.org; Tue, 14 Feb 2017 05:26:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cdaJU-0003Sm-IF for qemu-devel@nongnu.org; Tue, 14 Feb 2017 05:26:27 -0500 Received: from mx1.redhat.com ([209.132.183.28]:40890) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cdaJN-0003NW-VA; Tue, 14 Feb 2017 05:26:18 -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 2CB7F4DD49; Tue, 14 Feb 2017 10:26:18 +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 v1EAQGBQ024050 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Tue, 14 Feb 2017 05:26:17 -0500 Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id BA1BD11384C3; Tue, 14 Feb 2017 11:26:11 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Tue, 14 Feb 2017 11:26:08 +0100 Message-Id: <1487067971-10443-22-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.38]); Tue, 14 Feb 2017 10:26:18 +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 21/24] util/cutils: Let qemu_strtosz*() optionally reject trailing crap 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: Kevin Wolf , Max Reitz , "Dr . David Alan Gilbert" , "open list:Block layer core" , Eduardo Habkost 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" Change the qemu_strtosz() & friends to return -EINVAL when @endptr is null and the conversion doesn't consume the string completely. Matches how qemu_strtol() & friends work. Only test_qemu_strtosz_simple() passes a null @endptr. No functional change there, because its conversion consumes the string. Simplify callers that use @endptr only to fail when it doesn't point to '\0' to pass a null @endptr instead. Cc: Dr. David Alan Gilbert Cc: Eduardo Habkost (maintainer:X86) Cc: Kevin Wolf (supporter:Block layer core) Cc: Max Reitz (supporter:Block layer core) Cc: qemu-block@nongnu.org (open list:Block layer core) Signed-off-by: Markus Armbruster Reviewed-by: Dr. David Alan Gilbert Reviewed-by: Eric Blake --- hmp.c | 6 ++---- hw/misc/ivshmem.c | 6 ++---- qapi/opts-visitor.c | 5 ++--- qemu-img.c | 7 +------ qemu-io-cmds.c | 7 +------ target/i386/cpu.c | 5 ++--- tests/test-cutils.c | 6 ++++++ util/cutils.c | 14 +++++++++----- 8 files changed, 25 insertions(+), 31 deletions(-) diff --git a/hmp.c b/hmp.c index 6d0d05b..0eb5b6d 100644 --- a/hmp.c +++ b/hmp.c @@ -1340,7 +1340,6 @@ void hmp_migrate_set_parameter(Monitor *mon, const QD= ict *qdict) const char *valuestr =3D qdict_get_str(qdict, "value"); int64_t valuebw =3D 0; long valueint =3D 0; - char *endp; Error *err =3D NULL; bool use_int_value =3D false; int i; @@ -1379,9 +1378,8 @@ void hmp_migrate_set_parameter(Monitor *mon, const QD= ict *qdict) break; case MIGRATION_PARAMETER_MAX_BANDWIDTH: p.has_max_bandwidth =3D true; - valuebw =3D qemu_strtosz_mebi(valuestr, &endp); - if (valuebw < 0 || (size_t)valuebw !=3D valuebw - || *endp !=3D '\0') { + valuebw =3D qemu_strtosz_mebi(valuestr, NULL); + if (valuebw < 0 || (size_t)valuebw !=3D valuebw) { error_setg(&err, "Invalid size %s", valuestr); goto cleanup; } diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c index 382dd8b..f00cd75 100644 --- a/hw/misc/ivshmem.c +++ b/hw/misc/ivshmem.c @@ -1267,10 +1267,8 @@ static void ivshmem_realize(PCIDevice *dev, Error **= errp) if (s->sizearg =3D=3D NULL) { s->legacy_size =3D 4 << 20; /* 4 MB default */ } else { - char *end; - int64_t size =3D qemu_strtosz_mebi(s->sizearg, &end); - if (size < 0 || (size_t)size !=3D size || *end !=3D '\0' - || !is_power_of_2(size)) { + int64_t size =3D qemu_strtosz_mebi(s->sizearg, NULL); + if (size < 0 || (size_t)size !=3D size || !is_power_of_2(size)) { error_setg(errp, "Invalid size %s", s->sizearg); return; } diff --git a/qapi/opts-visitor.c b/qapi/opts-visitor.c index 360d337..911a0ee 100644 --- a/qapi/opts-visitor.c +++ b/qapi/opts-visitor.c @@ -482,15 +482,14 @@ opts_type_size(Visitor *v, const char *name, uint64_t= *obj, Error **errp) OptsVisitor *ov =3D to_ov(v); const QemuOpt *opt; int64_t val; - char *endptr; =20 opt =3D lookup_scalar(ov, name, errp); if (!opt) { return; } =20 - val =3D qemu_strtosz(opt->str ? opt->str : "", &endptr); - if (val < 0 || *endptr) { + val =3D qemu_strtosz(opt->str ? opt->str : "", NULL); + if (val < 0) { error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name, "a size value representible as a non-negative int64"); return; diff --git a/qemu-img.c b/qemu-img.c index 2a47966..adcb511 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -370,14 +370,9 @@ static int add_old_style_options(const char *fmt, Qemu= Opts *opts, =20 static int64_t cvtnum(const char *s) { - char *end; int64_t ret; =20 - ret =3D qemu_strtosz(s, &end); - if (*end !=3D '\0') { - /* Detritus at the end of the string */ - return -EINVAL; - } + ret =3D qemu_strtosz(s, NULL); return ret; } =20 diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c index 6b7a89c..417a4e0 100644 --- a/qemu-io-cmds.c +++ b/qemu-io-cmds.c @@ -137,14 +137,9 @@ static char **breakline(char *input, int *count) =20 static int64_t cvtnum(const char *s) { - char *end; int64_t ret; =20 - ret =3D qemu_strtosz(s, &end); - if (*end !=3D '\0') { - /* Detritus at the end of the string */ - return -EINVAL; - } + ret =3D qemu_strtosz(s, NULL); return ret; } =20 diff --git a/target/i386/cpu.c b/target/i386/cpu.c index 3a8d72d..5cb0b4b 100644 --- a/target/i386/cpu.c +++ b/target/i386/cpu.c @@ -2034,10 +2034,9 @@ static void x86_cpu_parse_featurestr(const char *typ= ename, char *features, /* Special case: */ if (!strcmp(name, "tsc-freq")) { int64_t tsc_freq; - char *err; =20 - tsc_freq =3D qemu_strtosz_metric(val, &err); - if (tsc_freq < 0 || *err) { + tsc_freq =3D qemu_strtosz_metric(val, NULL); + if (tsc_freq < 0) { error_setg(errp, "bad numerical value %s", val); return; } diff --git a/tests/test-cutils.c b/tests/test-cutils.c index 585912b..07095e3 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1510,10 +1510,16 @@ static void test_qemu_strtosz_trailing(void) g_assert_cmpint(res, =3D=3D, 123 * M_BYTE); g_assert(endptr =3D=3D str + 3); =20 + res =3D qemu_strtosz(str, NULL); + g_assert_cmpint(res, =3D=3D, -EINVAL); + str =3D "1kiB"; res =3D qemu_strtosz(str, &endptr); g_assert_cmpint(res, =3D=3D, 1024); g_assert(endptr =3D=3D str + 2); + + res =3D qemu_strtosz(str, NULL); + g_assert_cmpint(res, =3D=3D, -EINVAL); } =20 static void test_qemu_strtosz_erange(void) diff --git a/util/cutils.c b/util/cutils.c index 4a290ea..5c1bfe5 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -208,7 +208,7 @@ static int64_t suffix_mul(char suffix, int64_t unit) static int64_t do_strtosz(const char *nptr, char **end, const char default_suffix, int64_t unit) { - int64_t retval =3D -EINVAL; + int64_t retval; char *endptr; unsigned char c; int mul_required =3D 0; @@ -217,7 +217,8 @@ static int64_t do_strtosz(const char *nptr, char **end, errno =3D 0; val =3D strtod(nptr, &endptr); if (isnan(val) || endptr =3D=3D nptr || errno !=3D 0) { - goto fail; + retval =3D -EINVAL; + goto out; } fraction =3D modf(val, &integral); if (fraction !=3D 0) { @@ -232,17 +233,20 @@ static int64_t do_strtosz(const char *nptr, char **en= d, assert(mul >=3D 0); } if (mul =3D=3D 1 && mul_required) { - goto fail; + retval =3D -EINVAL; + goto out; } if ((val * mul >=3D INT64_MAX) || val < 0) { retval =3D -ERANGE; - goto fail; + goto out; } retval =3D val * mul; =20 -fail: +out: if (end) { *end =3D endptr; + } else if (*endptr) { + retval =3D -EINVAL; } =20 return retval; --=20 2.7.4