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

Re: [PATCH v2] sha1_name: fix uninitialized memory errors

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 27, 2018, 22:30 UTC
Message-ID
<xmqqsh9mnjzk.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<20180227213305.GB6899@sigill.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 5 quoted lines
> Thanks, this looks good to me.
>
> Semi-related, I wondered also at the weird asymmetry in the if-else,
> ...
> So I think the code is right, but the comment is wrong.
-- >8 --
From: Derrick Stolee <dstolee@microsoft.com>
Date: Tue, 27 Feb 2018 06:47:04 -0500
Subject: [PATCH] sha1_name: fix uninitialized memory errors

During abbreviation checks, we navigate to the position within a pack-index that an OID would be inserted and check surrounding OIDs for the maximum matching prefix. This position may be beyond the last position, because the given OID is lexicographically larger than every OID in the pack. Then nth_packed_object_oid() does not initialize "oid".

Use the return value of nth_packed_object_oid() to prevent these errors.

Also the comment about checking near-by objects miscounts the neighbours. If we have a hit at "first", we check "first-1" and "first+1" to make sure we have sufficiently long abbreviation not to match either. If we do not have a hit, "first" is the smallest among the objects that are larger than what we want to name, so we check that and "first-1" to make sure we have sufficiently long abbreviation not to match either. In either case, we only check up to two near-by objects.

Reported-by: Christian Couder <christian.couder@gmail.com>
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 sha1_name.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/sha1_name.c b/sha1_name.c
index 05a635911b..f1c3d37a6d 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -542,20 +542,20 @@ static void find_abbrev_len_for_pack(struct packed_git *p,
 	/*
 	 * first is now the position in the packfile where we would insert
 	 * mad->hash if it does not exist (or the position of mad->hash if
-	 * it does exist). Hence, we consider a maximum of three objects
+	 * it does exist). Hence, we consider a maximum of two objects
 	 * nearby for the abbreviation length.
 	 */
 	mad->init_len = 0;
 	if (!match) {
-		nth_packed_object_oid(&oid, p, first);
-		extend_abbrev_len(&oid, mad);
+		if (nth_packed_object_oid(&oid, p, first))
+			extend_abbrev_len(&oid, mad);
 	} else if (first < num - 1) {
-		nth_packed_object_oid(&oid, p, first + 1);
-		extend_abbrev_len(&oid, mad);
+		if (nth_packed_object_oid(&oid, p, first + 1))
+			extend_abbrev_len(&oid, mad);
 	}
 	if (first > 0) {
-		nth_packed_object_oid(&oid, p, first - 1);
-		extend_abbrev_len(&oid, mad);
+		if (nth_packed_object_oid(&oid, p, first - 1))
+			extend_abbrev_len(&oid, mad);
 	}
 	mad->init_len = mad->cur_len;
 }
-- 
2.16.2-325-g2fc74f41c5
Previous: Jeff KingNext: Jeff King
Message 10 of 14 in “Use of uninitialised value of size 8 in sha1_name.c”
  1. Christian CouderFeb 26, 2018
  2. Jeff KingFeb 26, 2018
  3. Christian CouderFeb 26, 2018
  4. Derrick StoleeFeb 26, 2018
  5. Christian CouderFeb 26, 2018
  6. sha1_name: fix uninitialized memory errorsDerrick Stolee, Feb 26, 2018
  7. Jeff KingFeb 26, 2018
  8. sha1_name: fix uninitialized memory errorsDerrick Stolee, Feb 27, 2018
  9. Jeff KingFeb 27, 2018
  10. Junio C HamanoFeb 27, 2018
  11. Jeff KingFeb 27, 2018
  12. Junio C HamanoFeb 28, 2018
  13. Derrick StoleeFeb 28, 2018
  14. Junio C HamanoFeb 28, 2018

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.