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

[PATCH v2 0/5] parse-options: harden handling of integer values

From
Patrick Steinhardt <ps@pks.im>
Date
Apr 15, 2025, 12:14 UTC
Message-ID
<20250415-b4-pks-parse-options-integers-v2-0-ce07441a1f01@pks.im>
In-Reply-To
<20250401-b4-pks-parse-options-integers-v1-0-a628ad40c3b4@pks.im>
Hi,

this patch series addresses the issues raised in [1] and [2]. As discussed in [1], the series also introduces a couple of safeguards to make it harder to misuse `OPT_INTEGER()` and `OPT_MAGNITUDE()`:

  - We now track the precision of the underlying integer types. This
    makes it possible to pass arbitrarily-sized integers to those
    options, not only `int` and `unsigned long`, respectively.
  - We introduce a build assert to verify that the passed variable has
    correct signedness.

Furthermore, the series introduces `OPT_UNSIGNED()` to adapt all callsites that previously used variables with the wrong signedness.

Changes in v2:
  - Adapt computation of upper bounds to use similar logic to
    `maximum_signed_value_of_type()`.
  - Link to v1: https://lore.kernel.org/r/20250401-b4-pks-parse-options-integers-v1-0-a628ad40c3b4@pks.im
Thanks!
Patrick

[1]: <89257ab82cd60d135cce02d51eacee7ec35c1c37.camel@physik.fu-berlin.de> [2]: <Z8HW6petWuMRWSXf@teonanacatl.net>

---
Patrick Steinhardt (5):
      global: use designated initializers for options
      parse-options: introduce precision handling for `OPTION_INTEGER`
      parse-options: introduce precision handling for `OPTION_MAGNITUDE`
      parse-options: introduce `OPTION_UNSIGNED`
      parse-options: detect mismatches in integer signedness
 apply.c                       |   4 +-
 archive.c                     |  35 ++++++++---
 builtin/am.c                  |  28 ++++++---
 builtin/backfill.c            |   4 +-
 builtin/clone.c               |  13 ++++-
 builtin/column.c              |   2 +-
 builtin/commit-tree.c         |  12 +++-
 builtin/commit.c              |  62 +++++++++++++++-----
 builtin/config.c              |  13 ++++-
 builtin/describe.c            |  24 ++++++--
 builtin/fetch.c               |  10 +++-
 builtin/fmt-merge-msg.c       |  27 ++++++---
 builtin/gc.c                  |  12 +++-
 builtin/grep.c                |  18 ++++--
 builtin/init-db.c             |  13 +++--
 builtin/ls-remote.c           |  11 +++-
 builtin/merge.c               |  38 +++++++++---
 builtin/read-tree.c           |  11 +++-
 builtin/rebase.c              |  25 ++++++--
 builtin/revert.c              |  12 +++-
 builtin/show-branch.c         |  13 ++++-
 builtin/tag.c                 |  24 ++++++--
 builtin/update-index.c        | 131 +++++++++++++++++++++++++++++-------------
 builtin/write-tree.c          |  12 ++--
 diff.c                        |  13 +++--
 git-compat-util.h             |   7 +++
 parse-options.c               | 131 +++++++++++++++++++++++++++++++++++-------
 parse-options.h               |  23 +++++++-
 ref-filter.h                  |  15 +++--
 t/helper/test-parse-options.c |  46 ++++++++++++---
 t/t0040-parse-options.sh      |  57 +++++++++++++++++-
 31 files changed, 657 insertions(+), 189 deletions(-)
Range-diff versus v1:
1:  632c627b9ba = 1:  92d1cbd1153 global: use designated initializers for options
2:  65c450a4395 ! 2:  8d2400d7470 parse-options: introduce precision handling for `OPTION_INTEGER`
    @@ parse-options.c: static enum parse_opt_result do_get_value(struct parse_opt_ctx_
      	}
      	case OPTION_INTEGER:
     +	{
    -+		intmax_t upper_bound = (((intmax_t) 1 << (opt->precision * 8 - 1)) - 1);
    ++		intmax_t upper_bound = INTMAX_MAX >> (bitsizeof(intmax_t) - CHAR_BIT * opt->precision);
     +		intmax_t lower_bound = -upper_bound - 1;
     +		intmax_t value;
     +
3:  4946ca0f702 ! 3:  a9b0d8c1127 parse-options: introduce precision handling for `OPTION_MAGNITUDE`
    @@ parse-options.c: static enum parse_opt_result do_get_value(struct parse_opt_ctx_
      	}
      	case OPTION_MAGNITUDE:
     +	{
    -+		uintmax_t upper_bound = 0;
    ++		uintmax_t upper_bound = UINTMAX_MAX >> (bitsizeof(uintmax_t) - CHAR_BIT * opt->precision);
     +		unsigned long value;
    -+
    -+		/*
    -+		 * It's stupid, but the obvious way of calculating the upper
    -+		 * bound via `2 ^ n - 1` overflows.
    -+		 */
    -+		for (size_t i = 0; i < opt->precision * 8; i++)
    -+			upper_bound |= ((uintmax_t) 1 << i);
     +
      		if (unset) {
     -			*(unsigned long *)opt->value = 0;
4:  9728d57d5df ! 4:  743afbf539e parse-options: introduce `OPTION_UNSIGNED`
    @@ parse-options.c: static enum parse_opt_result do_get_value(struct parse_opt_ctx_
      	}
     +	case OPTION_UNSIGNED:
     +	{
    -+		uintmax_t upper_bound = 0;
    ++		uintmax_t upper_bound = UINTMAX_MAX >> (bitsizeof(uintmax_t) - CHAR_BIT * opt->precision);
     +		uintmax_t value;
     +
    -+		/*
    -+		 * It's stupid, but the obvious way of calculating the upper
    -+		 * bound via `2 ^ n - 1` overflows.
    -+		 */
    -+		for (size_t i = 0; i < opt->precision * 8; i++)
    -+			upper_bound |= ((uintmax_t) 1 << i);
    -+
     +		if (unset) {
     +			value = 0;
     +		} else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
    @@ parse-options.c: static enum parse_opt_result do_get_value(struct parse_opt_ctx_
     +	}
      	case OPTION_MAGNITUDE:
      	{
    - 		uintmax_t upper_bound = 0;
    + 		uintmax_t upper_bound = UINTMAX_MAX >> (bitsizeof(uintmax_t) - CHAR_BIT * opt->precision);
     
      ## parse-options.h ##
     @@ parse-options.h: enum parse_opt_type {
5:  59aacc72548 = 5:  1e42672a439 parse-options: detect mismatches in integer signedness

--- base-commit: 5b97a56fa0e7d580dc8865b73107407c9b3f0eff change-id: 20250401-b4-pks-parse-options-integers-9b4bbcf21011

Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 9 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.