From nobody Sun Oct 5 21:10:51 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=1589819762; cv=none; d=zohomail.com; s=zohoarc; b=Gl4iFRzlEZuWbzpHljAzys0mxSCkKMqvIf2aGWEEk/0KNI02kEIyZHUGsXaLKZ03++otTsNE55HUVRosKMVyS1019QTRTi6Du7r8A7fbCYq9a8/40+aoJAQiH95//Ww10rE9Sh4WJSh0Ck6Ioc32h/M5AA/TdpX0KZmungliHUI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1589819762; 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=oii076EEtIpLbw2zbK/UeFHcdIxo/eOwUrg1PeUKOQo=; b=LM3bMT/dSaod8XntMIyaagaZargPrYFXJs3ojCwyOK81SFrwtsHG5YSaDrWN1/wgAzsIqf2RCgn6gNv1H4VFpKWumqI+H8Eu398wDA91F4KEu6+1kG7Op8va03Z5kLQO9yVcf6YqY3X2pa0P/FvTusBGfqQ/glUSYPeO5ejR0Eg= 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 158981976227528.16254060163294; Mon, 18 May 2020 09:36:02 -0700 (PDT) Received: from localhost ([::1]:51762 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jaikD-0001Zj-0Y for importer@patchew.org; Mon, 18 May 2020 12:36:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58722) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jaign-0005IH-1s for qemu-devel@nongnu.org; Mon, 18 May 2020 12:32:29 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:37220 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jaigk-0003DS-N0 for qemu-devel@nongnu.org; Mon, 18 May 2020 12:32:28 -0400 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-335-uUGmwDcHMvmY0_j0MYoOTA-1; Mon, 18 May 2020 12:32:22 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D311B107ACF7; Mon, 18 May 2020 16:32:21 +0000 (UTC) Received: from blue.redhat.com (ovpn-112-88.phx2.redhat.com [10.3.112.88]) by smtp.corp.redhat.com (Postfix) with ESMTP id 52F9A5D9DC; Mon, 18 May 2020 16:32:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1589819545; 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=oii076EEtIpLbw2zbK/UeFHcdIxo/eOwUrg1PeUKOQo=; b=Chq74P1oUXwQk+Az6fyM0Myzmw2/i6DJWt29Sj4VCx/K0xvSvYPyR9po66bzklGeSLhajK uFYLwVaC+URSP3EKzWVLkEKvFc5c/4QRIO/BQxa1CukoilGMIbd3NQ7zrp2q0AJORsTsSk Ks64ouFkDHCWPJ6JaelRXcHQtJLzMuQ= X-MC-Unique: uUGmwDcHMvmY0_j0MYoOTA-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 2/6] qemu_img: add cvtnum_full to print error reports Date: Mon, 18 May 2020 11:32:14 -0500 Message-Id: <20200518163218.649412-3-eblake@redhat.com> In-Reply-To: <20200518163218.649412-1-eblake@redhat.com> References: <20200518163218.649412-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 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=207.211.31.81; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/17 22:52:27 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN 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 , Eyal Moscovici , "open list:Block layer core" , 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" From: Eyal Moscovici All calls to cvtnum check the return value and print the same error message more or less. And so error reporting moved to cvtnum_full to reduce code duplication and provide a single error message. Additionally, cvtnum now wraps cvtnum_full with the existing default range of 0 to MAX_INT64. Acked-by: Mark Kanda Signed-off-by: Eyal Moscovici Message-Id: <20200513133629.18508-2-eyal.moscovici@oracle.com> Reviewed-by: Eric Blake [eblake: fix printf formatting, avoid trailing space, change error wording, reformat commit message] Signed-off-by: Eric Blake --- qemu-img.c | 74 +++++++++++++++++--------------------- tests/qemu-iotests/049.out | 8 ++--- 2 files changed, 37 insertions(+), 45 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index 6a4327aaba56..5d824fc15f39 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -470,19 +470,31 @@ static int add_old_style_options(const char *fmt, Qem= uOpts *opts, return 0; } -static int64_t cvtnum(const char *s) +static int64_t cvtnum_full(const char *name, const char *value, int64_t mi= n, + int64_t max) { int err; - uint64_t value; + uint64_t res; - err =3D qemu_strtosz(s, NULL, &value); - if (err < 0) { + err =3D qemu_strtosz(value, NULL, &res); + if (err < 0 && err !=3D -ERANGE) { + error_report("Invalid %s specified. You may use " + "k, M, G, T, P or E suffixes for", name); + error_report("kilobytes, megabytes, gigabytes, terabytes, " + "petabytes and exabytes."); return err; } - if (value > INT64_MAX) { + if (err =3D=3D -ERANGE || res > max || res < min) { + error_report("Invalid %s specified. Must be between %" PRId64 + " and %" PRId64 ".", name, min, max); return -ERANGE; } - return value; + return res; +} + +static int64_t cvtnum(const char *name, const char *value) +{ + return cvtnum_full(name, value, 0, INT64_MAX); } static int img_create(int argc, char **argv) @@ -572,16 +584,8 @@ static int img_create(int argc, char **argv) if (optind < argc) { int64_t sval; - sval =3D cvtnum(argv[optind++]); + sval =3D cvtnum("image size", argv[optind++]); if (sval < 0) { - if (sval =3D=3D -ERANGE) { - error_report("Image size must be less than 8 EiB!"); - } else { - error_report("Invalid image size specified! You may use k,= M, " - "G, T, P or E suffixes for "); - error_report("kilobytes, megabytes, gigabytes, terabytes, " - "petabytes and exabytes."); - } goto fail; } img_size =3D (uint64_t)sval; @@ -2187,8 +2191,10 @@ static int img_convert(int argc, char **argv) { int64_t sval; - sval =3D cvtnum(optarg); - if (sval < 0 || !QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) || + sval =3D cvtnum("buffer size for sparse output", optarg); + if (sval < 0) { + goto fail_getopt; + } else if (!QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) || sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) { error_report("Invalid buffer size for sparse output specif= ied. " "Valid sizes are multiples of %llu up to %llu. Select " @@ -4291,9 +4297,8 @@ static int img_bench(int argc, char **argv) break; case 'o': { - offset =3D cvtnum(optarg); + offset =3D cvtnum("offset", optarg); if (offset < 0) { - error_report("Invalid offset specified"); return 1; } break; @@ -4306,9 +4311,8 @@ static int img_bench(int argc, char **argv) { int64_t sval; - sval =3D cvtnum(optarg); - if (sval < 0 || sval > INT_MAX) { - error_report("Invalid buffer size specified"); + sval =3D cvtnum_full("buffer size", optarg, 0, INT_MAX); + if (sval < 0) { return 1; } @@ -4319,9 +4323,8 @@ static int img_bench(int argc, char **argv) { int64_t sval; - sval =3D cvtnum(optarg); - if (sval < 0 || sval > INT_MAX) { - error_report("Invalid step size specified"); + sval =3D cvtnum_full("step_size", optarg, 0, INT_MAX); + if (sval < 0) { return 1; } @@ -4491,10 +4494,9 @@ static int img_dd_bs(const char *arg, { int64_t res; - res =3D cvtnum(arg); + res =3D cvtnum_full("bs", arg, 1, INT_MAX); - if (res <=3D 0 || res > INT_MAX) { - error_report("invalid number: '%s'", arg); + if (res < 0) { return 1; } in->bsz =3D out->bsz =3D res; @@ -4506,10 +4508,9 @@ static int img_dd_count(const char *arg, struct DdIo *in, struct DdIo *out, struct DdInfo *dd) { - dd->count =3D cvtnum(arg); + dd->count =3D cvtnum("count", arg); if (dd->count < 0) { - error_report("invalid number: '%s'", arg); return 1; } @@ -4538,10 +4539,9 @@ static int img_dd_skip(const char *arg, struct DdIo *in, struct DdIo *out, struct DdInfo *dd) { - in->offset =3D cvtnum(arg); + in->offset =3D cvtnum("skip", arg); if (in->offset < 0) { - error_report("invalid number: '%s'", arg); return 1; } @@ -4923,16 +4923,8 @@ static int img_measure(int argc, char **argv) { int64_t sval; - sval =3D cvtnum(optarg); + sval =3D cvtnum("image size", optarg); if (sval < 0) { - if (sval =3D=3D -ERANGE) { - error_report("Image size must be less than 8 EiB!"); - } else { - error_report("Invalid image size specified! You may us= e " - "k, M, G, T, P or E suffixes for "); - error_report("kilobytes, megabytes, gigabytes, terabyt= es, " - "petabytes and exabytes."); - } goto out; } img_size =3D (uint64_t)sval; diff --git a/tests/qemu-iotests/049.out b/tests/qemu-iotests/049.out index a5cfba175626..c54ae21b868a 100644 --- a/tests/qemu-iotests/049.out +++ b/tests/qemu-iotests/049.out @@ -92,19 +92,19 @@ Formatting 'TEST_DIR/t.qcow2', fmt=3Dqcow2 size=3D16492= 67441664 cluster_size=3D65536 l =3D=3D 3. Invalid sizes =3D=3D qemu-img create -f qcow2 TEST_DIR/t.qcow2 -- -1024 -qemu-img: Image size must be less than 8 EiB! +qemu-img: Invalid image size specified. Must be between 0 and 922337203685= 4775807. 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 create -f qcow2 TEST_DIR/t.qcow2 -- -1k -qemu-img: Image size must be less than 8 EiB! +qemu-img: Invalid image size specified. Must be between 0 and 922337203685= 4775807. 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 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 +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=3D1kilobyte TEST_DIR/t.qcow2 @@ -113,7 +113,7 @@ 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 -- foobar -qemu-img: Invalid image size specified! You may use k, M, G, T, P or E suf= fixes for +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=3Dfoobar TEST_DIR/t.qcow2 --=20 2.26.2