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

[PATCH v4 1/7] parse: fix off-by-one for minimum signed values

From
Patrick Steinhardt <ps@pks.im>
Date
Apr 17, 2025, 10:49 UTC
Message-ID
<20250417-b4-pks-parse-options-integers-v4-1-9cbc76b61cfe@pks.im>
In-Reply-To
<20250417-b4-pks-parse-options-integers-v4-0-9cbc76b61cfe@pks.im>

We accept a maximum value in `git_parse_signed()` that restricts the range of accepted integers. As the intent is to pass `INT*_MAX` values here, this maximum doesn't only act as the upper bound, but also as the implicit lower bound of the accepted range.

This lower bound is calculated by negating the maximum. But given that the maximum value of a signed integer with N bits is `2^(N-1)-1` whereas the minimum value is `-2^(N-1)` we have an off-by-one error in the lower bound.

Fix this off-by-one error by using `-max - 1` as lower bound instead.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 parse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/parse.c b/parse.c
index 7a60a4f816c..3c47448ca67 100644
--- a/parse.c
+++ b/parse.c
@@ -38,7 +38,7 @@ int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
 			errno = EINVAL;
 			return 0;
 		}
-		if ((val < 0 && -max / factor > val) ||
+		if ((val < 0 && (-max - 1) / factor > val) ||
 		    (val > 0 && max / factor < val)) {
 			errno = ERANGE;
 			return 0;
-- 
2.49.0.805.g082f7c87e0.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 2 of 9 in “parse-options: harden handling of integer values”
  1. 0/7 parse-options: harden handling of integer valuesPatrick Steinhardt, Apr 17, 2025
  2. 1/7 parse: fix off-by-one for minimum signed valuesPatrick Steinhardt, Apr 17, 2025
  3. 2/7 global: use designated initializers for optionsPatrick Steinhardt, Apr 17, 2025
  4. 3/7 parse-options: support unit factors in `OPT_INTEGER()`Patrick Steinhardt, Apr 17, 2025
  5. 4/7 parse-options: rename `OPT_MAGNITUDE()` to `OPT_UNSIGNED()`Patrick Steinhardt, Apr 17, 2025
  6. Junio C HamanoApr 17, 2025
  7. 5/7 parse-options: introduce precision handling for `OPTION_INTEGER`Patrick Steinhardt, Apr 17, 2025
  8. 6/7 parse-options: introduce precision handling for `OPTION_UNSIGNED`Patrick Steinhardt, Apr 17, 2025
  9. 7/7 parse-options: detect mismatches in integer signednessPatrick Steinhardt, Apr 17, 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.