From: "René Scharfe" <l.s.r@web•de>
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>,
"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 19:38:04 +0200 [thread overview]
Message-ID: <94f4ba9a-81a9-4e3a-932b-faee5aa2d2f4@web.de> (raw)
In-Reply-To: <20250415-b4-pks-parse-options-integers-v2-4-ce07441a1f01@pks.im>
Am 15.04.25 um 14:14 schrieb Patrick Steinhardt:
> We have two generic ways to parse integers in the "parse-options"
> subsytem:
"subsystem"
> - `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.
>
> 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;
uint8_t, surely. Similarly for the other casts below.
> + 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);
next prev parent reply other threads:[~2025-04-15 17:38 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
2025-04-16 10:27 ` Patrick Steinhardt
2025-04-16 13:31 ` phillip.wood123
2025-04-15 17:38 ` René Scharfe [this message]
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=94f4ba9a-81a9-4e3a-932b-faee5aa2d2f4@web.de \
--to=l.s.r@web$(echo .)de \
--cc=git@vger$(echo .)kernel.org \
--cc=glaubitz@physik$(echo .)fu-berlin.de \
--cc=peff@peff$(echo .)net \
--cc=ps@pks$(echo .)im \
--cc=stolee@gmail$(echo .)com \
--cc=szeder.dev@gmail$(echo .)com \
--cc=tmz@pobox$(echo .)com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
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