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

[PATCH V2 5/6] pack-bitmap: fix a memleak

From
Stefan Beller <sbeller@google.com>
Date
Mar 27, 2015, 22:32 UTC
Message-ID
<1427495569-10863-6-git-send-email-sbeller@google.com>
In-Reply-To
<1427495569-10863-1-git-send-email-sbeller@google.com>

`recent_bitmaps` is allocated in the function load_bitmap_entries_v1 and it is not passed into any function, so it's safe to free it before leaving that function.

Signed-off-by: Stefan Beller <sbeller@google.com>
---
Notes:
    I wonder however if we need to free the actual bitmaps
    stored in the recent_bitmaps as well.
 pack-bitmap.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
diff --git a/pack-bitmap.c b/pack-bitmap.c
index 365f9d9..34823e9 100644
--- a/pack-bitmap.c
+++ b/pack-bitmap.c
@@ -213,7 +213,7 @@ static int load_bitmap_entries_v1(struct bitmap_index *index)
 {
 	static const size_t MAX_XOR_OFFSET = 160;
 
-	uint32_t i;
+	uint32_t i, ret = 0;
 	struct stored_bitmap **recent_bitmaps;
 
 	recent_bitmaps = xcalloc(MAX_XOR_OFFSET, sizeof(struct stored_bitmap));
@@ -232,24 +232,31 @@ static int load_bitmap_entries_v1(struct bitmap_index *index)
 		sha1 = nth_packed_object_sha1(index->pack, commit_idx_pos);
 
 		bitmap = read_bitmap_1(index);
-		if (!bitmap)
-			return -1;
+		if (!bitmap) {
+			ret = -1;
+			goto out;
+		}
 
-		if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
-			return error("Corrupted bitmap pack index");
+		if (xor_offset > MAX_XOR_OFFSET || xor_offset > i) {
+			ret = error("Corrupted bitmap pack index");
+			goto out;
+		}
 
 		if (xor_offset > 0) {
 			xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
 
-			if (xor_bitmap == NULL)
-				return error("Invalid XOR offset in bitmap pack index");
+			if (xor_bitmap == NULL) {
+				ret = error("Invalid XOR offset in bitmap pack index");
+				goto out;
+			}
 		}
 
 		recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
 			index, bitmap, sha1, xor_bitmap, flags);
 	}
-
-	return 0;
+out:
+	free(recent_bitmaps);
+	return ret;
 }
 
 static char *pack_bitmap_filename(struct packed_git *p)
@@ -986,6 +993,8 @@ void test_bitmap_walk(struct rev_info *revs)
 		fprintf(stderr, "OK!\n");
 	else
 		fprintf(stderr, "Mismatch!\n");
+
+	free(result);
 }
 
 static int rebuild_bitmap(uint32_t *reposition,
-- 
2.3.0.81.gc37f363
Previous: Eric SunshineNext: Eric Sunshine
Message 8 of 14 in “Memory leaks once again”
  1. 0/6 Memory leaks once againStefan Beller, Mar 27, 2015
  2. 1/6 shallow: fix a memleakStefan Beller, Mar 27, 2015
  3. Eric SunshineMar 27, 2015
  4. 2/6 line-log.c: fix a memleakStefan Beller, Mar 27, 2015
  5. 3/6 line-log.c: fix a memleakStefan Beller, Mar 27, 2015
  6. 4/6 wt-status.c: fix a memleakStefan Beller, Mar 27, 2015
  7. Eric SunshineMar 27, 2015
  8. 5/6 pack-bitmap: fix a memleakStefan Beller, Mar 27, 2015
  9. Eric SunshineMar 27, 2015
  10. Jeff KingMar 31, 2015
  11. 6/6 WIP/RFC/entry.c: fix a memleakStefan Beller, Mar 27, 2015
  12. Eric SunshineMar 27, 2015
  13. Eric SunshineMar 28, 2015
  14. John KeepingMar 28, 2015

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.