[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 20/24] qemu-img: Wrap cvtnum() around qemu_strtosz()
From: |
Markus Armbruster |
Subject: |
[Qemu-devel] [PULL 20/24] qemu-img: Wrap cvtnum() around qemu_strtosz() |
Date: |
Thu, 23 Feb 2017 20:53:58 +0100 |
Cc: Kevin Wolf <address@hidden>
Cc: Max Reitz <address@hidden>
Cc: address@hidden
Signed-off-by: Markus Armbruster <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
Message-Id: <address@hidden>
---
qemu-img.c | 58 +++++++++++++++++++++++++++++++---------------------------
1 file changed, 31 insertions(+), 27 deletions(-)
diff --git a/qemu-img.c b/qemu-img.c
index f1c641c..4062917 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -368,6 +368,19 @@ static int add_old_style_options(const char *fmt, QemuOpts
*opts,
return 0;
}
+static int64_t cvtnum(const char *s)
+{
+ char *end;
+ int64_t ret;
+
+ ret = qemu_strtosz(s, &end);
+ if (*end != '\0') {
+ /* Detritus at the end of the string */
+ return -EINVAL;
+ }
+ return ret;
+}
+
static int img_create(int argc, char **argv)
{
int c;
@@ -461,9 +474,9 @@ static int img_create(int argc, char **argv)
/* Get image size, if specified */
if (optind < argc) {
int64_t sval;
- char *end;
- sval = qemu_strtosz(argv[optind++], &end);
- if (sval < 0 || *end) {
+
+ sval = cvtnum(argv[optind++]);
+ if (sval < 0) {
if (sval == -ERANGE) {
error_report("Image size must be less than 8 EiB!");
} else {
@@ -1863,9 +1876,9 @@ static int img_convert(int argc, char **argv)
case 'S':
{
int64_t sval;
- char *end;
- sval = qemu_strtosz(optarg, &end);
- if (sval < 0 || *end) {
+
+ sval = cvtnum(optarg);
+ if (sval < 0) {
error_report("Invalid minimum zero buffer size for sparse
output specified");
ret = -1;
goto fail_getopt;
@@ -3650,10 +3663,8 @@ static int img_bench(int argc, char **argv)
break;
case 'o':
{
- char *end;
- errno = 0;
- offset = qemu_strtosz(optarg, &end);
- if (offset < 0|| *end) {
+ offset = cvtnum(optarg);
+ if (offset < 0) {
error_report("Invalid offset specified");
return 1;
}
@@ -3666,10 +3677,9 @@ static int img_bench(int argc, char **argv)
case 's':
{
int64_t sval;
- char *end;
- sval = qemu_strtosz(optarg, &end);
- if (sval < 0 || sval > INT_MAX || *end) {
+ sval = cvtnum(optarg);
+ if (sval < 0 || sval > INT_MAX) {
error_report("Invalid buffer size specified");
return 1;
}
@@ -3680,10 +3690,9 @@ static int img_bench(int argc, char **argv)
case 'S':
{
int64_t sval;
- char *end;
- sval = qemu_strtosz(optarg, &end);
- if (sval < 0 || sval > INT_MAX || *end) {
+ sval = cvtnum(optarg);
+ if (sval < 0 || sval > INT_MAX) {
error_report("Invalid step size specified");
return 1;
}
@@ -3842,12 +3851,11 @@ static int img_dd_bs(const char *arg,
struct DdIo *in, struct DdIo *out,
struct DdInfo *dd)
{
- char *end;
int64_t res;
- res = qemu_strtosz(arg, &end);
+ res = cvtnum(arg);
- if (res <= 0 || res > INT_MAX || *end) {
+ if (res <= 0 || res > INT_MAX) {
error_report("invalid number: '%s'", arg);
return 1;
}
@@ -3860,11 +3868,9 @@ static int img_dd_count(const char *arg,
struct DdIo *in, struct DdIo *out,
struct DdInfo *dd)
{
- char *end;
+ dd->count = cvtnum(arg);
- dd->count = qemu_strtosz(arg, &end);
-
- if (dd->count < 0 || *end) {
+ if (dd->count < 0) {
error_report("invalid number: '%s'", arg);
return 1;
}
@@ -3894,11 +3900,9 @@ static int img_dd_skip(const char *arg,
struct DdIo *in, struct DdIo *out,
struct DdInfo *dd)
{
- char *end;
+ in->offset = cvtnum(arg);
- in->offset = qemu_strtosz(arg, &end);
-
- if (in->offset < 0 || *end) {
+ if (in->offset < 0) {
error_report("invalid number: '%s'", arg);
return 1;
}
--
2.7.4
- [Qemu-devel] [PULL 02/24] option: Assert value string isn't null, (continued)
- [Qemu-devel] [PULL 02/24] option: Assert value string isn't null, Markus Armbruster, 2017/02/23
- [Qemu-devel] [PULL 14/24] util/cutils: New qemu_strtosz_metric(), Markus Armbruster, 2017/02/23
- [Qemu-devel] [PULL 13/24] test-cutils: Cover qemu_strtosz() around range limits, Markus Armbruster, 2017/02/23
- [Qemu-devel] [PULL 24/24] option: Fix checking of sizes for overflow and trailing crap, Markus Armbruster, 2017/02/23
- [Qemu-devel] [PULL 06/24] util/cutils: Rename qemu_strtoll(), qemu_strtoull(), Markus Armbruster, 2017/02/23
- [Qemu-devel] [PULL 07/24] util/cutils: Clean up variable names around qemu_strtol(), Markus Armbruster, 2017/02/23
- [Qemu-devel] [PULL 08/24] util/cutils: Clean up control flow around qemu_strtol() a bit, Markus Armbruster, 2017/02/23
- [Qemu-devel] [PULL 21/24] util/cutils: Let qemu_strtosz*() optionally reject trailing crap, Markus Armbruster, 2017/02/23
- [Qemu-devel] [PULL 15/24] util/cutils: Rename qemu_strtosz() to qemu_strtosz_MiB(), Markus Armbruster, 2017/02/23
- [Qemu-devel] [PULL 17/24] util/cutils: Drop QEMU_STRTOSZ_DEFSUFFIX_* macros, Markus Armbruster, 2017/02/23
- [Qemu-devel] [PULL 20/24] qemu-img: Wrap cvtnum() around qemu_strtosz(),
Markus Armbruster <=
- [Qemu-devel] [PULL 04/24] test-cutils: Clean up qemu_strtoul() result checks, Markus Armbruster, 2017/02/23
- [Qemu-devel] [PULL 01/24] test-qemu-opts: Cover qemu_opts_parse(), Markus Armbruster, 2017/02/23
- [Qemu-devel] [PULL 16/24] util/cutils: New qemu_strtosz(), Markus Armbruster, 2017/02/23
- [Qemu-devel] [PULL 18/24] test-cutils: Use qemu_strtosz() more often, Markus Armbruster, 2017/02/23
- [Qemu-devel] [PULL 05/24] util/cutils: Rewrite documentation of qemu_strtol() & friends, Markus Armbruster, 2017/02/23
- [Qemu-devel] [PULL 22/24] util/cutils: Return qemu_strtosz*() error and value separately, Markus Armbruster, 2017/02/23
- [Qemu-devel] [PULL 23/24] util/cutils: Change qemu_strtosz*() from int64_t to uint64_t, Markus Armbruster, 2017/02/23
- Re: [Qemu-devel] [PULL 00/24] option cutils: Fix and clean up number conversions, no-reply, 2017/02/23
- Re: [Qemu-devel] [PULL 00/24] option cutils: Fix and clean up number conversions, Peter Maydell, 2017/02/25