From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, stefanha@redhat.com, qemu-devel@nongnu.org
Subject: [PULL 57/57] qemu-img: extend cvtnum() and use it in more places
Date: Tue, 15 Jul 2025 21:03:30 +0200 [thread overview]
Message-ID: <20250715190330.378764-58-kwolf@redhat.com> (raw)
In-Reply-To: <20250715190330.378764-1-kwolf@redhat.com>
From: Michael Tokarev <mjt@tls.msk.ru>
cvtnum() expects input string to specify some sort of size
(optionally with KMG... suffix). However, there are a lot
of other number conversions in there (using qemu_strtol &Co),
also, not all conversions which use cvtnum, actually expects
size, - like dd count=nn.
Add bool is_size argument to cvtnum() to specify if it should
treat the argument as a size or something else, - this changes
conversion routine in use and error text.
Use the new cvtnum() in more places (like where strtol were used),
since it never return negative number in successful conversion.
When it makes sense, also specify upper or lower bounds at the
same time. This simplifies option processing in multiple places,
removing the need of local temporary variables and longer error
reporting code.
While at it, fix errors, like depth in measure must be >= 1,
while the previous code allowed it to be 0.
In a few places, change unsigned variables (like of type size_t)
to be signed instead, - to avoid the need of temporary conversion
variable. All these variables are okay to be signed, we never
assign <0 value to them except of the cases of conversion error,
where we return immediately.
While at it, remove allowed size suffixes from the error message
as it makes no sense most of the time (should be in help instead).
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Message-ID: <20250531171609.197078-28-mjt@tls.msk.ru>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
qemu-img.c | 111 +++++++++++++------------------------
tests/qemu-iotests/049.out | 9 +--
2 files changed, 40 insertions(+), 80 deletions(-)
diff --git a/qemu-img.c b/qemu-img.c
index b34b1390bb..7a162fdc08 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -398,18 +398,16 @@ static int add_old_style_options(const char *fmt, QemuOpts *opts,
return 0;
}
-static int64_t cvtnum_full(const char *name, const char *value, int64_t min,
- int64_t max)
+static int64_t cvtnum_full(const char *name, const char *value,
+ bool is_size, int64_t min, int64_t max)
{
int err;
uint64_t res;
- err = qemu_strtosz(value, NULL, &res);
+ err = is_size ? qemu_strtosz(value, NULL, &res) :
+ qemu_strtou64(value, NULL, 0, &res);
if (err < 0 && err != -ERANGE) {
- error_report("Invalid %s specified. You may use "
- "k, M, G, T, P or E suffixes for", name);
- error_report("kilobytes, megabytes, gigabytes, terabytes, "
- "petabytes and exabytes.");
+ error_report("Invalid %s specified: '%s'", name, value);
return err;
}
if (err == -ERANGE || res > max || res < min) {
@@ -420,9 +418,9 @@ static int64_t cvtnum_full(const char *name, const char *value, int64_t min,
return res;
}
-static int64_t cvtnum(const char *name, const char *value)
+static int64_t cvtnum(const char *name, const char *value, bool is_size)
{
- return cvtnum_full(name, value, 0, INT64_MAX);
+ return cvtnum_full(name, value, is_size, 0, INT64_MAX);
}
static int img_create(const img_cmd_t *ccmd, int argc, char **argv)
@@ -525,7 +523,7 @@ static int img_create(const img_cmd_t *ccmd, int argc, char **argv)
/* Get image size, if specified */
if (optind < argc) {
- img_size = cvtnum("image size", argv[optind++]);
+ img_size = cvtnum("image size", argv[optind++], true);
if (img_size < 0) {
goto fail;
}
@@ -984,7 +982,7 @@ static int img_commit(const img_cmd_t *ccmd, int argc, char **argv)
drop = true;
break;
case 'r':
- rate_limit = cvtnum("rate limit", optarg);
+ rate_limit = cvtnum("rate limit", optarg, true);
if (rate_limit < 0) {
return 1;
}
@@ -2428,7 +2426,7 @@ static int img_convert(const img_cmd_t *ccmd, int argc, char **argv)
{
int64_t sval;
- sval = cvtnum("buffer size for sparse output", optarg);
+ sval = cvtnum("buffer size for sparse output", optarg, true);
if (sval < 0) {
goto fail_getopt;
} else if (!QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) ||
@@ -2462,16 +2460,15 @@ static int img_convert(const img_cmd_t *ccmd, int argc, char **argv)
force_share = true;
break;
case 'r':
- rate_limit = cvtnum("rate limit", optarg);
+ rate_limit = cvtnum("rate limit", optarg, true);
if (rate_limit < 0) {
goto fail_getopt;
}
break;
case 'm':
- if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
- s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
- error_report("Invalid number of coroutines. Allowed number of"
- " coroutines is between 1 and %d", MAX_COROUTINES);
+ s.num_coroutines = cvtnum_full("number of coroutines", optarg,
+ false, 1, MAX_COROUTINES);
+ if (s.num_coroutines < 0) {
goto fail_getopt;
}
break;
@@ -3376,13 +3373,13 @@ static int img_map(const img_cmd_t *ccmd, int argc, char **argv)
image_opts = true;
break;
case 's':
- start_offset = cvtnum("start offset", optarg);
+ start_offset = cvtnum("start offset", optarg, true);
if (start_offset < 0) {
return 1;
}
break;
case 'l':
- max_length = cvtnum("max length", optarg);
+ max_length = cvtnum("max length", optarg, true);
if (max_length < 0) {
return 1;
}
@@ -4720,9 +4717,9 @@ static int img_bench(const img_cmd_t *ccmd, int argc, char **argv)
int count = 75000;
int depth = 64;
int64_t offset = 0;
- size_t bufsize = 4096;
+ ssize_t bufsize = 4096;
int pattern = 0;
- size_t step = 0;
+ ssize_t step = 0;
int flush_interval = 0;
bool drain_on_flush = true;
int64_t image_size;
@@ -4827,27 +4824,17 @@ static int img_bench(const img_cmd_t *ccmd, int argc, char **argv)
}
break;
case 'c':
- {
- unsigned long res;
-
- if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
- error_report("Invalid request count specified");
+ count = cvtnum_full("request count", optarg, false, 1, INT_MAX);
+ if (count < 0) {
return 1;
}
- count = res;
break;
- }
case 'd':
- {
- unsigned long res;
-
- if (qemu_strtoul(optarg, NULL, 0, &res) <= 0 || res > INT_MAX) {
- error_report("Invalid queue depth specified");
+ depth = cvtnum_full("queue depth", optarg, false, 1, INT_MAX);
+ if (depth < 0) {
return 1;
}
- depth = res;
break;
- }
case 'n':
flags |= BDRV_O_NATIVE_AIO;
break;
@@ -4860,64 +4847,40 @@ static int img_bench(const img_cmd_t *ccmd, int argc, char **argv)
}
break;
case 'o':
- {
- offset = cvtnum("offset", optarg);
+ offset = cvtnum("offset", optarg, true);
if (offset < 0) {
return 1;
}
break;
- }
- break;
case 's':
- {
- int64_t sval;
-
- sval = cvtnum_full("buffer size", optarg, 0, INT_MAX);
- if (sval < 0) {
+ bufsize = cvtnum_full("buffer size", optarg, true, 1, INT_MAX);
+ if (bufsize < 0) {
return 1;
}
-
- bufsize = sval;
break;
- }
case 'S':
- {
- int64_t sval;
-
- sval = cvtnum_full("step_size", optarg, 0, INT_MAX);
- if (sval < 0) {
+ step = cvtnum_full("step size", optarg, true, 0, INT_MAX);
+ if (step < 0) {
return 1;
}
-
- step = sval;
break;
- }
case 'w':
flags |= BDRV_O_RDWR;
is_write = true;
break;
case OPTION_PATTERN:
- {
- unsigned long res;
-
- if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
- error_report("Invalid pattern byte specified");
+ pattern = cvtnum_full("pattern byte", optarg, false, 0, 0xff);
+ if (pattern < 0) {
return 1;
}
- pattern = res;
break;
- }
case OPTION_FLUSH_INTERVAL:
- {
- unsigned long res;
-
- if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
- error_report("Invalid flush interval specified");
+ flush_interval = cvtnum_full("flush interval", optarg,
+ false, 0, INT_MAX);
+ if (flush_interval < 0) {
return 1;
}
- flush_interval = res;
break;
- }
case OPTION_NO_DRAIN:
drain_on_flush = false;
break;
@@ -5129,7 +5092,7 @@ static int img_bitmap(const img_cmd_t *ccmd, int argc, char **argv)
add = true;
break;
case 'g':
- granularity = cvtnum("granularity", optarg);
+ granularity = cvtnum("granularity", optarg, true);
if (granularity < 0) {
return 1;
}
@@ -5314,7 +5277,7 @@ static int img_dd_bs(const char *arg,
{
int64_t res;
- res = cvtnum_full("bs", arg, 1, INT_MAX);
+ res = cvtnum_full("bs", arg, true, 1, INT_MAX);
if (res < 0) {
return 1;
@@ -5328,7 +5291,7 @@ static int img_dd_count(const char *arg,
struct DdIo *in, struct DdIo *out,
struct DdInfo *dd)
{
- dd->count = cvtnum("count", arg);
+ dd->count = cvtnum("count", arg, true);
if (dd->count < 0) {
return 1;
@@ -5359,7 +5322,7 @@ static int img_dd_skip(const char *arg,
struct DdIo *in, struct DdIo *out,
struct DdInfo *dd)
{
- in->offset = cvtnum("skip", arg);
+ in->offset = cvtnum("skip", arg, true);
if (in->offset < 0) {
return 1;
@@ -5767,7 +5730,7 @@ static int img_measure(const img_cmd_t *ccmd, int argc, char **argv)
user_creatable_process_cmdline(optarg);
break;
case 's':
- img_size = cvtnum("image size", optarg);
+ img_size = cvtnum("image size", optarg, true);
if (img_size < 0) {
goto out;
}
diff --git a/tests/qemu-iotests/049.out b/tests/qemu-iotests/049.out
index 34e1b452e6..70c627538b 100644
--- a/tests/qemu-iotests/049.out
+++ b/tests/qemu-iotests/049.out
@@ -98,8 +98,7 @@ qemu-img create -f qcow2 -o size=-1024 TEST_DIR/t.qcow2
qemu-img: TEST_DIR/t.qcow2: Value '-1024' is out of range for parameter 'size'
qemu-img create -f qcow2 TEST_DIR/t.qcow2 -- -1k
-qemu-img: Invalid image size specified. You may use k, M, G, T, P or E suffixes for
-qemu-img: kilobytes, megabytes, gigabytes, terabytes, petabytes and exabytes.
+qemu-img: Invalid image size specified: '-1k'
qemu-img create -f qcow2 -o size=-1k TEST_DIR/t.qcow2
qemu-img: TEST_DIR/t.qcow2: Parameter 'size' expects a non-negative number below 2^64
@@ -107,8 +106,7 @@ Optional suffix k, M, G, T, P or E means kilo-, mega-, giga-, tera-, peta-
and exabytes, respectively.
qemu-img create -f qcow2 TEST_DIR/t.qcow2 -- 1kilobyte
-qemu-img: Invalid image size specified. You may use k, M, G, T, P or E suffixes for
-qemu-img: kilobytes, megabytes, gigabytes, terabytes, petabytes and exabytes.
+qemu-img: Invalid image size specified: '1kilobyte'
qemu-img create -f qcow2 -o size=1kilobyte TEST_DIR/t.qcow2
qemu-img: TEST_DIR/t.qcow2: Parameter 'size' expects a non-negative number below 2^64
@@ -116,8 +114,7 @@ Optional suffix k, M, G, T, P or E means kilo-, mega-, giga-, tera-, peta-
and exabytes, respectively.
qemu-img create -f qcow2 TEST_DIR/t.qcow2 -- foobar
-qemu-img: Invalid image size specified. You may use k, M, G, T, P or E suffixes for
-qemu-img: kilobytes, megabytes, gigabytes, terabytes, petabytes and exabytes.
+qemu-img: Invalid image size specified: 'foobar'
qemu-img create -f qcow2 -o size=foobar TEST_DIR/t.qcow2
qemu-img: TEST_DIR/t.qcow2: Parameter 'size' expects a non-negative number below 2^64
--
2.50.1
next prev parent reply other threads:[~2025-07-15 19:34 UTC|newest]
Thread overview: 59+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-15 19:02 [PULL 00/57] Block layer patches Kevin Wolf
2025-07-15 19:02 ` [PULL 01/57] block: never use atomics to access bs->quiesce_counter Kevin Wolf
2025-07-15 19:02 ` [PULL 02/57] block: add bdrv_graph_wrlock_drained() convenience wrapper Kevin Wolf
2025-07-15 19:02 ` [PULL 03/57] block/mirror: switch to bdrv_set_backing_hd_drained() variant Kevin Wolf
2025-07-15 19:02 ` [PULL 04/57] block/commit: " Kevin Wolf
2025-07-15 19:02 ` [PULL 05/57] block: call bdrv_set_backing_hd() while unlocked in bdrv_open_backing_file() Kevin Wolf
2025-07-15 19:02 ` [PULL 06/57] block: mark bdrv_set_backing_hd() as GRAPH_UNLOCKED Kevin Wolf
2025-07-15 19:02 ` [PULL 07/57] blockdev: avoid locking and draining multiple times in external_snapshot_abort() Kevin Wolf
2025-07-15 19:02 ` [PULL 08/57] block: drop wrapper for bdrv_set_backing_hd_drained() Kevin Wolf
2025-07-15 19:02 ` [PULL 09/57] block-backend: mark blk_drain_all() as GRAPH_UNLOCKED Kevin Wolf
2025-07-15 19:02 ` [PULL 10/57] block/snapshot: mark bdrv_all_delete_snapshot() " Kevin Wolf
2025-07-15 19:02 ` [PULL 11/57] block/stream: mark stream_prepare() " Kevin Wolf
2025-07-15 19:02 ` [PULL 12/57] block: mark bdrv_reopen_queue() and bdrv_reopen_multiple() " Kevin Wolf
2025-07-15 19:02 ` [PULL 13/57] block: mark bdrv_inactivate() as GRAPH_RDLOCK and move drain to callers Kevin Wolf
2025-07-15 19:02 ` [PULL 14/57] block: mark bdrv_inactivate_all() as GRAPH_UNLOCKED Kevin Wolf
2025-07-15 19:02 ` [PULL 15/57] block: mark blk_remove_bs() " Kevin Wolf
2025-07-15 19:02 ` [PULL 16/57] block: mark blk_drain() " Kevin Wolf
2025-07-15 19:02 ` [PULL 17/57] block-backend: mark blk_io_limits_disable() " Kevin Wolf
2025-07-15 19:02 ` [PULL 18/57] block/commit: mark commit_abort() " Kevin Wolf
2025-07-15 19:02 ` [PULL 19/57] block: Allow bdrv_new() with and without graph lock Kevin Wolf
2025-07-15 19:02 ` [PULL 20/57] block: mark bdrv_replace_child_bs() as GRAPH_UNLOCKED Kevin Wolf
2025-07-15 19:02 ` [PULL 21/57] block: mark bdrv_insert_node() " Kevin Wolf
2025-07-15 19:02 ` [PULL 22/57] block: mark bdrv_drop_intermediate() " Kevin Wolf
2025-07-15 19:02 ` [PULL 23/57] block: mark bdrv_close_all() " Kevin Wolf
2025-07-15 19:02 ` [PULL 24/57] block: mark bdrv_close() " Kevin Wolf
2025-07-15 19:02 ` [PULL 25/57] block: mark bdrv_open_child_common() and its callers GRAPH_UNLOCKED Kevin Wolf
2025-07-15 19:02 ` [PULL 26/57] blockjob: mark block_job_remove_all_bdrv() as GRAPH_UNLOCKED Kevin Wolf
2025-07-15 19:03 ` [PULL 27/57] block/qapi: include child references in block device info Kevin Wolf
2025-07-15 19:03 ` [PULL 28/57] block/qapi: make @node-name in @BlockDeviceInfo non-optional Kevin Wolf
2025-07-15 19:03 ` [PULL 29/57] file-posix: Fix aio=threads performance regression after enablign FUA Kevin Wolf
2025-07-15 19:03 ` [PULL 30/57] iotests: add test for changing the 'drive' property via 'qom-set' Kevin Wolf
2025-07-15 19:03 ` [PULL 31/57] qemu-img: measure: convert img_size to signed, simplify handling Kevin Wolf
2025-07-15 19:03 ` [PULL 32/57] qemu-img: create: " Kevin Wolf
2025-07-15 19:03 ` [PULL 33/57] qemu-img: global option processing and error printing Kevin Wolf
2025-07-15 19:03 ` [PULL 34/57] qemu-img: pass current cmd info into command handlers Kevin Wolf
2025-07-15 19:03 ` [PULL 35/57] qemu-img: create: refresh options/--help (short option change) Kevin Wolf
2025-07-15 19:03 ` [PULL 36/57] qemu-img: factor out parse_output_format() and use it in the code Kevin Wolf
2025-07-15 19:03 ` [PULL 37/57] qemu-img: check: refresh options/--help Kevin Wolf
2025-07-15 19:03 ` [PULL 38/57] qemu-img: simplify --repair error message Kevin Wolf
2025-07-15 19:03 ` [PULL 39/57] qemu-img: commit: refresh options/--help Kevin Wolf
2025-07-15 19:03 ` [PULL 40/57] qemu-img: compare: use helper function for --object Kevin Wolf
2025-07-15 19:03 ` [PULL 41/57] qemu-img: compare: refresh options/--help Kevin Wolf
2025-07-15 19:03 ` [PULL 42/57] qemu-img: convert: refresh options/--help (short option change) Kevin Wolf
2025-07-15 19:03 ` [PULL 43/57] qemu-img: info: refresh options/--help Kevin Wolf
2025-07-15 19:03 ` [PULL 44/57] qemu-img: map: " Kevin Wolf
2025-07-15 19:03 ` [PULL 45/57] qemu-img: snapshot: allow specifying -f fmt Kevin Wolf
2025-07-15 19:03 ` [PULL 46/57] qemu-img: snapshot: make -l (list) the default, simplify option handling Kevin Wolf
2025-07-15 19:03 ` [PULL 47/57] qemu-img: snapshot: refresh options/--help Kevin Wolf
2025-07-15 19:03 ` [PULL 48/57] qemu-img: rebase: refresh options/--help (short option change) Kevin Wolf
2025-07-15 19:03 ` [PULL 49/57] qemu-img: resize: do not always eat last argument Kevin Wolf
2025-07-15 19:03 ` [PULL 50/57] qemu-img: resize: refresh options/--help Kevin Wolf
2025-07-15 19:03 ` [PULL 51/57] qemu-img: amend: " Kevin Wolf
2025-07-15 19:03 ` [PULL 52/57] qemu-img: bench: " Kevin Wolf
2025-07-15 19:03 ` [PULL 53/57] qemu-img: bitmap: " Kevin Wolf
2025-07-15 19:03 ` [PULL 54/57] qemu-img: dd: " Kevin Wolf
2025-07-15 19:03 ` [PULL 55/57] qemu-img: measure: " Kevin Wolf
2025-07-15 19:03 ` [PULL 56/57] qemu-img: implement short --help, remove global help() function Kevin Wolf
2025-07-15 19:03 ` Kevin Wolf [this message]
2025-07-16 12:41 ` [PULL 00/57] Block layer patches Stefan Hajnoczi
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=20250715190330.378764-58-kwolf@redhat.com \
--to=kwolf@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.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).