From nobody Mon Feb 9 01:22:39 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 1487069638187992.3041590734922; Tue, 14 Feb 2017 02:53:58 -0800 (PST) Received: from localhost ([::1]:33849 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cdak7-0006YK-PS for importer@patchew.org; Tue, 14 Feb 2017 05:53:55 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38838) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cdaJQ-0005r2-Q5 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 1cdaJO-0003OS-4T for qemu-devel@nongnu.org; Tue, 14 Feb 2017 05:26:20 -0500 Received: from mx1.redhat.com ([209.132.183.28]:58602) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cdaJN-0003NT-Rz for qemu-devel@nongnu.org; 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 1AEBE61D30 for ; 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 v1EAQGvV024054 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO) for ; Tue, 14 Feb 2017 05:26:17 -0500 Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id C471311384C8; Tue, 14 Feb 2017 11:26:11 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Tue, 14 Feb 2017 11:26:11 +0100 Message-Id: <1487067971-10443-25-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.39]); 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 24/24] QemuOpts: Fix checking of sizes for overflow and 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: , 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" parse_option_size()'s checking for overflow and trailing crap is wrong. Has always been that way. qemu_strtosz() gets it right, so use that. This adds support for size suffixes 'P', 'E', and ignores case for all suffixes, not just 'k'. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- tests/test-qemu-opts.c | 21 +++++++++------------ util/qemu-option.c | 41 +++++++++++++---------------------------- 2 files changed, 22 insertions(+), 40 deletions(-) diff --git a/tests/test-qemu-opts.c b/tests/test-qemu-opts.c index 6a9d3c5..b9d5b7e 100644 --- a/tests/test-qemu-opts.c +++ b/tests/test-qemu-opts.c @@ -667,10 +667,9 @@ static void test_opts_parse_size(void) g_assert(!opts); opts =3D qemu_opts_parse(&opts_list_02, "size1=3D18446744073709550592", /* ffffffffffff= fc00 */ - false, &error_abort); - /* BUG: should reject */ - g_assert_cmpuint(opts_count(opts), =3D=3D, 1); - g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), =3D=3D, 0); + false, &err); + error_free_or_abort(&err); + g_assert(!opts); =20 /* Suffixes */ opts =3D qemu_opts_parse(&opts_list_02, "size1=3D8b,size2=3D1.5k,size3= =3D2M", @@ -688,19 +687,17 @@ static void test_opts_parse_size(void) =20 /* Beyond limit with suffix */ opts =3D qemu_opts_parse(&opts_list_02, "size1=3D16777216T", - false, &error_abort); - /* BUG: should reject */ - g_assert_cmpuint(opts_count(opts), =3D=3D, 1); - g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), =3D=3D, 0); + false, &err); + error_free_or_abort(&err); + g_assert(!opts); =20 /* Trailing crap */ opts =3D qemu_opts_parse(&opts_list_02, "size1=3D16E", false, &err); error_free_or_abort(&err); g_assert(!opts); - opts =3D qemu_opts_parse(&opts_list_02, "size1=3D16Gi", false, &error_= abort); - /* BUG: should reject */ - g_assert_cmpuint(opts_count(opts), =3D=3D, 1); - g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), =3D=3D, 16 * G_B= YTE); + opts =3D qemu_opts_parse(&opts_list_02, "size1=3D16Gi", false, &err); + error_free_or_abort(&err); + g_assert(!opts); =20 qemu_opts_reset(&opts_list_02); } diff --git a/util/qemu-option.c b/util/qemu-option.c index 5d82327..c11ce93 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -174,39 +174,24 @@ static const QemuOptDesc *find_desc_by_name(const Qem= uOptDesc *desc, void parse_option_size(const char *name, const char *value, uint64_t *ret, Error **errp) { - char *postfix; - double sizef; + uint64_t size; + int err; =20 - sizef =3D strtod(value, &postfix); - if (sizef < 0 || sizef > UINT64_MAX) { + err =3D qemu_strtosz(value, NULL, &size); + if (err =3D=3D -ERANGE) { + error_setg(errp, "Value '%s' is too large for parameter '%s'", + value, name); + return; + } + if (err) { error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a non-negative number below 2^64"); + error_append_hint(errp, "Optional suffix k, M, G, T, P or E means" + " kilo-, mega-, giga-, tera-, peta-\n" + "and exabytes, respectively.\n"); return; } - switch (*postfix) { - case 'T': - sizef *=3D 1024; - /* fall through */ - case 'G': - sizef *=3D 1024; - /* fall through */ - case 'M': - sizef *=3D 1024; - /* fall through */ - case 'K': - case 'k': - sizef *=3D 1024; - /* fall through */ - case 'b': - case '\0': - *ret =3D (uint64_t) sizef; - break; - default: - error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size"); - error_append_hint(errp, "You may use k, M, G or T suffixes for " - "kilobytes, megabytes, gigabytes and terabytes.\= n"); - return; - } + *ret =3D size; } =20 bool has_help_option(const char *param) --=20 2.7.4