From: Phillip Wood <phillip.wood123@gmail.com>
To: Patrick Steinhardt <ps@pks.im>, git@vger.kernel.org
Cc: "John Paul Adrian Glaubitz" <glaubitz@physik.fu-berlin.de>,
"Todd Zullinger" <tmz@pobox.com>, "René Scharfe" <l.s.r@web.de>,
"SZEDER Gábor" <szeder.dev@gmail.com>,
"Derrick Stolee" <stolee@gmail.com>, "Jeff King" <peff@peff.net>
Subject: Re: [PATCH v2 4/5] parse-options: introduce `OPTION_UNSIGNED`
Date: Tue, 15 Apr 2025 16:52:02 +0100 [thread overview]
Message-ID: <35300426-32cc-4c0e-b0c3-edb2b2ed312f@gmail.com> (raw)
In-Reply-To: <20250415-b4-pks-parse-options-integers-v2-4-ce07441a1f01@pks.im>
Hi Patrick
On 15/04/2025 13:14, Patrick Steinhardt wrote:
> We have two generic ways to parse integers in the "parse-options"
> subsytem:
>
> - `OPTION_INTEGER` parses a signed integer.
>
> - `OPTION_MAGNITUDE` parses an unsigned integer, but it also
> interprets suffixes like "k" or "g".
>
> Notably missing is a middle ground that parses unsigned integers without
> interpreting suffixes. Introduce a new `OPTION_UNSIGNED` option type to
> plug this gap. This option type will be used in subsequent commits.
I think this is a useful addition. I wonder about the way it handles
negative values though. For types narrower than uintmax_t "-1" will be
rejected but large negative values that parse as small positive numbers
will be accepted. Perhaps we should explicitly reject strings starting
with "-" as we do in git_parse_ulong() which is used by
OPTION_MAGNITUDE. This patch also needs the fix from patch 2 to detect
overflows for uintmax_t.
Best Wishes
Phillip
[1]
https://lore.kernel.org/git/NYMqsJ7uttDzFT2OOEg5LLsxCSoQhTzqBs16KrMHGEKC7LzOAiYnYTEZavRQWqGH41UgjdwScwer7MssNzI7AEDHnD8GTBWvoBIqJ2e7D6g=@proton.me/
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
> parse-options.c | 43 +++++++++++++++++++++++++++++++++++++++++++
> parse-options.h | 12 ++++++++++++
> t/helper/test-parse-options.c | 4 +++-
> t/t0040-parse-options.sh | 18 +++++++++++++++++-
> 4 files changed, 75 insertions(+), 2 deletions(-)
>
> diff --git a/parse-options.c b/parse-options.c
> index ae836c384c7..9670e46a679 100644
> --- a/parse-options.c
> +++ b/parse-options.c
> @@ -216,6 +216,49 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p,
> optname(opt, flags));
> }
> }
> + case OPTION_UNSIGNED:
> + {
> + uintmax_t upper_bound = UINTMAX_MAX >> (bitsizeof(uintmax_t) - CHAR_BIT * opt->precision);
> + uintmax_t value;
> +
> + if (unset) {
> + value = 0;
> + } else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
> + value = opt->defval;
> + } else if (get_arg(p, opt, flags, &arg)) {
> + return -1;
> + } else if (!*arg) {
> + return error(_("%s expects a numerical value"),
> + optname(opt, flags));
> + } else {
> + value = strtoumax(arg, (char **)&s, 10);
> + if (*s)
> + return error(_("%s expects a numerical value"),
> + optname(opt, flags));
> + }
> +
> + if (value > upper_bound)
> + return error(_("value %"PRIuMAX" for %s exceeds %"PRIuMAX),
> + value, optname(opt, flags), upper_bound);
> +
> + switch (opt->precision) {
> + case 1:
> + *(int8_t *)opt->value = value;
> + return 0;
> + case 2:
> + *(int16_t *)opt->value = value;
> + return 0;
> + case 4:
> + *(int32_t *)opt->value = value;
> + return 0;
> + case 8:
> + *(int64_t *)opt->value = value;
> + return 0;
> + default:
> + BUG("invalid precision for option %s",
> + optname(opt, flags));
> + }
> + }
> case OPTION_MAGNITUDE:
> {
> uintmax_t upper_bound = UINTMAX_MAX >> (bitsizeof(uintmax_t) - CHAR_BIT * opt->precision);
> diff --git a/parse-options.h b/parse-options.h
> index 4b561679581..20ea7d2ab13 100644
> --- a/parse-options.h
> +++ b/parse-options.h
> @@ -25,6 +25,7 @@ enum parse_opt_type {
> /* options with arguments (usually) */
> OPTION_STRING,
> OPTION_INTEGER,
> + OPTION_UNSIGNED,
> OPTION_MAGNITUDE,
> OPTION_CALLBACK,
> OPTION_LOWLEVEL_CALLBACK,
> @@ -224,6 +225,16 @@ struct option {
> .help = (h), \
> .flags = (f), \
> }
> +#define OPT_UNSIGNED_F(s, l, v, h, f) { \
> + .type = OPTION_UNSIGNED, \
> + .short_name = (s), \
> + .long_name = (l), \
> + .value = (v), \
> + .precision = sizeof(*v), \
> + .argh = N_("n"), \
> + .help = (h), \
> + .flags = (f), \
> +}
>
> #define OPT_END() { \
> .type = OPTION_END, \
> @@ -276,6 +287,7 @@ struct option {
> #define OPT_CMDMODE(s, l, v, h, i) OPT_CMDMODE_F(s, l, v, h, i, 0)
>
> #define OPT_INTEGER(s, l, v, h) OPT_INTEGER_F(s, l, v, h, 0)
> +#define OPT_UNSIGNED(s, l, v, h) OPT_UNSIGNED_F(s, l, v, h, 0)
> #define OPT_MAGNITUDE(s, l, v, h) { \
> .type = OPTION_MAGNITUDE, \
> .short_name = (s), \
> diff --git a/t/helper/test-parse-options.c b/t/helper/test-parse-options.c
> index 46deb4317ef..0d559288d9c 100644
> --- a/t/helper/test-parse-options.c
> +++ b/t/helper/test-parse-options.c
> @@ -120,7 +120,7 @@ int cmd__parse_options(int argc, const char **argv)
> };
> struct string_list expect = STRING_LIST_INIT_NODUP;
> struct string_list list = STRING_LIST_INIT_NODUP;
> - uint16_t m16 = 0;
> + uint16_t m16 = 0, u16 = 0;
> int16_t i16 = 0;
>
> struct option options[] = {
> @@ -142,6 +142,7 @@ int cmd__parse_options(int argc, const char **argv)
> OPT_GROUP(""),
> OPT_INTEGER('i', "integer", &integer, "get a integer"),
> OPT_INTEGER(0, "i16", &i16, "get a 16 bit integer"),
> + OPT_UNSIGNED(0, "u16", &u16, "get a 16 bit unsigned integer"),
> OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
> OPT_MAGNITUDE('m', "magnitude", &magnitude, "get a magnitude"),
> OPT_MAGNITUDE(0, "m16", &m16, "get a 16 bit magnitude"),
> @@ -215,6 +216,7 @@ int cmd__parse_options(int argc, const char **argv)
> show(&expect, &ret, "boolean: %d", boolean);
> show(&expect, &ret, "integer: %d", integer);
> show(&expect, &ret, "i16: %"PRIdMAX, (intmax_t) i16);
> + show(&expect, &ret, "u16: %"PRIuMAX, (uintmax_t) u16);
> show(&expect, &ret, "magnitude: %lu", magnitude);
> show(&expect, &ret, "m16: %"PRIuMAX, (uintmax_t) m16);
> show(&expect, &ret, "timestamp: %"PRItime, timestamp);
> diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh
> index 5f503b26cc8..9946e69f586 100755
> --- a/t/t0040-parse-options.sh
> +++ b/t/t0040-parse-options.sh
> @@ -23,6 +23,7 @@ usage: test-tool parse-options <options>
> -i, --[no-]integer <n>
> get a integer
> --[no-]i16 <n> get a 16 bit integer
> + --[no-]u16 <n> get a 16 bit unsigned integer
> -j <n> get a integer, too
> -m, --magnitude <n> get a magnitude
> --m16 <n> get a 16 bit magnitude
> @@ -139,6 +140,7 @@ cat >expect <<\EOF
> boolean: 2
> integer: 1729
> i16: 0
> +u16: 0
> magnitude: 16384
> m16: 0
> timestamp: 0
> @@ -161,6 +163,7 @@ cat >expect <<\EOF
> boolean: 2
> integer: 1729
> i16: 9000
> +u16: 5432
> magnitude: 16384
> m16: 32768
> timestamp: 0
> @@ -173,7 +176,7 @@ file: prefix/fi.le
> EOF
>
> test_expect_success 'long options' '
> - test-tool parse-options --boolean --integer 1729 --i16 9000 --magnitude 16k \
> + test-tool parse-options --boolean --integer 1729 --i16 9000 --u16 5432 --magnitude 16k \
> --m16 32k --boolean --string2=321 --verbose --verbose --no-dry-run \
> --abbrev=10 --file fi.le --obsolete \
> >output 2>output.err &&
> @@ -186,6 +189,7 @@ test_expect_success 'abbreviate to something longer than SHA1 length' '
> boolean: 0
> integer: 0
> i16: 0
> + u16: 0
> magnitude: 0
> m16: 0
> timestamp: 0
> @@ -262,6 +266,7 @@ cat >expect <<\EOF
> boolean: 1
> integer: 13
> i16: 0
> +u16: 0
> magnitude: 0
> m16: 0
> timestamp: 0
> @@ -287,6 +292,7 @@ cat >expect <<\EOF
> boolean: 0
> integer: 2
> i16: 0
> +u16: 0
> magnitude: 0
> m16: 0
> timestamp: 0
> @@ -356,6 +362,7 @@ Callback: "four", 0
> boolean: 5
> integer: 4
> i16: 0
> +u16: 0
> magnitude: 0
> m16: 0
> timestamp: 0
> @@ -383,6 +390,7 @@ cat >expect <<\EOF
> boolean: 1
> integer: 23
> i16: 0
> +u16: 0
> magnitude: 0
> m16: 0
> timestamp: 0
> @@ -464,6 +472,7 @@ cat >expect <<\EOF
> boolean: 0
> integer: 0
> i16: 0
> +u16: 0
> magnitude: 0
> m16: 0
> timestamp: 0
> @@ -820,4 +829,11 @@ test_expect_success 'm16 limits range' '
> test_grep "value 65536 for option .m16. exceeds 65535" err
> '
>
> +test_expect_success 'u16 limits range' '
> + test-tool parse-options --u16 65535 >out &&
> + test_grep "u16: 65535" out &&
> + test_must_fail test-tool parse-options --u16 65536 2>err &&
> + test_grep "value 65536 for option .u16. exceeds 65535" err
> +'
> +
> test_done
>
next prev parent reply other threads:[~2025-04-15 15:52 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-01 15:01 [PATCH 0/5] parse-options: harden handling of integer values Patrick Steinhardt
2025-04-01 15:01 ` [PATCH 1/5] global: use designated initializers for options Patrick Steinhardt
2025-04-01 15:01 ` [PATCH 2/5] parse-options: introduce precision handling for `OPTION_INTEGER` Patrick Steinhardt
2025-04-01 18:47 ` René Scharfe
2025-04-15 10:26 ` Patrick Steinhardt
2025-04-01 15:01 ` [PATCH 3/5] parse-options: introduce precision handling for `OPTION_MAGNITUDE` Patrick Steinhardt
2025-04-01 15:01 ` [PATCH 4/5] parse-options: introduce `OPTION_UNSIGNED` Patrick Steinhardt
2025-04-01 15:01 ` [PATCH 5/5] parse-options: detect mismatches in integer signedness Patrick Steinhardt
2025-04-15 12:14 ` [PATCH v2 0/5] parse-options: harden handling of integer values Patrick Steinhardt
2025-04-15 12:14 ` [PATCH v2 1/5] global: use designated initializers for options Patrick Steinhardt
2025-04-15 12:14 ` [PATCH v2 2/5] parse-options: introduce precision handling for `OPTION_INTEGER` Patrick Steinhardt
2025-04-15 15:51 ` Phillip Wood
2025-04-16 10:28 ` Patrick Steinhardt
2025-04-15 16:59 ` Junio C Hamano
2025-04-16 10:28 ` Patrick Steinhardt
2025-04-15 12:14 ` [PATCH v2 3/5] parse-options: introduce precision handling for `OPTION_MAGNITUDE` Patrick Steinhardt
2025-04-15 12:14 ` [PATCH v2 4/5] parse-options: introduce `OPTION_UNSIGNED` Patrick Steinhardt
2025-04-15 15:52 ` Phillip Wood [this message]
2025-04-16 10:27 ` Patrick Steinhardt
2025-04-16 13:31 ` phillip.wood123
2025-04-15 17:38 ` René Scharfe
2025-04-16 10:28 ` Patrick Steinhardt
2025-04-15 12:14 ` [PATCH v2 5/5] parse-options: detect mismatches in integer signedness Patrick Steinhardt
2025-04-15 17:02 ` Junio C Hamano
2025-04-16 10:02 ` [PATCH v3 0/7] parse-options: harden handling of integer values Patrick Steinhardt
2025-04-16 10:02 ` [PATCH v3 1/7] global: use designated initializers for options Patrick Steinhardt
2025-04-16 10:02 ` [PATCH v3 2/7] parse-options: check for overflow when parsing integers Patrick Steinhardt
2025-04-16 10:02 ` [PATCH v3 3/7] parse-options: introduce precision handling for `OPTION_INTEGER` Patrick Steinhardt
2025-04-16 17:29 ` Junio C Hamano
2025-04-16 10:02 ` [PATCH v3 4/7] parse-options: introduce precision handling for `OPTION_MAGNITUDE` Patrick Steinhardt
2025-04-16 10:02 ` [PATCH v3 5/7] parse-options: introduce `OPTION_UNSIGNED` Patrick Steinhardt
2025-04-16 18:50 ` Junio C Hamano
2025-04-17 8:15 ` Patrick Steinhardt
2025-04-16 10:02 ` [PATCH v3 6/7] parse-options: detect mismatches in integer signedness Patrick Steinhardt
2025-04-16 10:02 ` [PATCH v3 7/7] parse-options: introduce bounded integer options Patrick Steinhardt
2025-04-16 19:19 ` Junio C Hamano
2025-04-17 8:14 ` Patrick Steinhardt
2025-04-17 10:49 ` [PATCH v4 0/7] parse-options: harden handling of integer values Patrick Steinhardt
2025-04-17 10:49 ` [PATCH v4 1/7] parse: fix off-by-one for minimum signed values Patrick Steinhardt
2025-04-17 10:49 ` [PATCH v4 2/7] global: use designated initializers for options Patrick Steinhardt
2025-04-17 10:49 ` [PATCH v4 3/7] parse-options: support unit factors in `OPT_INTEGER()` Patrick Steinhardt
2025-04-17 10:49 ` [PATCH v4 4/7] parse-options: rename `OPT_MAGNITUDE()` to `OPT_UNSIGNED()` Patrick Steinhardt
2025-04-17 15:17 ` Junio C Hamano
2025-04-17 10:49 ` [PATCH v4 5/7] parse-options: introduce precision handling for `OPTION_INTEGER` Patrick Steinhardt
2025-04-17 10:49 ` [PATCH v4 6/7] parse-options: introduce precision handling for `OPTION_UNSIGNED` Patrick Steinhardt
2025-04-17 10:49 ` [PATCH v4 7/7] parse-options: detect mismatches in integer signedness Patrick Steinhardt
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=35300426-32cc-4c0e-b0c3-edb2b2ed312f@gmail.com \
--to=phillip.wood123@gmail.com \
--cc=git@vger.kernel.org \
--cc=glaubitz@physik.fu-berlin.de \
--cc=l.s.r@web.de \
--cc=peff@peff.net \
--cc=phillip.wood@dunelm.org.uk \
--cc=ps@pks.im \
--cc=stolee@gmail.com \
--cc=szeder.dev@gmail.com \
--cc=tmz@pobox.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).