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

[PATCH] pack-bitmap: fix memory leak if `load_bitmap_entries_v1` failed

From
Lidong Yan via GitGitGadget <gitgitgadget@gmail.com>
Date
May 12, 2025, 12:22 UTC
Message-ID
<pull.1962.git.git.1747052530271.gitgitgadget@gmail.com>
From: Lidong Yan <502024330056@smail.nju.edu.cn>

In pack-bitmap.c:load_bitmap_entries_v1, the function `read_bitmap_1` allocates a bitmap and reads index data into it. However, if any of the validation checks following the allocation fail, the allocated bitmap is not freed, resulting in a memory leak. To avoid this, the validation checks should be performed before the bitmap is allocated.

Signed-off-by: Lidong Yan <502024330056@smail.nju.edu.cn>
---
    pack-bitmap: fix memory leak if load_bitmap_entries_v1 failed
    
    In pack-bitmap.c:load_bitmap_entries_v1, the function read_bitmap_1
    allocates a bitmap and reads index data into it. However, if any of the
    validation checks following the allocation fail, the allocated bitmap is
    not freed, resulting in a memory leak. To avoid this, the validation
    checks should be performed before the bitmap is allocated.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1962%2Fbrandb97%2Ffix-pack-bitmap-leak-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1962/brandb97/fix-pack-bitmap-leak-v1
Pull-Request: https://github.com/git/git/pull/1962
 pack-bitmap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/pack-bitmap.c b/pack-bitmap.c
index b9f1d866046..ac6d62b980c 100644
--- a/pack-bitmap.c
+++ b/pack-bitmap.c
@@ -388,10 +388,6 @@ static int load_bitmap_entries_v1(struct bitmap_index *index)
 			return error(_("corrupt ewah bitmap: commit index %u out of range"),
 				     (unsigned)commit_idx_pos);
 
-		bitmap = read_bitmap_1(index);
-		if (!bitmap)
-			return -1;
-
 		if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
 			return error(_("corrupted bitmap pack index"));
 
@@ -402,6 +398,10 @@ static int load_bitmap_entries_v1(struct bitmap_index *index)
 				return error(_("invalid XOR offset in bitmap pack index"));
 		}
 
+		bitmap = read_bitmap_1(index);
+		if (!bitmap)
+			return -1;
+
 		recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
 			index, bitmap, &oid, xor_bitmap, flags);
 	}

base-commit: 6f84262c44a89851c3ae5a6e4c1a9d06b2068d75
-- 
gitgitgadget
Next: Jeff King
Message 1 of 45 in “pack-bitmap: fix memory leak if `load_bitmap_entries_v1` failed”
  1. pack-bitmap: fix memory leak if `load_bitmap_entries_v1` failedLidong Yan via GitGitGadget, May 12, 2025
  2. Jeff KingMay 12, 2025
  3. Taylor BlauMay 13, 2025
  4. Junio C HamanoMay 14, 2025
  5. Jeff KingMay 14, 2025
  6. lidongyanMay 15, 2025
  7. 0/3 pack-bitmap: fix memory leak if load_bitmap_entries_v1 failedLidong Yan via GitGitGadget, May 20, 2025
  8. 1/3 pack-bitmap: fix memory leak if `load_bitmap_entries_v1` failedLidong Yan via GitGitGadget, May 20, 2025
  9. 2/3 pack-bitmap: fix memory leak if `load_bitmap_entries_v1` failedTaylor Blau via GitGitGadget, May 20, 2025
  10. Taylor BlauMay 21, 2025
  11. lidongyanMay 22, 2025
  12. Junio C HamanoMay 22, 2025
  13. 3/3 pack-bitmap: add loading corrupt bitmap_index testLidong Yan via GitGitGadget, May 20, 2025
  14. Taylor BlauMay 22, 2025
  15. lidongyanMay 22, 2025
  16. Taylor BlauMay 23, 2025
  17. lidongyanMay 23, 2025
  18. 0/2 pack-bitmap: fix memory leak if load_bitmap_entries_v1 failedLidong Yan via GitGitGadget, May 25, 2025
  19. 1/2 pack-bitmap: fix memory leak if `load_bitmap_entries_v1` failedTaylor Blau via GitGitGadget, May 25, 2025
  20. 2/2 pack-bitmap: add load corrupt bitmap testLidong Yan via GitGitGadget, May 25, 2025
  21. 0/2 pack-bitmap: fix memory leak if load_bitmap failedLidong Yan via GitGitGadget, May 25, 2025
  22. 1/2 pack-bitmap: fix memory leak if load_bitmap() failedTaylor Blau via GitGitGadget, May 25, 2025
  23. Junio C HamanoMay 29, 2025
  24. Taylor BlauMay 29, 2025
  25. Junio C HamanoMay 29, 2025
  26. lidongyanMay 30, 2025
  27. 2/2 pack-bitmap: add load corrupt bitmap testLidong Yan via GitGitGadget, May 25, 2025
  28. Junio C HamanoMay 29, 2025
  29. Taylor BlauMay 29, 2025
  30. lidongyanMay 30, 2025
  31. Taylor BlauMay 29, 2025
  32. lidongyanMay 30, 2025
  33. 0/3 pack-bitmap: fix memory leak if load_bitmap failedLidong Yan via GitGitGadget, Jun 3, 2025
  34. 1/3 pack-bitmap: fix memory leak if load_bitmap() failedTaylor Blau via GitGitGadget, Jun 3, 2025
  35. 2/3 pack-bitmap: reword comments in test_bitmap_commits()Lidong Yan via GitGitGadget, Jun 3, 2025
  36. Taylor BlauJun 3, 2025
  37. 3/3 pack-bitmap: add load corrupt bitmap testLidong Yan via GitGitGadget, Jun 3, 2025
  38. Taylor BlauJun 3, 2025
  39. 0/3 pack-bitmap: fix memory leak if load_bitmap failedLidong Yan via GitGitGadget, Jul 1, 2025
  40. 1/3 pack-bitmap: fix memory leak if load_bitmap() failedTaylor Blau via GitGitGadget, Jul 1, 2025
  41. 2/3 pack-bitmap: reword comments in test_bitmap_commits()Lidong Yan via GitGitGadget, Jul 1, 2025
  42. 3/3 pack-bitmap: add load corrupt bitmap testLidong Yan via GitGitGadget, Jul 1, 2025
  43. Junio C HamanoJul 7, 2025
  44. Taylor BlauJul 8, 2025
  45. Junio C HamanoJul 8, 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.