From nobody Fri Nov 14 16:59:42 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=oracle.com ARC-Seal: i=1; a=rsa-sha256; t=1588801017; cv=none; d=zohomail.com; s=zohoarc; b=Nv6ROUg9tgq9ih/gCZDrQPvbupcyVxBuapOWsvjzCD05i0PS633bLW1wuVeh9LEVUB8i+8Aijr0sWoVOytBwTGNmbDeHf1W7i0MLstA5T359e+quOQj464A+3YR613kMmIGT6n3VeNEJfxaCi93LFvRNDi1Z5i8wteC++wgZfZU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1588801017; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject; bh=KfZMoZ7bnwZv2BOgcPIdtmUUQ0+cdV+ar33s6cn9D04=; b=T1Q2SyHl27X9NvNt0bKAATGkpMRv+4G0qP1r75rQxCJChfnsIF+lYUpmnHV/7zr5CIzNBKWhycDiG8UZHruykr8tHFA/8YW7YanGuemjSSEy3zsVspaJWsIz5q9Gr+NShXw3vRAvxWMyNx6OM+UVh2TQYWGGw0S1KmWs+PdzU1k= 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 1588801017428364.0294745323366; Wed, 6 May 2020 14:36:57 -0700 (PDT) Received: from localhost ([::1]:59112 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jWRiq-0008Ou-2g for importer@patchew.org; Wed, 06 May 2020 17:36:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50726) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jWRhR-0006wz-V6; Wed, 06 May 2020 17:35:30 -0400 Received: from aserp2120.oracle.com ([141.146.126.78]:50436) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jWRhQ-0007HT-J0; Wed, 06 May 2020 17:35:29 -0400 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 046LJG2N099741; Wed, 6 May 2020 21:35:26 GMT Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by aserp2120.oracle.com with ESMTP id 30usgq3y8u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 06 May 2020 21:35:26 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 046LHnku039740; Wed, 6 May 2020 21:35:25 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userp3030.oracle.com with ESMTP id 30t1r8ss1r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 06 May 2020 21:35:25 +0000 Received: from abhmp0009.oracle.com (abhmp0009.oracle.com [141.146.116.15]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id 046LZONI017970; Wed, 6 May 2020 21:35:24 GMT Received: from localhost.localdomain (/10.74.121.228) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 06 May 2020 14:35:24 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2020-01-29; bh=KfZMoZ7bnwZv2BOgcPIdtmUUQ0+cdV+ar33s6cn9D04=; b=JPXYcFi3KIh2jVZ7A3U7Zf2ZiOQkbRX2AKnYqKDAQU2WUQmo90ZdgQ+N8P7+NMOeF4RW r+D0LN0G0XcgU3s4573RZ1WMzMDZ7D526gekj6ih6z9qBv36MV4hSSxqAYo2qzDiVk+t O628XT5+TldekmLPwTHWBkzSugvdWDRJXLPr7w8uv9/t2uWVAagTNOeoqt7Sfx9rozfY +iKgJCKB9XHsK9PlYTtBNRg/+TwM83haKAN/xGjGhnDQz5ypWiStIyTU2oAr8robbsUN 2YRaDLAVmSjr86M6DzfNKTNv0N8rOwCt4rb5rHbDT/wBB0xr1Lj5lYy+jKFF7+Pc8NK7 Ow== From: Eyal Moscovici To: Subject: [PATCH v2 2/5] qemu_img: add error report to cvtnum Date: Thu, 7 May 2020 00:34:56 +0300 Message-Id: <20200506213459.44743-3-eyal.moscovici@oracle.com> X-Mailer: git-send-email 2.17.2 (Apple Git-113) In-Reply-To: <20200506213459.44743-1-eyal.moscovici@oracle.com> References: <59b0896d-85ad-08b5-fcc1-36adad7501a4@redhat.com> <20200506213459.44743-1-eyal.moscovici@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9613 signatures=668687 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 adultscore=0 suspectscore=0 spamscore=0 mlxlogscore=999 malwarescore=0 phishscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2005060172 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9613 signatures=668687 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 impostorscore=0 mlxscore=0 priorityscore=1501 lowpriorityscore=0 malwarescore=0 clxscore=1015 mlxlogscore=999 spamscore=0 adultscore=0 bulkscore=0 phishscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2005060172 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=141.146.126.78; envelope-from=eyal.moscovici@oracle.com; helo=aserp2120.oracle.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/06 17:35:26 X-ACL-Warn: Detected OS = Linux 3.x [generic] [fuzzy] X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 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_MED=-2.3, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=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 , qemu-block@nongnu.org, qemu-devel@nongnu.org, Max Reitz , liran.alon@oracle.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" All calls to cvtnum check the return value and print the same error message= more or less. And so error reporting moved to cvtnum to reduce duplicate code and provide a single error message. Acked-by: Mark Kanda Signed-off-by: Eyal Moscovici --- qemu-img.c | 63 ++++++++++++++++---------------------- tests/qemu-iotests/049.out | 4 +-- 2 files changed, 28 insertions(+), 39 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index 116a9c6349..4a06ab7fe8 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -470,16 +470,22 @@ static int add_old_style_options(const char *fmt, Qem= uOpts *opts, return 0; } =20 -static int64_t cvtnum(const char *s) +static int64_t cvtnum(const char *arg_name, const char *arg_value) { int err; uint64_t value; =20 - err =3D qemu_strtosz(s, NULL, &value); - if (err < 0) { + err =3D qemu_strtosz(arg_value, NULL, &value); + if (err < 0 && err !=3D -ERANGE) { + error_report("Invalid %s specified! You may use " + "k, M, G, T, P or E suffixes for ", arg_name); + error_report("kilobytes, megabytes, gigabytes, terabytes, " + "petabytes and exabytes."); return err; } - if (value > INT64_MAX) { + if (err =3D=3D -ERANGE || value > INT64_MAX) { + error_report("Invalid %s specified! Must be less than 8 EiB!", + arg_name); return -ERANGE; } return value; @@ -572,16 +578,8 @@ static int img_create(int argc, char **argv) if (optind < argc) { int64_t sval; =20 - 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 +2185,10 @@ static int img_convert(int argc, char **argv) { int64_t sval; =20 - 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 +4291,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 +4305,8 @@ static int img_bench(int argc, char **argv) { int64_t sval; =20 - sval =3D cvtnum(optarg); + sval =3D cvtnum("buffer size", optarg); if (sval < 0) { - error_report("Invalid buffer size specified"); return 1; } =20 @@ -4319,9 +4317,8 @@ static int img_bench(int argc, char **argv) { int64_t sval; =20 - sval =3D cvtnum(optarg); + sval =3D cvtnum("step_size", optarg); if (sval < 0) { - error_report("Invalid step size specified"); return 1; } =20 @@ -4491,10 +4488,12 @@ static int img_dd_bs(const char *arg, { int64_t res; =20 - res =3D cvtnum(arg); + res =3D cvtnum("bs", arg); =20 - if (res <=3D 0) { - error_report("invalid number: '%s'", arg); + if (res < 0) { + return 1; + } else if (res =3D=3D 0) { + error_report("Invalid bs specified! It cannot be 0."); return 1; } in->bsz =3D out->bsz =3D res; @@ -4506,10 +4505,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); =20 if (dd->count < 0) { - error_report("invalid number: '%s'", arg); return 1; } =20 @@ -4538,10 +4536,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); =20 if (in->offset < 0) { - error_report("invalid number: '%s'", arg); return 1; } =20 @@ -4923,16 +4920,8 @@ static int img_measure(int argc, char **argv) { int64_t sval; =20 - 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 affa55b341..0ec2e6b82e 100644 --- a/tests/qemu-iotests/049.out +++ b/tests/qemu-iotests/049.out @@ -92,13 +92,13 @@ Formatting 'TEST_DIR/t.qcow2', fmt=3Dqcow2 size=3D16492= 67441664 cluster_size=3D65536 l =3D=3D 3. Invalid sizes =3D=3D =20 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 less than 8 EiB! =20 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' =20 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 less than 8 EiB! =20 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' --=20 2.17.2 (Apple Git-113)