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

[PATCH 2/3] walker_fetch(): avoid raw array length computation

From
Jeff King <peff@peff.net>
Date
Jan 30, 2020, 09:52 UTC
Message-ID
<20200130095232.GB840531@coredump.intra.peff.net>
In-Reply-To
<20200130095155.GA839988@coredump.intra.peff.net>

We compute the length of an array of object_id's with a raw multiplication. In theory this could trigger an integer overflow which would cause an under-allocation (and eventually an out of bounds write).

I doubt this can be triggered in practice, since you'd need to feed it an enormous number of target objects, which would typically come from the ref advertisement and be using proportional memory. And even on 64-bit systems, where "int" is much smaller than "size_t", that should hold: even though "targets" is an int, the multiplication will be done as a size_t because of the use of sizeof().

But we can easily fix it by using ALLOC_ARRAY(), which uses st_mult() under the hood.

Signed-off-by: Jeff King <peff@peff.net>
---
 walker.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/walker.c b/walker.c
index 06cd2bd569..bb010f7a2b 100644
--- a/walker.c
+++ b/walker.c
@@ -261,12 +261,14 @@ int walker_fetch(struct walker *walker, int targets, char **target,
 	struct strbuf refname = STRBUF_INIT;
 	struct strbuf err = STRBUF_INIT;
 	struct ref_transaction *transaction = NULL;
-	struct object_id *oids = xmalloc(targets * sizeof(struct object_id));
+	struct object_id *oids;
 	char *msg = NULL;
 	int i, ret = -1;
 
 	save_commit_buffer = 0;
 
+	ALLOC_ARRAY(oids, targets);
+
 	if (write_ref) {
 		transaction = ref_transaction_begin(&err);
 		if (!transaction) {
-- 
2.25.0.515.gaba5347bc6
Previous: Jeff KingNext: Jeff King
Message 5 of 13 in “some minor memory allocation cleanups”
  1. 0/3 some minor memory allocation cleanupsJeff King, Jan 30, 2020
  2. 1/3 normalize_path_copy(): document "dst" size expectationsJeff King, Jan 30, 2020
  3. Taylor BlauJan 30, 2020
  4. Jeff KingJan 31, 2020
  5. 2/3 walker_fetch(): avoid raw array length computationJeff King, Jan 30, 2020
  6. 3/3 traverse_trees(): use stack array for name entriesJeff King, Jan 30, 2020
  7. Elijah NewrenJan 30, 2020
  8. Jeff KingJan 31, 2020
  9. Elijah NewrenJan 31, 2020
  10. tree-walk.c: break circular dependency with unpack-treesJeff King, Feb 1, 2020
  11. Elijah NewrenFeb 1, 2020
  12. Elijah NewrenJan 30, 2020
  13. Taylor BlauJan 30, 2020

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.