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

Re: [PATCH v2 1/3] git_parse_unsigned: reject negative values

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Nov 9, 2022, 15:57 UTC
Message-ID
<221109.86pmdwp19z.gmgdl@evledraar.gmail.com>
In-Reply-To
<d1ac79909b9e777cae40a6a301e5cfd988c5f9d7.1668003388.git.gitgitgadget@gmail.com>
On Wed, Nov 09 2022, Phillip Wood via GitGitGadget wrote:
Show 39 quoted lines
> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>
> git_parse_unsigned() relies on strtoumax() which unfortunately parses
> negative values as large positive integers. Fix this by rejecting any
> string that contains '-' as we do in strtoul_ui(). I've chosen to treat
> negative numbers as invalid input and set errno to EINVAL rather than
> ERANGE one the basis that they are never acceptable if we're looking for
> a unsigned integer. This is also consistent with the existing behavior
> of rejecting "1–2" with EINVAL.
>
> As we do not have unit tests for this function it is tested indirectly
> by checking that negative values of reject for core.bigFileThreshold are
> rejected. As this function is also used by OPT_MAGNITUDE() a test is
> added to check that rejects negative values too.
>
> Helped-by: Jeff King <peff@peff.net>
> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
> ---
>  config.c                 | 5 +++++
>  t/t0040-parse-options.sh | 5 +++++
>  t/t1050-large.sh         | 6 ++++++
>  3 files changed, 16 insertions(+)
>
> diff --git a/config.c b/config.c
> index cbb5a3bab74..d5069d4f01d 100644
> --- a/config.c
> +++ b/config.c
> @@ -1193,6 +1193,11 @@ static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
>  		uintmax_t val;
>  		uintmax_t factor;
>  
> +		/* negative values would be accepted by strtoumax */
> +		if (strchr(value, '-')) {
> +			errno = EINVAL;
> +			return 0;
> +		}
>  		errno = 0;
>  		val = strtoumax(value, &end, 0);
>  		if (errno == ERANGE)

There's nothing wrong with this, but since the topic here is "some issues I noticed" here's another one: We don't actually care if you set "errno = EINVAL" here in particular, just as long as it's not "ERANGE", anything else will do.

So, not worth a re-roll in itself, but maybe a prep patch (or follow-up) to do this would be nice? to make sure this errno handling is "reachable"?

diff --git a/config.c b/config.c
index ff4ea29784b..33d05fde0ea 100644
--- a/config.c
+++ b/config.c
@@ -1260,9 +1260,12 @@ NORETURN
 static void die_bad_number(const char *name, const char *value)
 {
 	const char *error_type = (errno == ERANGE) ?
-		N_("out of range") : N_("invalid unit");
+		N_("out of range") : errno == EINVAL ? N_("invalid unit") : NULL;
 	const char *bad_numeric = N_("bad numeric config value '%s' for '%s': %s");
 
+	if (!error_type)
+		BUG("unhandled errno %d: %s", errno, strerror(errno));
+
 	if (!value)
 		value = "";
 
Previous: Phillip Wood via GitGitGadgetNext: Phillip Wood via GitGitGadget
Message 24 of 27 in “a few config integer parsing fixes”
  1. 0/3 a few config integer parsing fixesPhillip Wood via GitGitGadget, Oct 21, 2022
  2. 1/3 git_parse_unsigned: reject negative valuesPhillip Wood via GitGitGadget, Oct 21, 2022
  3. Junio C HamanoOct 21, 2022
  4. Jeff KingOct 21, 2022
  5. Junio C HamanoOct 22, 2022
  6. 2/3 config: require at least one digit when parsing numbersPhillip Wood via GitGitGadget, Oct 21, 2022
  7. Junio C HamanoOct 21, 2022
  8. Phillip WoodOct 25, 2022
  9. Junio C HamanoOct 25, 2022
  10. Jeff KingOct 21, 2022
  11. Junio C HamanoOct 22, 2022
  12. Jeff KingOct 22, 2022
  13. Junio C HamanoOct 22, 2022
  14. Phillip WoodOct 25, 2022
  15. 3/3 git_parse_signed(): avoid integer overflowPhillip Wood via GitGitGadget, Oct 21, 2022
  16. Junio C HamanoOct 21, 2022
  17. René ScharfeOct 22, 2022
  18. Junio C HamanoOct 22, 2022
  19. René ScharfeOct 23, 2022
  20. Phillip WoodOct 25, 2022
  21. René ScharfeOct 26, 2022
  22. 0/3 a few config integer parsing fixesPhillip Wood via GitGitGadget, Nov 9, 2022
  23. 1/3 git_parse_unsigned: reject negative valuesPhillip Wood via GitGitGadget, Nov 9, 2022
  24. Ævar Arnfjörð BjarmasonNov 9, 2022
  25. 2/3 config: require at least one digit when parsing numbersPhillip Wood via GitGitGadget, Nov 9, 2022
  26. 3/3 git_parse_signed(): avoid integer overflowPhillip Wood via GitGitGadget, Nov 9, 2022
  27. Taylor BlauNov 10, 2022

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.