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

[PATCH] load_subtree(): check that `prefix_len` is in the expected range

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Sep 8, 2017, 16:10 UTC
Message-ID
<ca6046b8b955df6a798b690fdecb0b8ba47d57a9.1504886586.git.mhagger@alum.mit.edu>

This value, which is stashed in the last byte of an object_id hash, gets handed around a lot. So add a sanity check before using it in `load_subtree()`.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
This patch is an addendum to v1 of the mh/notes-cleanup patch series
[1]. It adds the assertion that was suggested by Junio [2].

Since the first patch series is already in next, this patch is constructed to apply on top of that branch.

Thanks to Junio and Johan for their review of v1.
Michael

[1] https://public-inbox.org/git/cover.1503734566.git.mhagger@alum.mit.edu/ [2] https://public-inbox.org/git/xmqqh8wuqo6e.fsf@gitster.mtv.corp.google.com/

 notes.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/notes.c b/notes.c
index 40d9ba6252..27d232f294 100644
--- a/notes.c
+++ b/notes.c
@@ -417,7 +417,10 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
 		     oid_to_hex(&subtree->val_oid));
 
 	prefix_len = subtree->key_oid.hash[KEY_INDEX];
-	assert(prefix_len * 2 >= n);
+	if (prefix_len >= GIT_SHA1_RAWSZ)
+		BUG("prefix_len (%"PRIuMAX") is out of range", (uintmax_t)prefix_len);
+	if (prefix_len * 2 < n)
+		BUG("prefix_len (%"PRIuMAX") is too small", (uintmax_t)prefix_len);
 	memcpy(object_oid.hash, subtree->key_oid.hash, prefix_len);
 	while (tree_entry(&desc, &entry)) {
 		unsigned char type;
-- 
2.14.1
Message 1 of 1 in “load_subtree(): check that `prefix_len` is in the expected range”
  1. load_subtree(): check that `prefix_len` is in the expected rangeMichael Haggerty, Sep 8, 2017

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.