{"thread":{"id":"45221","subject":"[PATCH] sha1_file: release fallback base's memory in unpack_entry()","startedAt":"2017-02-25T10:02:49Z","lastAt":"2017-02-25T20:48:48Z","messageCount":2,"participants":["René Scharfe","Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"312626","messageId":"fe07ed70-0b93-8b2c-59f9-967d3dc1313f@web.de","threadId":"45221","inReplyTo":null,"subject":"[PATCH] sha1_file: release fallback base's memory in unpack_entry()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2017-02-25T10:02:28Z","receivedAt":"2017-02-25T10:02:49Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"If a pack entry that's used as a delta base is corrupt, unpack_entry()\nmarks it as unusable and then searches the object again in the hope that\nit can be found in another pack or in a loose file.  The memory for this\nexternal base object is never released.  Free it after use.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n sha1_file.c | 4 ++++\n 1 file changed, 4 insertions(+)\n\ndiff --git a/sha1_file.c b/sha1_file.c\nindex ec957db5e1..8ce80d4481 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -2532,6 +2532,7 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,\n \twhile (delta_stack_nr) {\n \t\tvoid *delta_data;\n \t\tvoid *base = data;\n+\t\tvoid *external_base = NULL;\n \t\tunsigned long delta_size, base_size = size;\n \t\tint i;\n \n@@ -2558,6 +2559,7 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,\n \t\t\t\t      p->pack_name);\n \t\t\t\tmark_bad_packed_object(p, base_sha1);\n \t\t\t\tbase = read_object(base_sha1, &type, &base_size);\n+\t\t\t\texternal_base = base;\n \t\t\t}\n \t\t}\n \n@@ -2576,6 +2578,7 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,\n \t\t\t      \"at offset %\"PRIuMAX\" from %s\",\n \t\t\t      (uintmax_t)curpos, p->pack_name);\n \t\t\tdata = NULL;\n+\t\t\tfree(external_base);\n \t\t\tcontinue;\n \t\t}\n \n@@ -2595,6 +2598,7 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,\n \t\t\terror(\"failed to apply delta\");\n \n \t\tfree(delta_data);\n+\t\tfree(external_base);\n \t}\n \n \t*final_type = type;\n-- \n2.12.0\n\n"},{"id":"312662","messageId":"20170225204841.hvrhbgrv42437b4r@sigill.intra.peff.net","threadId":"45221","inReplyTo":"fe07ed70-0b93-8b2c-59f9-967d3dc1313f@web.de","subject":"Re: [PATCH] sha1_file: release fallback base's memory in unpack_entry()","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-02-25T20:48:41Z","receivedAt":"2017-02-25T20:48:48Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sat, Feb 25, 2017 at 11:02:28AM +0100, René Scharfe wrote:\n\n> If a pack entry that's used as a delta base is corrupt, unpack_entry()\n> marks it as unusable and then searches the object again in the hope that\n> it can be found in another pack or in a loose file.  The memory for this\n> external base object is never released.  Free it after use.\n\nThis looks good. I wondered if there were any tricks with passing the\nresulting base to the delta-base cache. But I don't think so. We add to\nthe cache right _before_ the fallback check we're looking at here. And\nthe \"base\" variable does not persist to the next loop.\n\nArguably, one could do the fallback first and then add the result to the\ndelta base cache, but it probably isn't worth the trouble. However we\nread the object via read_object() would hopefully have done so already.\n\n-Peff\n"}]}