From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
"Richard W . M . Jones" <rjones@redhat.com>,
"open list:Network Block Dev..." <qemu-block@nongnu.org>
Subject: [Qemu-devel] [PULL 07/21] qemu-nbd: Avoid strtol open-coding
Date: Mon, 21 Jan 2019 16:48:53 -0600 [thread overview]
Message-ID: <20190121224907.26634-8-eblake@redhat.com> (raw)
In-Reply-To: <20190121224907.26634-1-eblake@redhat.com>
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 <eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
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 = 0;
- char *end;
int flags = BDRV_O_RDWR;
- int partition = -1;
+ int partition = 0;
int ret = 0;
bool seen_cache = false;
bool seen_discard = false;
@@ -660,9 +659,8 @@ int main(int argc, char **argv)
port = optarg;
break;
case 'o':
- dev_offset = 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 &= ~BDRV_O_RDWR;
break;
case 'P':
- partition = 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 = optarg;
break;
case 'e':
- shared = 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 = optarg;
@@ -1007,7 +997,7 @@ int main(int argc, char **argv)
}
fd_size -= dev_offset;
- if (partition != -1) {
+ if (partition) {
uint64_t limit;
if (dev_offset) {
--
2.20.1
next prev parent reply other threads:[~2019-01-21 22:49 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-21 22:48 [Qemu-devel] [PULL 00/21] NBD patches through 2019-01-21 Eric Blake
2019-01-21 22:48 ` [Qemu-devel] [PULL 01/21] iotests: Make 233 output more reliable Eric Blake
2019-01-21 22:48 ` [Qemu-devel] [PULL 02/21] maint: Allow for EXAMPLES in texi2pod Eric Blake
2019-01-21 22:48 ` [Qemu-devel] [PULL 03/21] qemu-nbd: Enhance man page Eric Blake
2019-01-21 22:48 ` [Qemu-devel] [PULL 04/21] qemu-nbd: Sanity check partition bounds Eric Blake
2019-01-21 22:48 ` [Qemu-devel] [PULL 05/21] nbd/server: Hoist length check to qmp_nbd_server_add Eric Blake
2019-01-21 22:48 ` [Qemu-devel] [PULL 06/21] nbd/server: Favor [u]int64_t over off_t Eric Blake
2019-01-21 22:48 ` Eric Blake [this message]
2019-01-21 22:48 ` [Qemu-devel] [PULL 08/21] nbd/client: Refactor nbd_receive_list() Eric Blake
2019-01-21 22:48 ` [Qemu-devel] [PULL 09/21] nbd/client: Move export name into NBDExportInfo Eric Blake
2019-01-21 22:48 ` [Qemu-devel] [PULL 10/21] nbd/client: Change signature of nbd_negotiate_simple_meta_context() Eric Blake
2019-01-21 22:48 ` [Qemu-devel] [PULL 11/21] nbd/client: Split out nbd_send_meta_query() Eric Blake
2019-01-21 22:48 ` [Qemu-devel] [PULL 12/21] nbd/client: Split out nbd_receive_one_meta_context() Eric Blake
2019-01-21 22:48 ` [Qemu-devel] [PULL 13/21] nbd/client: Refactor return of nbd_receive_negotiate() Eric Blake
2019-01-21 22:49 ` [Qemu-devel] [PULL 14/21] nbd/client: Split handshake into two functions Eric Blake
2019-01-21 22:49 ` [Qemu-devel] [PULL 15/21] nbd/client: Pull out oldstyle size determination Eric Blake
2019-01-21 22:49 ` [Qemu-devel] [PULL 16/21] nbd/client: Refactor nbd_opt_go() to support NBD_OPT_INFO Eric Blake
2019-01-21 22:49 ` [Qemu-devel] [PULL 17/21] nbd/client: Add nbd_receive_export_list() Eric Blake
2019-01-21 22:49 ` [Qemu-devel] [PULL 18/21] nbd/client: Add meta contexts to nbd_receive_export_list() Eric Blake
2019-01-21 22:49 ` [Qemu-devel] [PULL 19/21] qemu-nbd: Add --list option Eric Blake
2019-01-21 22:49 ` [Qemu-devel] [PULL 20/21] nbd/client: Work around 3.0 bug for listing meta contexts Eric Blake
2019-01-21 22:49 ` [Qemu-devel] [PULL 21/21] iotests: Enhance 223, 233 to cover 'qemu-nbd --list' Eric Blake
2019-01-22 19:23 ` [Qemu-devel] [PULL 00/21] NBD patches through 2019-01-21 Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190121224907.26634-8-eblake@redhat.com \
--to=eblake@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=rjones@redhat.com \
--cc=vsementsov@virtuozzo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).