Skip to content

Commit 1fb23e6

Browse files
dotdashgitster
authored andcommitted
force_object_loose: Fix memory leak
read_packed_sha1 expectes its caller to free the buffer it returns, which force_object_loose didn't do. This leak is eventually triggered by "git gc", when it is manually invoked or there are too many packs around, making gc totally unusable when there are lots of unreachable objects. Signed-off-by: Björn Steinbrink <[email protected]> Acked-by: Nicolas Pitre <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7213080 commit 1fb23e6

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

sha1_file.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2322,14 +2322,18 @@ int force_object_loose(const unsigned char *sha1, time_t mtime)
23222322
enum object_type type;
23232323
char hdr[32];
23242324
int hdrlen;
2325+
int ret;
23252326

23262327
if (has_loose_object(sha1))
23272328
return 0;
23282329
buf = read_packed_sha1(sha1, &type, &len);
23292330
if (!buf)
23302331
return error("cannot read sha1_file for %s", sha1_to_hex(sha1));
23312332
hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
2332-
return write_loose_object(sha1, hdr, hdrlen, buf, len, mtime);
2333+
ret = write_loose_object(sha1, hdr, hdrlen, buf, len, mtime);
2334+
free(buf);
2335+
2336+
return ret;
23332337
}
23342338

23352339
int has_pack_index(const unsigned char *sha1)

0 commit comments

Comments
 (0)