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

[RFC PATCH 4/5] pack-objects: avoid reading uninitalized data

From
Jeff King <peff@peff.net>
Date
Oct 22, 2008, 20:31 UTC
Message-ID
<20081022203103.GD4547@coredump.intra.peff.net>
In-Reply-To
<20081022202810.GA4439@coredump.intra.peff.net>
In the main loop of find_deltas, we do:
  struct object_entry *entry = *list++;
  ...
  if (!*list_size)
	  ...
	  break

Because we look at and increment *list _before_ the check of list_size, in the very last iteration of the loop we will look at uninitialized data, and increment the pointer beyond one past the end of the allocated space. Since we don't actually do anything with the data until after the check, this is not a problem in practice.

But since it technically violates the C standard, and because it provokes a spurious valgrind warning, let's just move the initialization of entry to a safe place.

This fixes valgrind errors in t5300, t5301, t5302, t303, and t9400.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin-pack-objects.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
diff --git a/builtin-pack-objects.c b/builtin-pack-objects.c
index 59c30d1..15b80db 100644
--- a/builtin-pack-objects.c
+++ b/builtin-pack-objects.c
@@ -1375,7 +1375,7 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
 	array = xcalloc(window, sizeof(struct unpacked));
 
 	for (;;) {
-		struct object_entry *entry = *list++;
+		struct object_entry *entry;
 		struct unpacked *n = array + idx;
 		int j, max_depth, best_base = -1;
 
@@ -1384,6 +1384,7 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
 			progress_unlock();
 			break;
 		}
+		entry = *list++;
 		(*list_size)--;
 		if (!entry->preferred_base) {
 			(*processed)++;
-- 
1.6.0.2.825.g6d19d
Previous: Jeff KingNext: Nicolas Pitre
Message 9 of 13 in “valgrind in test scripts”
  1. 0/5 valgrind in test scriptsJeff King, Oct 22, 2008
  2. 1/5 add valgrind support in test scriptsJeff King, Oct 22, 2008
  3. Johannes SchindelinOct 22, 2008
  4. Junio C HamanoOct 23, 2008
  5. Jeff KingOct 23, 2008
  6. Jeff KingOct 23, 2008
  7. 2/5 valgrind: ignore ldso errorsJeff King, Oct 22, 2008
  8. 3/5 correct cache_entry allocationJeff King, Oct 22, 2008
  9. 4/5 pack-objects: avoid reading uninitalized dataJeff King, Oct 22, 2008
  10. Nicolas PitreOct 23, 2008
  11. Jeff KingOct 23, 2008
  12. Nicolas PitreOct 23, 2008
  13. 5/5 fix overlapping memcpy in normalize_absolute_pathJeff King, Oct 22, 2008

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.