From nobody Sun Oct 5 17:38:41 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1613078427; cv=none; d=zohomail.com; s=zohoarc; b=mVlYBnHe5i/XYcn1cEFuFd8//6FLbxJsv3r8BnyicHIzcY/pZnX2jLhyWuVSE6QBhLhH/s4LJW6moWDWyr1zNMjzsob8eBoMYqu0rXopETyz5mOWnUfTJrkRFxyCojHV4ceYSmsCEcWba6N80K5MdhlT4AJMzke/2S72Y8y3NHg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1613078427; h=Content-Type: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=Wr1IAzfe30rhb7m/BSh9G7kt+HzJhVBA0gnMMwAHsDo=; b=QWswYxscYbrMWZsvtqgIzT81v7ThjHLc0IKTqzyTk+UfUq1iqr1e8YvrnbMOe+mkmBinP2u+ZkzunDOSUBSTaU4+TK+C+OFhdPCrOHYI5JsNbZFYZa1T08au78xuucPDP5VGrE2zY9KLUZXHl6Dxrv2bo5UTwqmCeDykjKnbhjk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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; 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 1613078426803684.122511367745; Thu, 11 Feb 2021 13:20:26 -0800 (PST) Received: from localhost ([::1]:53320 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lAIvh-0002x9-OF for importer@patchew.org; Thu, 11 Feb 2021 15:51:13 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:43040) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lAIpX-0005sh-Df for qemu-devel@nongnu.org; Thu, 11 Feb 2021 15:44:52 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:20298) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lAIpR-0007Os-JG for qemu-devel@nongnu.org; Thu, 11 Feb 2021 15:44:51 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-27-tIm2zacaPG6B-bUcHrvDQw-1; Thu, 11 Feb 2021 15:44:43 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D90DB100CCC2; Thu, 11 Feb 2021 20:44:41 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-150.phx2.redhat.com [10.3.114.150]) by smtp.corp.redhat.com (Postfix) with ESMTP id 31BC510013D7; Thu, 11 Feb 2021 20:44:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613076285; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Wr1IAzfe30rhb7m/BSh9G7kt+HzJhVBA0gnMMwAHsDo=; b=KKdKdjocXjQ1XGqB8RqmMpNumJSsIXsLBmL4zafhusN3MT1VS5xRA1jp1ZMuDSIrOb/Rm7 +4ZmCkClkGa2EV20kN9wx+qGBwvRMm6OtyyPuC4KRQ4ki6CTmcesWkgHEw/waTHgP2grr3 EezTCjKt326AJCTs5Ka30KOfVrzP7Qc= X-MC-Unique: tIm2zacaPG6B-bUcHrvDQw-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PATCH v2 1/4] utils: Enhance testsuite for do_strtosz() Date: Thu, 11 Feb 2021 14:44:35 -0600 Message-Id: <20210211204438.1184395-2-eblake@redhat.com> In-Reply-To: <20210211204438.1184395-1-eblake@redhat.com> References: <20210211204438.1184395-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable 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=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -33 X-Spam_score: -3.4 X-Spam_bar: --- X-Spam_report: (-3.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.569, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001, T_SPF_HELO_TEMPERROR=0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action 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: vsementsov@virtuozzo.com, berrange@redhat.com, qemu-block@nongnu.org, tao3.xu@intel.com, rjones@redhat.com, armbru@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Enhance our testsuite coverage of do_strtosz() to cover some things we know that existing users want to continue working (hex bytes), as well as some things that accidentally work but shouldn't (hex fractions) or accidentally fail but that users want to work (64-bit precision on byte values). This includes fixing a typo in the comment regarding our parsing near 2^64. Signed-off-by: Eric Blake Reviewed-by: Daniel P. Berrang=C3=A9 --- tests/test-cutils.c | 154 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 143 insertions(+), 11 deletions(-) diff --git a/tests/test-cutils.c b/tests/test-cutils.c index 1aa8351520ae..6d9802e00b1b 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1960,11 +1960,19 @@ static void test_qemu_strtosz_simple(void) g_assert_cmpint(res, =3D=3D, 0); g_assert(endptr =3D=3D str + 1); - str =3D "12345"; + /* Leading 0 gives decimal results, not octal */ + str =3D "08"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, 0); + g_assert_cmpint(res, =3D=3D, 8); + g_assert(endptr =3D=3D str + 2); + + /* Leading space is ignored */ + str =3D " 12345"; err =3D qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, =3D=3D, 0); g_assert_cmpint(res, =3D=3D, 12345); - g_assert(endptr =3D=3D str + 5); + g_assert(endptr =3D=3D str + 6); err =3D qemu_strtosz(str, NULL, &res); g_assert_cmpint(err, =3D=3D, 0); @@ -1984,7 +1992,7 @@ static void test_qemu_strtosz_simple(void) g_assert_cmpint(res, =3D=3D, 0x20000000000000); g_assert(endptr =3D=3D str + 16); - str =3D "9007199254740993"; /* 2^53+1 */ + str =3D "9007199254740993"; /* 2^53+1 FIXME loss of precision is a bug= */ 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 = */ @@ -1996,14 +2004,42 @@ static void test_qemu_strtosz_simple(void) g_assert_cmpint(res, =3D=3D, 0xfffffffffffff800); g_assert(endptr =3D=3D str + 20); - str =3D "18446744073709550591"; /* 0xfffffffffffffbff */ + str =3D "18446744073709550591"; /* 0xfffffffffffffbff FIXME */ 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(endptr =3D=3D str + 20); - /* 0x7ffffffffffffe00..0x7fffffffffffffff get rounded to - * 0x8000000000000000, thus -ERANGE; see test_qemu_strtosz_erange() */ + /* + * FIXME 0xfffffffffffffe00..0xffffffffffffffff get rounded to + * 2^64, thus -ERANGE; see test_qemu_strtosz_erange() + */ +} + +static void test_qemu_strtosz_hex(void) +{ + const char *str; + const char *endptr; + int err; + uint64_t res =3D 0xbaadf00d; + + str =3D "0x0"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, 0); + g_assert_cmpint(res, =3D=3D, 0); + g_assert(endptr =3D=3D str + 3); + + str =3D "0xab"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, 0); + g_assert_cmpint(res, =3D=3D, 171); + g_assert(endptr =3D=3D str + 4); + + str =3D "0xae"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, 0); + g_assert_cmpint(res, =3D=3D, 174); + g_assert(endptr =3D=3D str + 4); } static void test_qemu_strtosz_units(void) @@ -2064,14 +2100,36 @@ static void test_qemu_strtosz_units(void) static void test_qemu_strtosz_float(void) { - const char *str =3D "12.345M"; + const char *str; int err; const char *endptr; uint64_t res =3D 0xbaadf00d; + str =3D "0.5E"; err =3D qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, =3D=3D, 0); - g_assert_cmpint(res, =3D=3D, 12.345 * MiB); + g_assert_cmpint(res, =3D=3D, EiB / 2); + g_assert(endptr =3D=3D str + 4); + + /* For convenience, a fraction of 0 is tolerated even on bytes */ + str =3D "1.0B"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, 0); + g_assert_cmpint(res, =3D=3D, 1); + g_assert(endptr =3D=3D str + 4); + + /* An empty fraction is tolerated */ + str =3D "1.k"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, 0); + g_assert_cmpint(res, =3D=3D, 1024); + g_assert(endptr =3D=3D str + 3); + + /* For convenience, we permit values that are not byte-exact */ + str =3D "12.345M"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, 0); + g_assert_cmpint(res, =3D=3D, (uint64_t) (12.345 * MiB)); g_assert(endptr =3D=3D str + 7); } @@ -2106,6 +2164,47 @@ static void test_qemu_strtosz_invalid(void) err =3D qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, =3D=3D, -EINVAL); g_assert(endptr =3D=3D str); + + /* Fractional values require scale larger than bytes */ + /* FIXME endptr shouldn't move on -EINVAL */ + str =3D "1.1B"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, -EINVAL); + g_assert(endptr =3D=3D str + 4); + + /* FIXME endptr shouldn't move on -EINVAL */ + str =3D "1.1"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, -EINVAL); + g_assert(endptr =3D=3D str + 3); + + /* FIXME we should reject floating point exponents */ + str =3D "1.5e1k"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, 0); + g_assert_cmpint(res, =3D=3D, 15360); + g_assert(endptr =3D=3D str + 6); + + /* FIXME we should reject floating point exponents */ + str =3D "1.5E+0k"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, 0); + g_assert_cmpint(res, =3D=3D, 1536); + g_assert(endptr =3D=3D str + 7); + + /* FIXME we should reject hex fractions */ + str =3D "0x1.8k"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, 0); + g_assert_cmpint(res, =3D=3D, 1536); + g_assert(endptr =3D=3D str + 6); + + /* FIXME we reject all other attempts at negative, why not -0 */ + str =3D "-0"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, 0); + g_assert_cmpint(res, =3D=3D, 0); + g_assert(endptr =3D=3D str + 2); } static void test_qemu_strtosz_trailing(void) @@ -2131,6 +2230,30 @@ static void test_qemu_strtosz_trailing(void) err =3D qemu_strtosz(str, NULL, &res); g_assert_cmpint(err, =3D=3D, -EINVAL); + + str =3D "0x"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(res, =3D=3D, 0); + g_assert(endptr =3D=3D str + 1); + + err =3D qemu_strtosz(str, NULL, &res); + g_assert_cmpint(err, =3D=3D, -EINVAL); + + str =3D "0.NaN"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, 0); + g_assert(endptr =3D=3D str + 2); + + err =3D qemu_strtosz(str, NULL, &res); + g_assert_cmpint(err, =3D=3D, -EINVAL); + + str =3D "123-45"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(res, =3D=3D, 123); + g_assert(endptr =3D=3D str + 3); + + err =3D qemu_strtosz(str, NULL, &res); + g_assert_cmpint(err, =3D=3D, -EINVAL); } static void test_qemu_strtosz_erange(void) @@ -2145,12 +2268,12 @@ static void test_qemu_strtosz_erange(void) g_assert_cmpint(err, =3D=3D, -ERANGE); g_assert(endptr =3D=3D str + 2); - str =3D "18446744073709550592"; /* 0xfffffffffffffc00 */ + str =3D "18446744073709550592"; /* 0xfffffffffffffc00 FIXME accept thi= s */ 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 */ + str =3D "18446744073709551615"; /* 2^64-1 FIXME accept this */ err =3D qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, =3D=3D, -ERANGE); g_assert(endptr =3D=3D str + 20); @@ -2168,15 +2291,22 @@ static void test_qemu_strtosz_erange(void) static void test_qemu_strtosz_metric(void) { - const char *str =3D "12345k"; + const char *str; int err; const char *endptr; uint64_t res =3D 0xbaadf00d; + str =3D "12345k"; err =3D qemu_strtosz_metric(str, &endptr, &res); g_assert_cmpint(err, =3D=3D, 0); g_assert_cmpint(res, =3D=3D, 12345000); g_assert(endptr =3D=3D str + 6); + + str =3D "12.345M"; + err =3D qemu_strtosz_metric(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, 0); + g_assert_cmpint(res, =3D=3D, 12345000); + g_assert(endptr =3D=3D str + 7); } int main(int argc, char **argv) @@ -2443,6 +2573,8 @@ int main(int argc, char **argv) g_test_add_func("/cutils/strtosz/simple", test_qemu_strtosz_simple); + g_test_add_func("/cutils/strtosz/hex", + test_qemu_strtosz_hex); g_test_add_func("/cutils/strtosz/units", test_qemu_strtosz_units); g_test_add_func("/cutils/strtosz/float", --=20 2.30.1 From nobody Sun Oct 5 17:38:41 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1613078625; cv=none; d=zohomail.com; s=zohoarc; b=YF/KibegM36vAHg9p8H5Rn68F9t4XKbazjj5yPZo105e7P4ntHuGxWeFuNZrfsjgKBASCrxW1NKHLgzFMQr+DEwSzQ7N92xpQuAPFZbTE8zIG975oLiuuZ9VkDdldn8Y0LH54OYvJgwcu7FWJMhOTWqTl7ZryH05VOFY5nyleds= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1613078625; h=Content-Type: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=UE1ml/pn/7dvsQEH1urpOqLErh0+o3x1vMK8RcqUT6g=; b=UFy6OJ6sa4svZK8dlqOIPcHAYIdkwfRGK2TfyYBtlqhf7IGpx2Vp6yJ/000qMXlDdeAd1+8RSvFjWZQvIgfIsMwPAaMkAiPpxOkzudIAlb8xDOvHP5tilAznuqLESoY3KTAUWCXlq8WgS8vox+Pb+4BfpAISzZzIxnpy1nzf+1E= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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; 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 1613078624669828.9837224535522; Thu, 11 Feb 2021 13:23:44 -0800 (PST) Received: from localhost ([::1]:58058 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lAIxz-0004yP-5I for importer@patchew.org; Thu, 11 Feb 2021 15:53:35 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:43058) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lAIpa-0005tf-2v for qemu-devel@nongnu.org; Thu, 11 Feb 2021 15:44:58 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:23462) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lAIpT-0007Pf-SF for qemu-devel@nongnu.org; Thu, 11 Feb 2021 15:44:52 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-466-R6L8a1GDOZWHB1ekgL_mqw-1; Thu, 11 Feb 2021 15:44:44 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E63A3107ACC7; Thu, 11 Feb 2021 20:44:42 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-150.phx2.redhat.com [10.3.114.150]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2823610013D7; Thu, 11 Feb 2021 20:44:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613076286; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UE1ml/pn/7dvsQEH1urpOqLErh0+o3x1vMK8RcqUT6g=; b=PgwOY3c1j8bT6no9eB6DWk+uXP6SPxv/Sy1528eTO1RYbFV1JNJpS2+BWTkc+t6sB8/qiP c2l38XQyEgeK8ovWln9CTMg+BUh8I9YQRH5M0aHbPVXa4hmdoFp+DHBSqvQzELumBhY3Yk ocu99bBrBD3aMSlLyTV8WiXE7MBj7X8= X-MC-Unique: R6L8a1GDOZWHB1ekgL_mqw-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PATCH v2 2/4] utils: Improve qemu_strtosz() to have 64 bits of precision Date: Thu, 11 Feb 2021 14:44:36 -0600 Message-Id: <20210211204438.1184395-3-eblake@redhat.com> In-Reply-To: <20210211204438.1184395-1-eblake@redhat.com> References: <20210211204438.1184395-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable 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=63.128.21.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -33 X-Spam_score: -3.4 X-Spam_bar: --- X-Spam_report: (-3.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.569, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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: Kevin Wolf , vsementsov@virtuozzo.com, berrange@redhat.com, qemu-block@nongnu.org, tao3.xu@intel.com, rjones@redhat.com, armbru@redhat.com, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" We have multiple clients of qemu_strtosz (qemu-io, the opts visitor, the keyval visitor), and it gets annoying that edge-case testing is impacted by implicit rounding to 53 bits of precision due to parsing with strtod(). As an example posted by Rich Jones: $ nbdkit memory $(( 2**63 - 2**30 )) --run \ 'build/qemu-io -f raw "$uri" -c "w -P 3 $(( 2**63 - 2**30 - 512 )) 512" ' write failed: Input/output error because 9223372035781033472 got rounded to 0x7fffffffc0000000 which is out of bounds. It is also worth noting that our existing parser, by virtue of using strtod(), accepts decimal AND hex numbers, even though test-cutils previously lacked any coverage of the latter until the previous patch. We do have existing clients that expect a hex parse to work (for example, iotest 33 using qemu-io -c "write -P 0xa 0x200 0x400"), but strtod() parses "08" as 8 rather than as an invalid octal number, so we know there are no clients that depend on octal. Our use of strtod() also means that "0x1.8k" would actually parse as 1536 (the fraction is 8/16), rather than 1843 (if the fraction were 8/10); but as this was not covered in the testsuite, I have no qualms forbidding hex fractions as invalid, so this patch declares that the use of fractions is only supported with decimal input, and enhances the testsuite to document that. Our previous use of strtod() meant that -1 parsed as a negative; now that we parse with strtoull(), negative values can wrap around modulo 2^64, so we have to explicitly check whether the user passed in a '-'; and make it consistent to also reject '-0'. This has the minor effect of treating negative values as EINVAL (with no change to endptr) rather than ERANGE (with endptr advanced to what was parsed), visible in the updated iotest output. We also had no testsuite coverage of "1.1e0k", which happened to parse under strtod() but is unlikely to occur in practice; as long as we are making things more robust, it is easy enough to reject the use of exponents in a strtod parse. The fix is done by breaking the parse into an integer prefix (no loss in precision), rejecting negative values (since we can no longer rely on strtod() to do that), determining if a decimal or hexadecimal parse was intended (with the new restriction that a fractional hex parse is not allowed), and where appropriate, using a floating point fractional parse (where we also scan to reject use of exponents in the fraction). The bulk of the patch is then updates to the testsuite to match our new precision, as well as adding new cases we reject (whether they were rejected or inadvertently accepted before). Signed-off-by: Eric Blake Reviewed-by: Daniel P. Berrang=C3=A9 --- Note that this approach differs from what has been attempted in the past; see the thread starting at https://lists.gnu.org/archive/html/qemu-devel/2019-12/msg00852.html That approach tried to parse both as strtoull and strtod and take whichever was longer, but that was harder to document. --- tests/test-cutils.c | 74 ++++++++++---------------- tests/test-keyval.c | 35 +++++++++---- tests/test-qemu-opts.c | 33 ++++++++---- util/cutils.c | 90 ++++++++++++++++++++++++-------- tests/qemu-iotests/049.out | 14 +++-- tests/qemu-iotests/178.out.qcow2 | 3 +- tests/qemu-iotests/178.out.raw | 3 +- 7 files changed, 158 insertions(+), 94 deletions(-) diff --git a/tests/test-cutils.c b/tests/test-cutils.c index 6d9802e00b1b..bad3a6099389 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1978,8 +1978,6 @@ static void test_qemu_strtosz_simple(void) g_assert_cmpint(err, =3D=3D, 0); g_assert_cmpint(res, =3D=3D, 12345); - /* Note: precision is 53 bits since we're parsing with strtod() */ - str =3D "9007199254740991"; /* 2^53-1 */ err =3D qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, =3D=3D, 0); @@ -1992,10 +1990,10 @@ static void test_qemu_strtosz_simple(void) g_assert_cmpint(res, =3D=3D, 0x20000000000000); g_assert(endptr =3D=3D str + 16); - str =3D "9007199254740993"; /* 2^53+1 FIXME loss of precision is a bug= */ + 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_cmpint(res, =3D=3D, 0x20000000000001); g_assert(endptr =3D=3D str + 16); str =3D "18446744073709549568"; /* 0xfffffffffffff800 (53 msbs set) */ @@ -2004,16 +2002,17 @@ static void test_qemu_strtosz_simple(void) g_assert_cmpint(res, =3D=3D, 0xfffffffffffff800); g_assert(endptr =3D=3D str + 20); - str =3D "18446744073709550591"; /* 0xfffffffffffffbff FIXME */ + str =3D "18446744073709550591"; /* 0xfffffffffffffbff */ 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, 0xfffffffffffffbff); g_assert(endptr =3D=3D str + 20); - /* - * FIXME 0xfffffffffffffe00..0xffffffffffffffff get rounded to - * 2^64, thus -ERANGE; see test_qemu_strtosz_erange() - */ + str =3D "18446744073709551615"; /* 0xffffffffffffffff */ + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, 0); + g_assert_cmpint(res, =3D=3D, 0xffffffffffffffff); + g_assert(endptr =3D=3D str + 20); } static void test_qemu_strtosz_hex(void) @@ -2166,45 +2165,43 @@ static void test_qemu_strtosz_invalid(void) g_assert(endptr =3D=3D str); /* Fractional values require scale larger than bytes */ - /* FIXME endptr shouldn't move on -EINVAL */ str =3D "1.1B"; err =3D qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, =3D=3D, -EINVAL); - g_assert(endptr =3D=3D str + 4); + g_assert(endptr =3D=3D str); - /* FIXME endptr shouldn't move on -EINVAL */ str =3D "1.1"; err =3D qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, =3D=3D, -EINVAL); - g_assert(endptr =3D=3D str + 3); + g_assert(endptr =3D=3D str); - /* FIXME we should reject floating point exponents */ + /* No floating point exponents */ str =3D "1.5e1k"; err =3D qemu_strtosz(str, &endptr, &res); - g_assert_cmpint(err, =3D=3D, 0); - g_assert_cmpint(res, =3D=3D, 15360); - g_assert(endptr =3D=3D str + 6); + g_assert_cmpint(err, =3D=3D, -EINVAL); + g_assert(endptr =3D=3D str); - /* FIXME we should reject floating point exponents */ str =3D "1.5E+0k"; err =3D qemu_strtosz(str, &endptr, &res); - g_assert_cmpint(err, =3D=3D, 0); - g_assert_cmpint(res, =3D=3D, 1536); - g_assert(endptr =3D=3D str + 7); + g_assert_cmpint(err, =3D=3D, -EINVAL); + g_assert(endptr =3D=3D str); - /* FIXME we should reject hex fractions */ + /* No hex fractions */ str =3D "0x1.8k"; err =3D qemu_strtosz(str, &endptr, &res); - g_assert_cmpint(err, =3D=3D, 0); - g_assert_cmpint(res, =3D=3D, 1536); - g_assert(endptr =3D=3D str + 6); + g_assert_cmpint(err, =3D=3D, -EINVAL); + g_assert(endptr =3D=3D str); - /* FIXME we reject all other attempts at negative, why not -0 */ + /* No negative values */ str =3D "-0"; err =3D qemu_strtosz(str, &endptr, &res); - g_assert_cmpint(err, =3D=3D, 0); - g_assert_cmpint(res, =3D=3D, 0); - g_assert(endptr =3D=3D str + 2); + g_assert_cmpint(err, =3D=3D, -EINVAL); + g_assert(endptr =3D=3D str); + + str =3D "-1"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, -EINVAL); + g_assert(endptr =3D=3D str); } static void test_qemu_strtosz_trailing(void) @@ -2263,22 +2260,7 @@ static void test_qemu_strtosz_erange(void) int err; uint64_t res =3D 0xbaadf00d; - str =3D "-1"; - err =3D qemu_strtosz(str, &endptr, &res); - g_assert_cmpint(err, =3D=3D, -ERANGE); - g_assert(endptr =3D=3D str + 2); - - str =3D "18446744073709550592"; /* 0xfffffffffffffc00 FIXME accept thi= s */ - 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 FIXME accept this */ - 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 */ + str =3D "18446744073709551616"; /* 2^64; see strtosz_simple for 2^64-1= */ err =3D qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, =3D=3D, -ERANGE); g_assert(endptr =3D=3D str + 20); diff --git a/tests/test-keyval.c b/tests/test-keyval.c index ee927fe4e427..e20c07cf3ea5 100644 --- a/tests/test-keyval.c +++ b/tests/test-keyval.c @@ -445,9 +445,9 @@ static void test_keyval_visit_size(void) visit_end_struct(v, NULL); visit_free(v); - /* Note: precision is 53 bits since we're parsing with strtod() */ + /* Note: full 64 bits of precision */ - /* Around limit of precision: 2^53-1, 2^53, 2^53+1 */ + /* Around double limit of precision: 2^53-1, 2^53, 2^53+1 */ qdict =3D keyval_parse("sz1=3D9007199254740991," "sz2=3D9007199254740992," "sz3=3D9007199254740993", @@ -460,22 +460,25 @@ static void test_keyval_visit_size(void) 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); + g_assert_cmphex(sz, =3D=3D, 0x20000000000001); 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 = */ + /* Close to signed integer limit 2^63 */ + qdict =3D keyval_parse("sz1=3D9223372036854775807," /* 7ffffffffffffff= f */ + "sz2=3D9223372036854775808," /* 8000000000000000 = */ + "sz3=3D9223372036854775809", /* 8000000000000001 = */ NULL, 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); + g_assert_cmphex(sz, =3D=3D, 0x7fffffffffffffff); visit_type_size(v, "sz2", &sz, &error_abort); - g_assert_cmphex(sz, =3D=3D, 0x7ffffffffffffc00); + g_assert_cmphex(sz, =3D=3D, 0x8000000000000000); + visit_type_size(v, "sz3", &sz, &error_abort); + g_assert_cmphex(sz, =3D=3D, 0x8000000000000001); visit_check_struct(v, &error_abort); visit_end_struct(v, NULL); visit_free(v); @@ -490,14 +493,26 @@ static void test_keyval_visit_size(void) 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, 0xfffffffffffffbff); + visit_check_struct(v, &error_abort); + visit_end_struct(v, NULL); + visit_free(v); + + /* Actual limit 2^64-1*/ + qdict =3D keyval_parse("sz1=3D18446744073709551615", /* ffffffffffffff= ff */ + NULL, 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, 0xffffffffffffffff); visit_check_struct(v, &error_abort); visit_end_struct(v, NULL); visit_free(v); /* Beyond limits */ qdict =3D keyval_parse("sz1=3D-1," - "sz2=3D18446744073709550592", /* fffffffffffffc00= */ + "sz2=3D18446744073709551616", /* 2^64 */ NULL, 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 8bbb17b1c726..6568e31a7229 100644 --- a/tests/test-qemu-opts.c +++ b/tests/test-qemu-opts.c @@ -654,9 +654,9 @@ 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); - /* Note: precision is 53 bits since we're parsing with strtod() */ + /* Note: full 64 bits of precision */ - /* Around limit of precision: 2^53-1, 2^53, 2^54 */ + /* Around double limit of precision: 2^53-1, 2^53, 2^53+1 */ opts =3D qemu_opts_parse(&opts_list_02, "size1=3D9007199254740991," "size2=3D9007199254740992," @@ -668,18 +668,21 @@ static void test_opts_parse_size(void) 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); + =3D=3D, 0x20000000000001); - /* Close to signed upper limit 0x7ffffffffffffc00 (53 msbs set) */ + /* Close to signed int limit: 2^63-1, 2^63, 2^63+1 */ opts =3D qemu_opts_parse(&opts_list_02, - "size1=3D9223372036854774784," /* 7ffffffffffff= c00 */ - "size2=3D9223372036854775295", /* 7ffffffffffff= dff */ + "size1=3D9223372036854775807," /* 7ffffffffffff= fff */ + "size2=3D9223372036854775808," /* 8000000000000= 000 */ + "size3=3D9223372036854775809", /* 8000000000000= 001 */ false, &error_abort); - g_assert_cmpuint(opts_count(opts), =3D=3D, 2); + g_assert_cmpuint(opts_count(opts), =3D=3D, 3); g_assert_cmphex(qemu_opt_get_size(opts, "size1", 1), - =3D=3D, 0x7ffffffffffffc00); + =3D=3D, 0x7fffffffffffffff); g_assert_cmphex(qemu_opt_get_size(opts, "size2", 1), - =3D=3D, 0x7ffffffffffffc00); + =3D=3D, 0x8000000000000000); + g_assert_cmphex(qemu_opt_get_size(opts, "size3", 1), + =3D=3D, 0x8000000000000001); /* Close to actual upper limit 0xfffffffffffff800 (53 msbs set) */ opts =3D qemu_opts_parse(&opts_list_02, @@ -690,14 +693,22 @@ static void test_opts_parse_size(void) g_assert_cmphex(qemu_opt_get_size(opts, "size1", 1), =3D=3D, 0xfffffffffffff800); g_assert_cmphex(qemu_opt_get_size(opts, "size2", 1), - =3D=3D, 0xfffffffffffff800); + =3D=3D, 0xfffffffffffffbff); + + /* Actual limit, 2^64-1 */ + opts =3D qemu_opts_parse(&opts_list_02, + "size1=3D18446744073709551615", /* ffffffffffff= ffff */ + false, &error_abort); + g_assert_cmpuint(opts_count(opts), =3D=3D, 1); + g_assert_cmphex(qemu_opt_get_size(opts, "size1", 1), + =3D=3D, 0xffffffffffffffff); /* 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 0b5073b33012..22d27b0fd21b 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -241,52 +241,100 @@ static int64_t suffix_mul(char suffix, int64_t unit) } /* - * Convert string to bytes, allowing either B/b for bytes, K/k for KB, - * M/m for MB, G/g for GB or T/t for TB. End pointer will be returned - * in *end, if not NULL. Return -ERANGE on overflow, and -EINVAL on - * other error. + * Convert size string to bytes. + * + * The size parsing supports the following syntaxes + * - 12345 - decimal, scale determined by @default_suffix and @unit + * - 12345{bBkKmMgGtTpPeE} - decimal, scale determined by suffix and @unit + * - 12345.678{kKmMgGtTpPeE} - decimal, scale determined by suffix, and + * fractional portion is truncated to byte + * - 0x7fEE - hexadecimal, unit determined by @default_suffix + * + * The following are intentionally not supported + * - octal, such as 08 + * - fractional hex, such as 0x1.8 + * - floating point exponents, such as 1e3 + * + * The end pointer will be returned in *end, if not NULL. If there is + * no fraction, the input can be decimal or hexadecimal; if there is a + * fraction, then the input must be decimal and there must be a suffix + * (possibly by @default_suffix) larger than Byte, and the fractional + * portion may suffer from precision loss or rounding. The input must + * be positive. + * + * Return -ERANGE on overflow (with *@end advanced), and -EINVAL on + * other error (with *@end left unchanged). */ static int do_strtosz(const char *nptr, const char **end, const char default_suffix, int64_t unit, uint64_t *result) { int retval; - const char *endptr; + const char *endptr, *f; unsigned char c; - int mul_required =3D 0; - double val, mul, integral, fraction; + bool mul_required =3D false; + uint64_t val; + int64_t mul; + double fraction =3D 0.0; - retval =3D qemu_strtod_finite(nptr, &endptr, &val); + /* Parse integral portion as decimal. */ + retval =3D qemu_strtou64(nptr, &endptr, 10, &val); if (retval) { goto out; } - fraction =3D modf(val, &integral); - if (fraction !=3D 0) { - mul_required =3D 1; + if (memchr(nptr, '-', endptr - nptr) !=3D NULL) { + endptr =3D nptr; + retval =3D -EINVAL; + goto out; + } + if (val =3D=3D 0 && (*endptr =3D=3D 'x' || *endptr =3D=3D 'X')) { + /* Input looks like hex, reparse, and insist on no fraction. */ + retval =3D qemu_strtou64(nptr, &endptr, 16, &val); + if (retval) { + goto out; + } + if (*endptr =3D=3D '.') { + endptr =3D nptr; + retval =3D -EINVAL; + goto out; + } + } else if (*endptr =3D=3D '.') { + /* + * Input looks like a fraction. Make sure even 1.k works + * without fractional digits. If we see an exponent, treat + * the entire input as invalid instead. + */ + f =3D endptr; + retval =3D qemu_strtod_finite(f, &endptr, &fraction); + if (retval) { + fraction =3D 0.0; + endptr++; + } else if (memchr(f, 'e', endptr - f) || memchr(f, 'E', endptr - f= )) { + endptr =3D nptr; + retval =3D -EINVAL; + goto out; + } else if (fraction !=3D 0) { + mul_required =3D true; + } } c =3D *endptr; mul =3D suffix_mul(c, unit); - if (mul >=3D 0) { + if (mul > 0) { endptr++; } else { mul =3D suffix_mul(default_suffix, unit); - assert(mul >=3D 0); + assert(mul > 0); } if (mul =3D=3D 1 && mul_required) { + endptr =3D nptr; retval =3D -EINVAL; goto out; } - /* - * Values near UINT64_MAX overflow to 2**64 when converting to double - * precision. Compare against the maximum representable double precis= ion - * value below 2**64, computed as "the next value after 2**64 (0x1p64)= in - * the direction of 0". - */ - if ((val * mul > nextafter(0x1p64, 0)) || val < 0) { + if (val > (UINT64_MAX - ((uint64_t) (fraction * mul))) / mul) { retval =3D -ERANGE; goto out; } - *result =3D val * mul; + *result =3D val * mul + (uint64_t) (fraction * mul); retval =3D 0; out: diff --git a/tests/qemu-iotests/049.out b/tests/qemu-iotests/049.out index b1d8fd9107ef..01f7b1f2408b 100644 --- a/tests/qemu-iotests/049.out +++ b/tests/qemu-iotests/049.out @@ -92,16 +92,22 @@ Formatting 'TEST_DIR/t.qcow2', fmt=3Dqcow2 cluster_size= =3D65536 extended_l2=3Doff comp =3D=3D 3. Invalid sizes =3D=3D qemu-img create -f qcow2 TEST_DIR/t.qcow2 -- -1024 -qemu-img: Invalid image size specified. Must be between 0 and 922337203685= 4775807. +qemu-img: Invalid image size specified. You may use k, M, G, T, P or E suf= fixes for +qemu-img: kilobytes, megabytes, gigabytes, terabytes, petabytes and exabyt= es. qemu-img create -f qcow2 -o size=3D-1024 TEST_DIR/t.qcow2 -qemu-img: TEST_DIR/t.qcow2: Value '-1024' is out of range for parameter 's= ize' +qemu-img: TEST_DIR/t.qcow2: Parameter 'size' expects a non-negative number= below 2^64 +Optional suffix k, M, G, T, P or E means kilo-, mega-, giga-, tera-, peta- +and exabytes, respectively. qemu-img create -f qcow2 TEST_DIR/t.qcow2 -- -1k -qemu-img: Invalid image size specified. Must be between 0 and 922337203685= 4775807. +qemu-img: Invalid image size specified. You may use k, M, G, T, P or E suf= fixes for +qemu-img: kilobytes, megabytes, gigabytes, terabytes, petabytes and exabyt= es. qemu-img create -f qcow2 -o size=3D-1k TEST_DIR/t.qcow2 -qemu-img: TEST_DIR/t.qcow2: Value '-1k' is out of range for parameter 'siz= e' +qemu-img: TEST_DIR/t.qcow2: Parameter 'size' expects a non-negative number= below 2^64 +Optional suffix k, M, G, T, P or E means kilo-, mega-, giga-, tera-, peta- +and exabytes, respectively. qemu-img create -f qcow2 TEST_DIR/t.qcow2 -- 1kilobyte qemu-img: Invalid image size specified. You may use k, M, G, T, P or E suf= fixes for diff --git a/tests/qemu-iotests/178.out.qcow2 b/tests/qemu-iotests/178.out.= qcow2 index fe193fd5f4ff..0d51fe401ecb 100644 --- a/tests/qemu-iotests/178.out.qcow2 +++ b/tests/qemu-iotests/178.out.qcow2 @@ -13,7 +13,8 @@ qemu-img: Invalid option list: , qemu-img: Invalid parameter 'snapshot.foo' qemu-img: Failed in parsing snapshot param 'snapshot.foo=3Dbar' qemu-img: --output must be used with human or json as argument. -qemu-img: Invalid image size specified. Must be between 0 and 922337203685= 4775807. +qemu-img: Invalid image size specified. You may use k, M, G, T, P or E suf= fixes for +qemu-img: kilobytes, megabytes, gigabytes, terabytes, petabytes and exabyt= es. qemu-img: Unknown file format 'foo' =3D=3D Size calculation for a new file (human) =3D=3D diff --git a/tests/qemu-iotests/178.out.raw b/tests/qemu-iotests/178.out.raw index 445e460fad99..116241ddef2f 100644 --- a/tests/qemu-iotests/178.out.raw +++ b/tests/qemu-iotests/178.out.raw @@ -13,7 +13,8 @@ qemu-img: Invalid option list: , qemu-img: Invalid parameter 'snapshot.foo' qemu-img: Failed in parsing snapshot param 'snapshot.foo=3Dbar' qemu-img: --output must be used with human or json as argument. -qemu-img: Invalid image size specified. Must be between 0 and 922337203685= 4775807. +qemu-img: Invalid image size specified. You may use k, M, G, T, P or E suf= fixes for +qemu-img: kilobytes, megabytes, gigabytes, terabytes, petabytes and exabyt= es. qemu-img: Unknown file format 'foo' =3D=3D Size calculation for a new file (human) =3D=3D --=20 2.30.1 From nobody Sun Oct 5 17:38:41 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1613076346; cv=none; d=zohomail.com; s=zohoarc; b=Oj10sexw5e8nY+BvSVujs7bIw7BjwjXTOtDzlpr1Sg3OglHf71HoaGHby9pugfFdOIkbkyGO0QZjKHCVaHeCqMmVklyv1Aux8buOgNYmzRaydiNndqpSc28bEfTsiMyTOJhX6vYblOXD7UOJtZ/wmILrkLCCWihKMaFwqoHHpjk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1613076346; h=Content-Type: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=Wx940ZCaWVZE/SPo08+hKc6m3jrEP+FYeAiIba80nYk=; b=QYiYPU3DNr+ywTa9K8q89UqjbgVzrn+NuB1BDH+Fg2nPVq4Ej4ZAHjYxMaE4pyTt1z5Kswhez84qSTSf8u1gNcLEZeMajf8Tzys2WbENsGNUAHbAxx/lXzdH8iTyRS2YXYXuiSpItSuSUhSAL4jiLWsJ/HhC8rC9MnhZC/dRvUQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1613076346295356.2764660314783; Thu, 11 Feb 2021 12:45:46 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-195-FlLrJ16oPqqnNCjnkPFpUA-1; Thu, 11 Feb 2021 15:45:42 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 703238749AC; Thu, 11 Feb 2021 20:45:34 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 705C31A86A; Thu, 11 Feb 2021 20:45:30 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id B15DC4A7C6; Thu, 11 Feb 2021 20:45:24 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 11BKikOc024221 for ; Thu, 11 Feb 2021 15:44:46 -0500 Received: by smtp.corp.redhat.com (Postfix) id BFC311002388; Thu, 11 Feb 2021 20:44:46 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-150.phx2.redhat.com [10.3.114.150]) by smtp.corp.redhat.com (Postfix) with ESMTP id 20A2B10013D7; Thu, 11 Feb 2021 20:44:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613076345; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=Wx940ZCaWVZE/SPo08+hKc6m3jrEP+FYeAiIba80nYk=; b=hSneLWowR2qIjHFtsv/XAk2Gv8ZkLwBCUgN2JyJ9nLjEFBLyp/cuN3uNFuDoR6Zie9J3Yq 8GhKl9fYuQhytbv+BD/UwihbxxskjzgI4FNWJ4TOdGl+NXH/psHEZybOSBgVRNas7DcuhN 7hFeHaMcvySqb56aGFolB7MNhZbf5uY= X-MC-Unique: FlLrJ16oPqqnNCjnkPFpUA-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PATCH v2 3/4] utils: Deprecate hex-with-suffix sizes Date: Thu, 11 Feb 2021 14:44:37 -0600 Message-Id: <20210211204438.1184395-4-eblake@redhat.com> In-Reply-To: <20210211204438.1184395-1-eblake@redhat.com> References: <20210211204438.1184395-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Cc: vsementsov@virtuozzo.com, qemu-block@nongnu.org, "reviewer:Incompatible changes" , tao3.xu@intel.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Supporting '0x20M' looks odd, particularly since we have a 'B' suffix that is ambiguous for bytes, as well as a less-frequently-used 'E' suffix for extremely large exibytes. In practice, people using hex inputs are specifying values in bytes (and would have written 0x2000000, or possibly relied on default_suffix in the case of qemu_strtosz_MiB), and the use of scaling suffixes makes the most sense for inputs in decimal (where the user would write 32M). But rather than outright dropping support for hex-with-suffix, let's follow our deprecation policy. Sadly, since qemu_strtosz() does not have an Err** parameter, and plumbing that in would be a much larger task, we instead go with just directly emitting the deprecation warning to stderr. Signed-off-by: Eric Blake Reviewed-by: Daniel P. Berrang=C3=A9 Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- docs/system/deprecated.rst | 8 ++++++++ util/cutils.c | 10 +++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/docs/system/deprecated.rst b/docs/system/deprecated.rst index 2fcac7861e07..113c2e933f1b 100644 --- a/docs/system/deprecated.rst +++ b/docs/system/deprecated.rst @@ -146,6 +146,14 @@ library enabled as a cryptography provider. Neither the ``nettle`` library, or the built-in cryptography provider are supported on FIPS enabled hosts. +hexadecimal sizes with scaling multipliers (since 6.0) +'''''''''''''''''''''''''''''''''''''''''''''''''''''' + +Input parameters that take a size value should only use a size suffix +(such as 'k' or 'M') when the base is written in decimal, and not when +the value is hexadecimal. That is, '0x20M' is deprecated, and should +be written either as '32M' or as '0x2000000'. + QEMU Machine Protocol (QMP) commands ------------------------------------ diff --git a/util/cutils.c b/util/cutils.c index 22d27b0fd21b..6a8a175e0d71 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -250,6 +250,9 @@ static int64_t suffix_mul(char suffix, int64_t unit) * fractional portion is truncated to byte * - 0x7fEE - hexadecimal, unit determined by @default_suffix * + * The following cause a deprecation warning, and may be removed in the fu= ture + * - 0xabc{kKmMgGtTpP} - hex with scaling suffix + * * The following are intentionally not supported * - octal, such as 08 * - fractional hex, such as 0x1.8 @@ -272,7 +275,7 @@ static int do_strtosz(const char *nptr, const char **en= d, int retval; const char *endptr, *f; unsigned char c; - bool mul_required =3D false; + bool mul_required =3D false, hex =3D false; uint64_t val; int64_t mul; double fraction =3D 0.0; @@ -298,6 +301,7 @@ static int do_strtosz(const char *nptr, const char **en= d, retval =3D -EINVAL; goto out; } + hex =3D true; } else if (*endptr =3D=3D '.') { /* * Input looks like a fraction. Make sure even 1.k works @@ -320,6 +324,10 @@ static int do_strtosz(const char *nptr, const char **e= nd, c =3D *endptr; mul =3D suffix_mul(c, unit); if (mul > 0) { + if (hex) { + warn_report("Using a multiplier suffix on hex numbers " + "is deprecated: %s", nptr); + } endptr++; } else { mul =3D suffix_mul(default_suffix, unit); --=20 2.30.1 From nobody Sun Oct 5 17:38:41 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1613076362; cv=none; d=zohomail.com; s=zohoarc; b=dwIaF6qvHGkQ8JlXpt5AqSFWhQYIJq35YU1FZm9Ndc48TwuMsbxfFCXSY+P3hkWTjbkXdulc2v5g0Qq0R7196qQdjnOunom3z0W0M0zCu0yDCXtVuIPEeDkZnT4ovR6CRdTWo7XZfbXNfMzgUC4N2NpeDgEitLGr0kYXCZ4J3Xs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1613076362; h=Content-Type: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=9t4IkoAmXiHcfKk4F3GTc5helKS3pdW0WkOh9WuI2hA=; b=YXN5ZcGvXgOrr0EdP1ACWud7KBmA6YrfzMj8+Asv6n7SGGPDzU0j3DgOPq9cOBmKle7QxIb1xi9pOVPCNh1KFti21m0aohIInD60A1Yc/Kzxt0V0y4i8KPy8OuHqYeTtl1XroCtG3/auFGlEfy6ej2xbBdtuznklK+MFXmWQYQU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 16130763622211012.5046770642597; Thu, 11 Feb 2021 12:46:02 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-553-hF7fw9CFN1KvL1p2KBS6NQ-1; Thu, 11 Feb 2021 15:45:56 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 491F3874981; Thu, 11 Feb 2021 20:45:50 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1AACC62467; Thu, 11 Feb 2021 20:45:50 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id A1F7118095CA; Thu, 11 Feb 2021 20:45:49 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 11BKil0K024226 for ; Thu, 11 Feb 2021 15:44:47 -0500 Received: by smtp.corp.redhat.com (Postfix) id A615410016F5; Thu, 11 Feb 2021 20:44:47 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-150.phx2.redhat.com [10.3.114.150]) by smtp.corp.redhat.com (Postfix) with ESMTP id 097DD10013D7; Thu, 11 Feb 2021 20:44:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613076359; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=9t4IkoAmXiHcfKk4F3GTc5helKS3pdW0WkOh9WuI2hA=; b=OQAP9RqMualj15AQ2cAY6ywX+qnsb+4leKbSMErLSEKORZ6rOBGavqjziRx2oh/iA6+88V 29eKeZUjmRjaPZSpYtRu5uCzVSHwBtKl2JQ0OoexsXLJFbf5imEk6HNerJJKQG69gmQKDc DB7vb4lKkeL8VEIyDjdoKhhlCfcvYMg= X-MC-Unique: hF7fw9CFN1KvL1p2KBS6NQ-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PATCH v2 4/4] utils: Deprecate inexact fractional suffix sizes Date: Thu, 11 Feb 2021 14:44:38 -0600 Message-Id: <20210211204438.1184395-5-eblake@redhat.com> In-Reply-To: <20210211204438.1184395-1-eblake@redhat.com> References: <20210211204438.1184395-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Cc: vsementsov@virtuozzo.com, qemu-block@nongnu.org, "reviewer:Incompatible changes" , tao3.xu@intel.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" The value '1.1k' is inexact; 1126.4 bytes is not possible, so we happen to truncate it to 1126. Our use of fractional sizes is intended for convenience, but when a user specifies a fraction that is not a clean translation to binary, truncating/rounding behind their backs can cause confusion. Better is to deprecate inexact values, which still leaves '1.5k' as valid, but alerts the user to spell out their values as a precise byte number in cases where they are currently being rounded. Note that values like '0.1G' in the testsuite need adjustment as a result. Since qemu_strtosz() does not have an Err** parameter, and plumbing that in would be a much larger task, we instead go with just directly emitting the deprecation warning to stderr. Signed-off-by: Eric Blake --- I'm not a fan of this patch, but am proposing it for discussion purposes. --- docs/system/deprecated.rst | 9 +++++++++ tests/test-cutils.c | 6 +++--- tests/test-keyval.c | 4 ++-- tests/test-qemu-opts.c | 4 ++-- util/cutils.c | 9 +++++++-- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/docs/system/deprecated.rst b/docs/system/deprecated.rst index 113c2e933f1b..2c9cb849eec5 100644 --- a/docs/system/deprecated.rst +++ b/docs/system/deprecated.rst @@ -154,6 +154,15 @@ Input parameters that take a size value should only us= e a size suffix the value is hexadecimal. That is, '0x20M' is deprecated, and should be written either as '32M' or as '0x2000000'. +inexact sizes via scaled fractions (since 6.0) +'''''''''''''''''''''''''''''''''''''''''''''' + +Input parameters that take a size value should only use a fractional +size (such as '1.5M') that will result in an exact byte value. The +use of inexact values (such as '1.1M') that require truncation or +rounding is deprecated, and you should instead consider writing your +unusual size in bytes (here, '1153433' or '1153434' as desired). + QEMU Machine Protocol (QMP) commands ------------------------------------ diff --git a/tests/test-cutils.c b/tests/test-cutils.c index bad3a6099389..c6c33866277b 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -2124,11 +2124,11 @@ static void test_qemu_strtosz_float(void) g_assert_cmpint(res, =3D=3D, 1024); g_assert(endptr =3D=3D str + 3); - /* For convenience, we permit values that are not byte-exact */ - str =3D "12.345M"; + /* Fractional values should still be byte-exact */ + str =3D "12.125M"; err =3D qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, =3D=3D, 0); - g_assert_cmpint(res, =3D=3D, (uint64_t) (12.345 * MiB)); + g_assert_cmpint(res, =3D=3D, (uint64_t) (12.125 * MiB)); g_assert(endptr =3D=3D str + 7); } diff --git a/tests/test-keyval.c b/tests/test-keyval.c index e20c07cf3ea5..7a45c22942e6 100644 --- a/tests/test-keyval.c +++ b/tests/test-keyval.c @@ -525,7 +525,7 @@ static void test_keyval_visit_size(void) visit_free(v); /* Suffixes */ - qdict =3D keyval_parse("sz1=3D8b,sz2=3D1.5k,sz3=3D2M,sz4=3D0.1G,sz5=3D= 16777215T", + qdict =3D keyval_parse("sz1=3D8b,sz2=3D1.5k,sz3=3D2M,sz4=3D0.125G,sz5= =3D16777215T", NULL, NULL, &error_abort); v =3D qobject_input_visitor_new_keyval(QOBJECT(qdict)); qobject_unref(qdict); @@ -537,7 +537,7 @@ static void test_keyval_visit_size(void) visit_type_size(v, "sz3", &sz, &error_abort); g_assert_cmphex(sz, =3D=3D, 2 * MiB); visit_type_size(v, "sz4", &sz, &error_abort); - g_assert_cmphex(sz, =3D=3D, GiB / 10); + g_assert_cmphex(sz, =3D=3D, GiB / 8); visit_type_size(v, "sz5", &sz, &error_abort); g_assert_cmphex(sz, =3D=3D, 16777215ULL * TiB); visit_check_struct(v, &error_abort); diff --git a/tests/test-qemu-opts.c b/tests/test-qemu-opts.c index 6568e31a7229..549e994938fe 100644 --- a/tests/test-qemu-opts.c +++ b/tests/test-qemu-opts.c @@ -720,10 +720,10 @@ static void test_opts_parse_size(void) g_assert_cmphex(qemu_opt_get_size(opts, "size1", 0), =3D=3D, 8); g_assert_cmphex(qemu_opt_get_size(opts, "size2", 0), =3D=3D, 1536); g_assert_cmphex(qemu_opt_get_size(opts, "size3", 0), =3D=3D, 2 * MiB); - opts =3D qemu_opts_parse(&opts_list_02, "size1=3D0.1G,size2=3D16777215= T", + opts =3D qemu_opts_parse(&opts_list_02, "size1=3D0.125G,size2=3D167772= 15T", false, &error_abort); g_assert_cmpuint(opts_count(opts), =3D=3D, 2); - g_assert_cmphex(qemu_opt_get_size(opts, "size1", 0), =3D=3D, GiB / 10); + g_assert_cmphex(qemu_opt_get_size(opts, "size1", 0), =3D=3D, GiB / 8); g_assert_cmphex(qemu_opt_get_size(opts, "size2", 0), =3D=3D, 16777215U= LL * TiB); /* Beyond limit with suffix */ diff --git a/util/cutils.c b/util/cutils.c index 6a8a175e0d71..1154b9de131a 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -246,12 +246,13 @@ static int64_t suffix_mul(char suffix, int64_t unit) * The size parsing supports the following syntaxes * - 12345 - decimal, scale determined by @default_suffix and @unit * - 12345{bBkKmMgGtTpPeE} - decimal, scale determined by suffix and @unit - * - 12345.678{kKmMgGtTpPeE} - decimal, scale determined by suffix, and - * fractional portion is truncated to byte + * - 12345.678{kKmMgGtTpPeE} - decimal, scale determined by suffix, if + * fractional portion is exact * - 0x7fEE - hexadecimal, unit determined by @default_suffix * * The following cause a deprecation warning, and may be removed in the fu= ture * - 0xabc{kKmMgGtTpP} - hex with scaling suffix + * - 12345.678{kKmMgGtTpPeE} - decimal with inexact fraction that caused t= runcation * * The following are intentionally not supported * - octal, such as 08 @@ -342,6 +343,10 @@ static int do_strtosz(const char *nptr, const char **e= nd, retval =3D -ERANGE; goto out; } + if (mul_required && fraction * mul !=3D (uint64_t) (fraction * mul)) { + warn_report("Using a fractional size that is not an exact byte " + "multiple is deprecated: %s", nptr); + } *result =3D val * mul + (uint64_t) (fraction * mul); retval =3D 0; --=20 2.30.1