qemu-nbd: Avoid strtol open-coding

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>
This commit is contained in:
Eric Blake 2019-01-17 13:36:44 -06:00
parent 9d26dfcbab
commit 43b510113b

View File

@ -546,9 +546,8 @@ int main(int argc, char **argv)
}; };
int ch; int ch;
int opt_ind = 0; int opt_ind = 0;
char *end;
int flags = BDRV_O_RDWR; int flags = BDRV_O_RDWR;
int partition = -1; int partition = 0;
int ret = 0; int ret = 0;
bool seen_cache = false; bool seen_cache = false;
bool seen_discard = false; bool seen_discard = false;
@ -660,9 +659,8 @@ int main(int argc, char **argv)
port = optarg; port = optarg;
break; break;
case 'o': case 'o':
dev_offset = strtoll (optarg, &end, 0); if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) {
if (*end) { error_report("Invalid offset '%s'", optarg);
error_report("Invalid offset `%s'", optarg);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
break; break;
@ -684,13 +682,9 @@ int main(int argc, char **argv)
flags &= ~BDRV_O_RDWR; flags &= ~BDRV_O_RDWR;
break; break;
case 'P': case 'P':
partition = strtol(optarg, &end, 0); if (qemu_strtoi(optarg, NULL, 0, &partition) < 0 ||
if (*end) { partition < 1 || partition > 8) {
error_report("Invalid partition `%s'", optarg); error_report("Invalid partition '%s'", optarg);
exit(EXIT_FAILURE);
}
if (partition < 1 || partition > 8) {
error_report("Invalid partition %d", partition);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
break; break;
@ -711,15 +705,11 @@ int main(int argc, char **argv)
device = optarg; device = optarg;
break; break;
case 'e': case 'e':
shared = strtol(optarg, &end, 0); if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
if (*end) { shared < 1) {
error_report("Invalid shared device number '%s'", optarg); error_report("Invalid shared device number '%s'", optarg);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (shared < 1) {
error_report("Shared device number must be greater than 0");
exit(EXIT_FAILURE);
}
break; break;
case 'f': case 'f':
fmt = optarg; fmt = optarg;
@ -1007,7 +997,7 @@ int main(int argc, char **argv)
} }
fd_size -= dev_offset; fd_size -= dev_offset;
if (partition != -1) { if (partition) {
uint64_t limit; uint64_t limit;
if (dev_offset) { if (dev_offset) {