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

[PATCH 3/5] fast-import: clean up pack_data pointer in end_packfile

From
Jeff King <peff@peff.net>
Date
Aug 23, 2014, 05:27 UTC
Message-ID
<20140823052741.GC18075@peff.net>
In-Reply-To
<20140823052334.GA17813@peff.net>

We have a global pointer pack_data pointing to the current pack we have open. Inside end_packfile we have two new pointers, old_p and new_p. The latter points to pack_data, and the former points to the new "installed" version of the packfile we get when we hand the file off to the regular sha1_file machinery. When then free old_p.

Presumably the extra old_p pointer was there so that we could overwrite pack_data with new_p and still free old_p, but we don't do that. We just leave pack_data pointing to bogus memory, and don't overwrite it until we call start_packfile again (if ever).

This can cause problems for our die routine, which calls end_packfile to clean things up. If we die at the wrong moment, we can end up looking at invalid memory in pack_data left after the last end_packfile().

Instead, let's make sure we set pack_data to NULL after we free it, and make calling endfile() again with a NULL pack_data a noop (there is nothing to end).

We can further make things less confusing by dropping old_p entirely, and moving new_p closer to its point of use.

Signed-off-by: Jeff King <peff@peff.net>
---
Noticed while running fast-import under valgrind to debug the next
commit. :)
 fast-import.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/fast-import.c b/fast-import.c
index d73f58c..f25a4ae 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -946,10 +946,12 @@ static void unkeep_all_packs(void)
 
 static void end_packfile(void)
 {
-	struct packed_git *old_p = pack_data, *new_p;
+	if (!pack_data)
+		return;
 
 	clear_delta_base_cache();
 	if (object_count) {
+		struct packed_git *new_p;
 		unsigned char cur_pack_sha1[20];
 		char *idx_name;
 		int i;
@@ -991,10 +993,11 @@ static void end_packfile(void)
 		pack_id++;
 	}
 	else {
-		close(old_p->pack_fd);
-		unlink_or_warn(old_p->pack_name);
+		close(pack_data->pack_fd);
+		unlink_or_warn(pack_data->pack_name);
 	}
-	free(old_p);
+	free(pack_data);
+	pack_data = NULL;
 
 	/* We can't carry a delta across packfiles. */
 	strbuf_release(&last_blob.data);
-- 
2.1.0.346.ga0367b9
Previous: Jeff KingNext: Ronnie Sahlberg
Message 4 of 14 in “fix pack-refs pruning of refs/foo”
  1. 0/5 fix pack-refs pruning of refs/fooJeff King, Aug 23, 2014
  2. 1/5 git-prompt: do not look for refs/stash in $GIT_DIRJeff King, Aug 23, 2014
  3. 2/5 pack-refs: prune top-level refs like "refs/foo"Jeff King, Aug 23, 2014
  4. 3/5 fast-import: clean up pack_data pointer in end_packfileJeff King, Aug 23, 2014
  5. Ronnie SahlbergAug 25, 2014
  6. Jeff KingAug 25, 2014
  7. 4/5 fast-import: fix buffer overflow in dump_tagsJeff King, Aug 23, 2014
  8. Ronnie SahlbergAug 25, 2014
  9. 5/5 fast-import: stop using lock_ref_sha1Jeff King, Aug 23, 2014
  10. Ronnie SahlbergAug 25, 2014
  11. Michael HaggertyAug 23, 2014
  12. Jeff KingAug 23, 2014
  13. Ronnie SahlbergAug 25, 2014
  14. Jeff KingAug 25, 2014

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.