From nobody Sun Oct 5 21:12:40 2025 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=redhat.com Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1548111462990745.9643970478743; Mon, 21 Jan 2019 14:57:42 -0800 (PST) Received: from localhost ([127.0.0.1]:35501 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gliVZ-0007ht-TR for importer@patchew.org; Mon, 21 Jan 2019 17:57:33 -0500 Received: from eggs.gnu.org ([209.51.188.92]:40224) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gliO3-0001P7-T4 for qemu-devel@nongnu.org; Mon, 21 Jan 2019 17:49:50 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gliO1-0005FY-Qm for qemu-devel@nongnu.org; Mon, 21 Jan 2019 17:49:47 -0500 Received: from mx1.redhat.com ([209.132.183.28]:49502) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gliNw-0004xi-FL; Mon, 21 Jan 2019 17:49:40 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B5E88356F5; Mon, 21 Jan 2019 22:49:19 +0000 (UTC) Received: from blue.redhat.com (ovpn-117-44.phx2.redhat.com [10.3.117.44]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5010960123; Mon, 21 Jan 2019 22:49:19 +0000 (UTC) From: Eric Blake To: qemu-devel@nongnu.org Date: Mon, 21 Jan 2019 16:48:53 -0600 Message-Id: <20190121224907.26634-8-eblake@redhat.com> In-Reply-To: <20190121224907.26634-1-eblake@redhat.com> References: <20190121224907.26634-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Mon, 21 Jan 2019 22:49:19 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 07/21] qemu-nbd: Avoid strtol open-coding X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Vladimir Sementsov-Ogievskiy , "Richard W . M . Jones" , "open list:Network Block Dev..." Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Our copy-and-pasted open-coding of strtol handling forgot to handle overflow conditions. Use qemu_strto*() instead. In the case of --partition, since we insist on a user-supplied partition to be non-zero, we can use 0 rather than -1 for our initial value to distinguish when a partition is not being served, for slightly more optimal code. The error messages for out-of-bounds values are less specific, but should not be a terrible loss in quality. Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Richard W.M. Jones Message-Id: <20190117193658.16413-8-eblake@redhat.com> --- qemu-nbd.c | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/qemu-nbd.c b/qemu-nbd.c index 598caa656b2..efca0e44a90 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -546,9 +546,8 @@ int main(int argc, char **argv) }; int ch; int opt_ind =3D 0; - char *end; int flags =3D BDRV_O_RDWR; - int partition =3D -1; + int partition =3D 0; int ret =3D 0; bool seen_cache =3D false; bool seen_discard =3D false; @@ -660,9 +659,8 @@ int main(int argc, char **argv) port =3D optarg; break; case 'o': - dev_offset =3D strtoll (optarg, &end, 0); - if (*end) { - error_report("Invalid offset `%s'", optarg); + if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) { + error_report("Invalid offset '%s'", optarg); exit(EXIT_FAILURE); } break; @@ -684,13 +682,9 @@ int main(int argc, char **argv) flags &=3D ~BDRV_O_RDWR; break; case 'P': - partition =3D strtol(optarg, &end, 0); - if (*end) { - error_report("Invalid partition `%s'", optarg); - exit(EXIT_FAILURE); - } - if (partition < 1 || partition > 8) { - error_report("Invalid partition %d", partition); + if (qemu_strtoi(optarg, NULL, 0, &partition) < 0 || + partition < 1 || partition > 8) { + error_report("Invalid partition '%s'", optarg); exit(EXIT_FAILURE); } break; @@ -711,15 +705,11 @@ int main(int argc, char **argv) device =3D optarg; break; case 'e': - shared =3D strtol(optarg, &end, 0); - if (*end) { + if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 || + shared < 1) { error_report("Invalid shared device number '%s'", optarg); exit(EXIT_FAILURE); } - if (shared < 1) { - error_report("Shared device number must be greater than 0"= ); - exit(EXIT_FAILURE); - } break; case 'f': fmt =3D optarg; @@ -1007,7 +997,7 @@ int main(int argc, char **argv) } fd_size -=3D dev_offset; - if (partition !=3D -1) { + if (partition) { uint64_t limit; if (dev_offset) { --=20 2.20.1