git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v3 3/7] parse-options: introduce precision handling for `OPTION_INTEGER`

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 16, 2025, 17:29 UTC
Message-ID
<xmqq1ptshv5k.fsf@gitster.g>
In-Reply-To
<20250416-b4-pks-parse-options-integers-v3-3-d390746bea79@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 46 quoted lines
> The `OPTION_INTEGER` option type accepts a signed integer. The type of
> the underlying integer is a simple `int`, which restricts the range of
> values accepted by such options. But there is a catch: because the
> caller provides a pointer to the value via the `.value` field, which is
> a simple void pointer. This has two consequences:
>
>   - There is no check whether the passed value is sufficiently long to
>     store the entire range of `int`. This can lead to integer wraparound
>     in the best case and out-of-bounds writes in the worst case.
>
>   - Even when a caller knows that they want to store a value larger than
>     `INT_MAX` they don't have a way to do so.
>
> Funny enough, even if the caller gets everything correct the parsing
> logic is still insufficient because we use `strtol()` to parse the
> argument, which returns a `long`. But as that value is implicitly cast
> when assigning it to the `int` field we may still get invalid results.
>
> In practice this doesn't tend to be a huge issue because users typically
> don't end up passing huge values to most commands. But the parsing logic
> is demonstrably broken, and it is too easy to get the calling convention
> wrong.
>
> Improve the situation by introducing a new `precision` field into the
> structure. This field gets assigned automatically by `OPT_INTEGER_F()`
> and tracks the size of the passed value. Like this it becomes possible
> for the caller to pass arbitrarily-sized integers and the underlying
> logic knows to handle it correctly by doing range checks. Furthermore,
> convert the code to use `strtoimax()` intstead of `strtol()` so that we
> can also parse values larger than `LONG_MAX`.
>
> Note that we do not yet assert signedness of the passed variable, which
> is another source of bugs. This will be handled in a subsequent commit.
>
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
> ...
> +	{
> +		intmax_t upper_bound = INTMAX_MAX >> (bitsizeof(intmax_t) - CHAR_BIT * opt->precision);
> +		intmax_t lower_bound = -upper_bound - 1;
> +		intmax_t value;
> +
>  		if (unset) {
> ...
> +		if (value < lower_bound || value > upper_bound)
>  			return error(_("value %s for %s not in range [%"PRIdMAX",%"PRIdMAX"]"),

When I imagined the case where precision is set to the same size as sizeof(intmax_t), this comparison made my head spin, but I think it should safely yield false for any "value", in which case it is fine.

Show 14 quoted lines
> +		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:

Good to have this "default" arm. As you cannot take an address of a bitfield, you cannot pass a bitfield member in a struct that is set to say 24-bit wide with .precision set to 3. IOW, limiting ourselves only to sizes of naturally occurring integral types is sufficient.

Show 19 quoted lines
> +			BUG("invalid precision for option %s",
> +			    optname(opt, flags));
> +		}
> +	}
>  	case OPTION_MAGNITUDE:
>  		if (unset) {
>  			*(unsigned long *)opt->value = 0;
> diff --git a/parse-options.h b/parse-options.h
> index 997ffbee805..8db96402c4d 100644
> --- a/parse-options.h
> +++ b/parse-options.h
> @@ -92,6 +92,10 @@ typedef int parse_opt_subcommand_fn(int argc, const char **argv,
>   * `value`::
>   *   stores pointers to the values to be filled.
>   *
> + * `precision`::
> + *   precision of the integer pointed to by `value` in number of bytes. Should
> + *   typically be its `sizeof()`.
> + *

Nicer to see "bytes" in the description. I am still puzzled, and more importantly, I suspect readers will be puzzled, when told "Should typically be", as it implies there may be valid cases when it can be different from it, and wanting to know what these cases are.

Show 8 quoted lines
> @@ -214,6 +219,7 @@ struct option {
>  	.short_name = (s), \
>  	.long_name = (l), \
>  	.value = (v), \
> +	.precision = sizeof(*v), \
>  	.argh = N_("n"), \
>  	.help = (h), \
>  	.flags = (f), \

Nice. By doing this in OPT_INTEGER_F(), we cover OPT_INTEGER() as well automatically.

Thanks.
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 30 of 37 in “parse-options: harden handling of integer values”
  1. 0/5 parse-options: harden handling of integer valuesPatrick Steinhardt, Apr 1, 2025
  2. 2/5 parse-options: introduce precision handling for `OPTION_INTEGER`Patrick Steinhardt, Apr 1, 2025
  3. René ScharfeApr 1, 2025
  4. Patrick SteinhardtApr 15, 2025
  5. 3/5 parse-options: introduce precision handling for `OPTION_MAGNITUDE`Patrick Steinhardt, Apr 1, 2025
  6. 4/5 parse-options: introduce `OPTION_UNSIGNED`Patrick Steinhardt, Apr 1, 2025
  7. 1/5 global: use designated initializers for optionsPatrick Steinhardt, Apr 1, 2025
  8. 5/5 parse-options: detect mismatches in integer signednessPatrick Steinhardt, Apr 1, 2025
  9. 0/5 parse-options: harden handling of integer valuesPatrick Steinhardt, Apr 15, 2025
  10. 1/5 global: use designated initializers for optionsPatrick Steinhardt, Apr 15, 2025
  11. 2/5 parse-options: introduce precision handling for `OPTION_INTEGER`Patrick Steinhardt, Apr 15, 2025
  12. Phillip WoodApr 15, 2025
  13. Patrick SteinhardtApr 16, 2025
  14. Junio C HamanoApr 15, 2025
  15. Patrick SteinhardtApr 16, 2025
  16. 3/5 parse-options: introduce precision handling for `OPTION_MAGNITUDE`Patrick Steinhardt, Apr 15, 2025
  17. 4/5 parse-options: introduce `OPTION_UNSIGNED`Patrick Steinhardt, Apr 15, 2025
  18. Phillip WoodApr 15, 2025
  19. Patrick SteinhardtApr 16, 2025
  20. phillip.wood123@gmail.comApr 16, 2025
  21. René ScharfeApr 15, 2025
  22. Patrick SteinhardtApr 16, 2025
  23. 5/5 parse-options: detect mismatches in integer signednessPatrick Steinhardt, Apr 15, 2025
  24. Junio C HamanoApr 15, 2025
  25. 0/7 parse-options: harden handling of integer valuesPatrick Steinhardt, Apr 16, 2025
  26. 2/7 parse-options: check for overflow when parsing integersPatrick Steinhardt, Apr 16, 2025
  27. 1/7 global: use designated initializers for optionsPatrick Steinhardt, Apr 16, 2025
  28. 4/7 parse-options: introduce precision handling for `OPTION_MAGNITUDE`Patrick Steinhardt, Apr 16, 2025
  29. 3/7 parse-options: introduce precision handling for `OPTION_INTEGER`Patrick Steinhardt, Apr 16, 2025
  30. Junio C HamanoApr 16, 2025
  31. 5/7 parse-options: introduce `OPTION_UNSIGNED`Patrick Steinhardt, Apr 16, 2025
  32. Junio C HamanoApr 16, 2025
  33. Patrick SteinhardtApr 17, 2025
  34. 7/7 parse-options: introduce bounded integer optionsPatrick Steinhardt, Apr 16, 2025
  35. Junio C HamanoApr 16, 2025
  36. Patrick SteinhardtApr 17, 2025
  37. 6/7 parse-options: detect mismatches in integer signednessPatrick Steinhardt, Apr 16, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.