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

[PATCH 2/2] sha1-name: check for overflow of N in "foo^N" and "foo~N"

From
René Scharfe <l.s.r@web.de>
Date
Sep 15, 2019, 12:10 UTC
Message-ID
<2be6e3ee-209e-9cd1-eb43-284f9a8462b3@web.de>
In-Reply-To
<c43610f2-a51f-7333-a200-a76930a0b705@web.de>

Reject values that don't fit into an int, as get_parent() and get_nth_ancestor() cannot handle them. That's better than potentially returning a random object.

If this restriction turns out to be too tight then we can switch to a wider data type, but we'd still have to check for overflow.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 sha1-name.c                    | 15 ++++++++++++---
 t/t1506-rev-parse-diagnosis.sh |  4 ++--
 2 files changed, 14 insertions(+), 5 deletions(-)
diff --git a/sha1-name.c b/sha1-name.c
index c665e3f96d..7a047e9e2b 100644
--- a/sha1-name.c
+++ b/sha1-name.c
@@ -1160,13 +1160,22 @@ static enum get_oid_result get_oid_1(struct repository *r,
 	}

 	if (has_suffix) {
-		int num = 0;
+		unsigned int num = 0;
 		int len1 = cp - name;
 		cp++;
-		while (cp < name + len)
-			num = num * 10 + *cp++ - '0';
+		while (cp < name + len) {
+			unsigned int digit = *cp++ - '0';
+			if (unsigned_mult_overflows(num, 10))
+				return MISSING_OBJECT;
+			num *= 10;
+			if (unsigned_add_overflows(num, digit))
+				return MISSING_OBJECT;
+			num += digit;
+		}
 		if (!num && len1 == len - 1)
 			num = 1;
+		else if (num > INT_MAX)
+			return MISSING_OBJECT;
 		if (has_suffix == '^')
 			return get_parent(r, name, len1, oid, num);
 		/* else if (has_suffix == '~') -- goes without saying */
diff --git a/t/t1506-rev-parse-diagnosis.sh b/t/t1506-rev-parse-diagnosis.sh
index 5c4df47401..6a938b205b 100755
--- a/t/t1506-rev-parse-diagnosis.sh
+++ b/t/t1506-rev-parse-diagnosis.sh
@@ -215,11 +215,11 @@ test_expect_success 'arg before dashdash must be a revision (ambiguous)' '
 	test_cmp expect actual
 '

-test_expect_failure 'reject Nth parent if N is too high' '
+test_expect_success 'reject Nth parent if N is too high' '
 	test_must_fail git rev-parse HEAD^100000000000000000000000000000000
 '

-test_expect_failure 'reject Nth ancestor if N is too high' '
+test_expect_success 'reject Nth ancestor if N is too high' '
 	test_must_fail git rev-parse HEAD~100000000000000000000000000000000
 '

--
2.23.0
Previous: René ScharfeNext: brian m. carlson
Message 2 of 4 in “rev-parse: demonstrate overflow of N for "foo^N" and "foo~N"”
  1. 1/2 rev-parse: demonstrate overflow of N for "foo^N" and "foo~N"René Scharfe, Sep 15, 2019
  2. 2/2 sha1-name: check for overflow of N in "foo^N" and "foo~N"René Scharfe, Sep 15, 2019
  3. brian m. carlsonSep 15, 2019
  4. René ScharfeSep 15, 2019

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.