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

[PATCH v2] last-modified: fix use of uninitialized memory

From
Toon Claes <toon@iotcl.com>
Date
Dec 8, 2025, 11:46 UTC
Message-ID
<20251208-toon-big-endian-ci-v2-1-76b46763a597@iotcl.com>
In-Reply-To
<20251128-toon-big-endian-ci-v1-1-80da0f629c1e@iotcl.com>

git-last-modified(1) uses a scratch bitmap to keep track of paths that have been changed between commits. To avoid reallocating a bitmap on each call of process_parent(), the scratch bitmap is kept and reused. Although, between loops, the memory allocated for the 'scratch' bitmap isn't correctly wiped.

`struct bitmap` uses `eword_t` to for internal storage. This type is typedef'd to uint64_t. To fully zero the memory used by the bitmap, the length (saved in `struct bitmap::word_alloc`) should be multiplied by the size of a single item. To simplify zeroing an array, a macro MEMZERO_ARRAY() is defined and used.

Reported-by: Anders Kaseorg <andersk@mit.edu>
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Toon Claes <toon@iotcl.com>
---
It was reported [1] the tests in t8020 fail on s390x. After some
research, it seems it was related to s390x being big-endian. Well,
actually, not really. Using big-endian simply uncovered the problem in
test.
[1]: https://lore.kernel.org/git/4dc4c8cd-c0cc-4784-8fcf-defa3a051087@mit.edu/
---
Changes in v2:
- Defined and used MEMZERO_ARRAY() macro.
- Fixed up title which used unexisting word
- Link to v1: https://lore.kernel.org/r/20251128-toon-big-endian-ci-v1-1-80da0f629c1e@iotcl.com
---
 builtin/last-modified.c | 2 +-
 git-compat-util.h       | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/builtin/last-modified.c b/builtin/last-modified.c
index b0ecbdc540..ac5387e861 100644
--- a/builtin/last-modified.c
+++ b/builtin/last-modified.c
@@ -327,7 +327,7 @@ static void process_parent(struct last_modified *lm,
 	if (!(parent->object.flags & PARENT1))
 		active_paths_free(lm, parent);
 
-	memset(lm->scratch->words, 0x0, lm->scratch->word_alloc);
+	MEMZERO_ARRAY(lm->scratch->words, lm->scratch->word_alloc);
 	diff_queue_clear(&diff_queued_diff);
 }
 
diff --git a/git-compat-util.h b/git-compat-util.h
index 398e0fac4f..2b8192fd2e 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -726,6 +726,7 @@ static inline uint64_t u64_add(uint64_t a, uint64_t b)
 #define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
 #define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x)))
 #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
+#define MEMZERO_ARRAY(x, alloc) memset((x), 0x0, st_mult(sizeof(*(x)), (alloc)))
 
 #define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \
 	BARF_UNLESS_COPYABLE((dst), (src)))

---
base-commit: bdc5341ff65278a3cc80b2e8a02a2f02aa1fac06
change-id: 20251126-toon-big-endian-ci-fe62bb361974
Previous: Toon ClaesNext: Junio C Hamano
Message 11 of 14 in “last-modified: fix bug caused by inproper initialized memory”
  1. last-modified: fix bug caused by inproper initialized memoryToon Claes, Nov 28, 2025
  2. Jeff KingNov 28, 2025
  3. Anders KaseorgNov 28, 2025
  4. Jeff KingNov 29, 2025
  5. Toon ClaesDec 8, 2025
  6. Jeff KingDec 8, 2025
  7. Junio C HamanoDec 8, 2025
  8. Junio C HamanoNov 29, 2025
  9. Junio C HamanoNov 29, 2025
  10. Toon ClaesNov 29, 2025
  11. last-modified: fix use of uninitialized memoryToon Claes, Dec 8, 2025
  12. Junio C HamanoDec 8, 2025
  13. Toon ClaesDec 9, 2025
  14. Junio C HamanoDec 9, 2025

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.