From nobody Tue Feb 10 05:43:51 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1574409028; cv=none; d=zoho.com; s=zohoarc; b=NZtbV9SfsWpz+IxVpmcCcZW5qhD8n5rC3KhqJV1hAYD2X6tuRWZN4N4J+RhP0pHabC9J4s2QsZDUz8cIyqgmsTkmmV7H7C4egOf5Wv+f+jxoo7Cx3N0WAYNWze+GQR0p7Yqny/g8exc/nJo7DXIrC+7dTi4D0rf0UOTLwYTERM0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1574409028; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=hjuRD1FBU4B8mbycuUVhbrX4Y1WM3W2S72LaREydNNs=; b=avRrwa55rOPLI3P7JTR6vy4kAS1DtIzmE75A4BkdW9AuFJJW7UZyqcMlzH1GQIEGKLp/jZoq9FWjLnnZhsbdKPB+n1zmsgXoGppAkXaCF3onxP8/senDAJBTwvSzZPIgC2QFlJG4j/vdt+BBWEhXMIa/+I/qeZndgUZqB5hPTSE= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574409028700760.6491031359914; Thu, 21 Nov 2019 23:50:28 -0800 (PST) Received: from localhost ([::1]:47944 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iY3hz-0006jW-Ey for importer@patchew.org; Fri, 22 Nov 2019 02:50:27 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39474) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iY3gJ-0005GW-A5 for qemu-devel@nongnu.org; Fri, 22 Nov 2019 02:48:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iY3gH-0002m9-Ot for qemu-devel@nongnu.org; Fri, 22 Nov 2019 02:48:43 -0500 Received: from mga18.intel.com ([134.134.136.126]:36768) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iY3gH-0002kF-Gj for qemu-devel@nongnu.org; Fri, 22 Nov 2019 02:48:41 -0500 Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 21 Nov 2019 23:48:38 -0800 Received: from tao-optiplex-7060.sh.intel.com ([10.239.159.36]) by orsmga003.jf.intel.com with ESMTP; 21 Nov 2019 23:48:35 -0800 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.69,229,1571727600"; d="scan'208";a="210178459" From: Tao Xu To: mst@redhat.com, imammedo@redhat.com, eblake@redhat.com, ehabkost@redhat.com, marcel.apfelbaum@gmail.com, armbru@redhat.com, sw@weilnetz.de, mdroth@linux.vnet.ibm.com, thuth@redhat.com, lvivier@redhat.com Subject: [PATCH v17 02/14] util/cutils: Use qemu_strtold_finite to parse size Date: Fri, 22 Nov 2019 15:48:14 +0800 Message-Id: <20191122074826.1373-3-tao3.xu@intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191122074826.1373-1-tao3.xu@intel.com> References: <20191122074826.1373-1-tao3.xu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.126 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: jingqi.liu@intel.com, tao3.xu@intel.com, fan.du@intel.com, qemu-devel@nongnu.org, jonathan.cameron@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Support full 64bit precision, modify related test cases. Signed-off-by: Tao Xu --- No changes in v17. --- tests/test-cutils.c | 41 +++++------------------------------- tests/test-keyval.c | 47 +++++++----------------------------------- tests/test-qemu-opts.c | 39 +++++++---------------------------- util/cutils.c | 13 +++++------- 4 files changed, 24 insertions(+), 116 deletions(-) diff --git a/tests/test-cutils.c b/tests/test-cutils.c index 1aa8351520..465514b85f 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1970,40 +1970,19 @@ static void test_qemu_strtosz_simple(void) g_assert_cmpint(err, =3D=3D, 0); g_assert_cmpint(res, =3D=3D, 12345); =20 - /* Note: precision is 53 bits since we're parsing with strtod() */ + /* Note: precision is 64 bits (UINT64_MAX) */ =20 - str =3D "9007199254740991"; /* 2^53-1 */ + str =3D "18446744073709551614"; /* UINT64_MAX - 1 */ err =3D qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, =3D=3D, 0); - g_assert_cmpint(res, =3D=3D, 0x1fffffffffffff); - g_assert(endptr =3D=3D str + 16); - - str =3D "9007199254740992"; /* 2^53 */ - err =3D qemu_strtosz(str, &endptr, &res); - g_assert_cmpint(err, =3D=3D, 0); - g_assert_cmpint(res, =3D=3D, 0x20000000000000); - g_assert(endptr =3D=3D str + 16); - - str =3D "9007199254740993"; /* 2^53+1 */ - err =3D qemu_strtosz(str, &endptr, &res); - g_assert_cmpint(err, =3D=3D, 0); - g_assert_cmpint(res, =3D=3D, 0x20000000000000); /* rounded to 53 bits = */ - g_assert(endptr =3D=3D str + 16); - - str =3D "18446744073709549568"; /* 0xfffffffffffff800 (53 msbs set) */ - err =3D qemu_strtosz(str, &endptr, &res); - g_assert_cmpint(err, =3D=3D, 0); - g_assert_cmpint(res, =3D=3D, 0xfffffffffffff800); + g_assert_cmpint(res, =3D=3D, 0xfffffffffffffffe); g_assert(endptr =3D=3D str + 20); =20 - str =3D "18446744073709550591"; /* 0xfffffffffffffbff */ + str =3D "18446744073709551615"; /* UINT64_MAX */ err =3D qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, =3D=3D, 0); - g_assert_cmpint(res, =3D=3D, 0xfffffffffffff800); /* rounded to 53 bit= s */ + g_assert_cmpint(res, =3D=3D, 0xffffffffffffffff); g_assert(endptr =3D=3D str + 20); - - /* 0x7ffffffffffffe00..0x7fffffffffffffff get rounded to - * 0x8000000000000000, thus -ERANGE; see test_qemu_strtosz_erange() */ } =20 static void test_qemu_strtosz_units(void) @@ -2145,16 +2124,6 @@ static void test_qemu_strtosz_erange(void) g_assert_cmpint(err, =3D=3D, -ERANGE); g_assert(endptr =3D=3D str + 2); =20 - str =3D "18446744073709550592"; /* 0xfffffffffffffc00 */ - err =3D qemu_strtosz(str, &endptr, &res); - g_assert_cmpint(err, =3D=3D, -ERANGE); - g_assert(endptr =3D=3D str + 20); - - str =3D "18446744073709551615"; /* 2^64-1 */ - err =3D qemu_strtosz(str, &endptr, &res); - g_assert_cmpint(err, =3D=3D, -ERANGE); - g_assert(endptr =3D=3D str + 20); - str =3D "18446744073709551616"; /* 2^64 */ err =3D qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, =3D=3D, -ERANGE); diff --git a/tests/test-keyval.c b/tests/test-keyval.c index 09b0ae3c68..fad941fcb8 100644 --- a/tests/test-keyval.c +++ b/tests/test-keyval.c @@ -383,59 +383,26 @@ static void test_keyval_visit_size(void) visit_end_struct(v, NULL); visit_free(v); =20 - /* Note: precision is 53 bits since we're parsing with strtod() */ + /* Note: precision is 64 bits (UINT64_MAX) */ =20 - /* Around limit of precision: 2^53-1, 2^53, 2^53+1 */ - qdict =3D keyval_parse("sz1=3D9007199254740991," - "sz2=3D9007199254740992," - "sz3=3D9007199254740993", + /* Around limit of precision: UINT64_MAX - 1, UINT64_MAX */ + qdict =3D keyval_parse("sz1=3D18446744073709551614," + "sz2=3D18446744073709551615", NULL, &error_abort); v =3D qobject_input_visitor_new_keyval(QOBJECT(qdict)); qobject_unref(qdict); visit_start_struct(v, NULL, NULL, 0, &error_abort); visit_type_size(v, "sz1", &sz, &error_abort); - g_assert_cmphex(sz, =3D=3D, 0x1fffffffffffff); + g_assert_cmphex(sz, =3D=3D, 0xfffffffffffffffe); visit_type_size(v, "sz2", &sz, &error_abort); - g_assert_cmphex(sz, =3D=3D, 0x20000000000000); - visit_type_size(v, "sz3", &sz, &error_abort); - g_assert_cmphex(sz, =3D=3D, 0x20000000000000); - visit_check_struct(v, &error_abort); - visit_end_struct(v, NULL); - visit_free(v); - - /* Close to signed upper limit 0x7ffffffffffffc00 (53 msbs set) */ - qdict =3D keyval_parse("sz1=3D9223372036854774784," /* 7ffffffffffffc0= 0 */ - "sz2=3D9223372036854775295", /* 7ffffffffffffdff = */ - NULL, &error_abort); - v =3D qobject_input_visitor_new_keyval(QOBJECT(qdict)); - qobject_unref(qdict); - visit_start_struct(v, NULL, NULL, 0, &error_abort); - visit_type_size(v, "sz1", &sz, &error_abort); - g_assert_cmphex(sz, =3D=3D, 0x7ffffffffffffc00); - visit_type_size(v, "sz2", &sz, &error_abort); - g_assert_cmphex(sz, =3D=3D, 0x7ffffffffffffc00); - visit_check_struct(v, &error_abort); - visit_end_struct(v, NULL); - visit_free(v); - - /* Close to actual upper limit 0xfffffffffffff800 (53 msbs set) */ - qdict =3D keyval_parse("sz1=3D18446744073709549568," /* fffffffffffff8= 00 */ - "sz2=3D18446744073709550591", /* fffffffffffffbff= */ - NULL, &error_abort); - v =3D qobject_input_visitor_new_keyval(QOBJECT(qdict)); - qobject_unref(qdict); - visit_start_struct(v, NULL, NULL, 0, &error_abort); - visit_type_size(v, "sz1", &sz, &error_abort); - g_assert_cmphex(sz, =3D=3D, 0xfffffffffffff800); - visit_type_size(v, "sz2", &sz, &error_abort); - g_assert_cmphex(sz, =3D=3D, 0xfffffffffffff800); + g_assert_cmphex(sz, =3D=3D, 0xffffffffffffffff); visit_check_struct(v, &error_abort); visit_end_struct(v, NULL); visit_free(v); =20 /* Beyond limits */ qdict =3D keyval_parse("sz1=3D-1," - "sz2=3D18446744073709550592", /* fffffffffffffc00= */ + "sz2=3D18446744073709551616", /* 2^64 */ NULL, &error_abort); v =3D qobject_input_visitor_new_keyval(QOBJECT(qdict)); qobject_unref(qdict); diff --git a/tests/test-qemu-opts.c b/tests/test-qemu-opts.c index ef96e84aed..1236bf6b7d 100644 --- a/tests/test-qemu-opts.c +++ b/tests/test-qemu-opts.c @@ -650,50 +650,25 @@ static void test_opts_parse_size(void) g_assert_cmpuint(opts_count(opts), =3D=3D, 1); g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), =3D=3D, 0); =20 - /* Note: precision is 53 bits since we're parsing with strtod() */ + /* Note: precision is 64 bits (UINT64_MAX) */ =20 - /* Around limit of precision: 2^53-1, 2^53, 2^54 */ + /* Around limit of precision: UINT64_MAX - 1, UINT64_MAX */ opts =3D qemu_opts_parse(&opts_list_02, - "size1=3D9007199254740991," - "size2=3D9007199254740992," - "size3=3D9007199254740993", - false, &error_abort); - g_assert_cmpuint(opts_count(opts), =3D=3D, 3); - g_assert_cmphex(qemu_opt_get_size(opts, "size1", 1), - =3D=3D, 0x1fffffffffffff); - g_assert_cmphex(qemu_opt_get_size(opts, "size2", 1), - =3D=3D, 0x20000000000000); - g_assert_cmphex(qemu_opt_get_size(opts, "size3", 1), - =3D=3D, 0x20000000000000); - - /* Close to signed upper limit 0x7ffffffffffffc00 (53 msbs set) */ - opts =3D qemu_opts_parse(&opts_list_02, - "size1=3D9223372036854774784," /* 7ffffffffffff= c00 */ - "size2=3D9223372036854775295", /* 7ffffffffffff= dff */ - false, &error_abort); - g_assert_cmpuint(opts_count(opts), =3D=3D, 2); - g_assert_cmphex(qemu_opt_get_size(opts, "size1", 1), - =3D=3D, 0x7ffffffffffffc00); - g_assert_cmphex(qemu_opt_get_size(opts, "size2", 1), - =3D=3D, 0x7ffffffffffffc00); - - /* Close to actual upper limit 0xfffffffffffff800 (53 msbs set) */ - opts =3D qemu_opts_parse(&opts_list_02, - "size1=3D18446744073709549568," /* ffffffffffff= f800 */ - "size2=3D18446744073709550591", /* ffffffffffff= fbff */ + "size1=3D18446744073709551614," + "size2=3D18446744073709551615", false, &error_abort); g_assert_cmpuint(opts_count(opts), =3D=3D, 2); g_assert_cmphex(qemu_opt_get_size(opts, "size1", 1), - =3D=3D, 0xfffffffffffff800); + =3D=3D, 0xfffffffffffffffe); g_assert_cmphex(qemu_opt_get_size(opts, "size2", 1), - =3D=3D, 0xfffffffffffff800); + =3D=3D, 0xffffffffffffffff); =20 /* Beyond limits */ opts =3D qemu_opts_parse(&opts_list_02, "size1=3D-1", false, &err); error_free_or_abort(&err); g_assert(!opts); opts =3D qemu_opts_parse(&opts_list_02, - "size1=3D18446744073709550592", /* ffffffffffff= fc00 */ + "size1=3D18446744073709551616", /* 2^64 */ false, &err); error_free_or_abort(&err); g_assert(!opts); diff --git a/util/cutils.c b/util/cutils.c index 5db3b2add5..d94a468954 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -216,13 +216,13 @@ static int do_strtosz(const char *nptr, const char **= end, const char *endptr; unsigned char c; int mul_required =3D 0; - double val, mul, integral, fraction; + long double val, mul, integral, fraction; =20 - retval =3D qemu_strtod_finite(nptr, &endptr, &val); + retval =3D qemu_strtold_finite(nptr, &endptr, &val); if (retval) { goto out; } - fraction =3D modf(val, &integral); + fraction =3D modfl(val, &integral); if (fraction !=3D 0) { mul_required =3D 1; } @@ -238,11 +238,8 @@ static int do_strtosz(const char *nptr, const char **e= nd, retval =3D -EINVAL; goto out; } - /* - * Values >=3D 0xfffffffffffffc00 overflow uint64_t after their trip - * through double (53 bits of precision). - */ - if ((val * mul >=3D 0xfffffffffffffc00) || val < 0) { + /* Values > UINT64_MAX overflow uint64_t */ + if ((val * mul > UINT64_MAX) || val < 0) { retval =3D -ERANGE; goto out; } --=20 2.20.1