Skip to content

Commit 16235e3

Browse files
avargitster
authored andcommitted
object-file: free(*contents) only in read_loose_object() caller
In the preceding commit a free() of uninitialized memory regression in 96e41f5 (fsck: report invalid object type-path combinations, 2021-10-01) was fixed, but we'd still have an issue with leaking memory from fsck_loose(). Let's fix that issue too. That issue was introduced in my 31deb28 (fsck: don't hard die on invalid object types, 2021-10-01). It can be reproduced under SANITIZE=leak with the test I added in 093fffd (fsck tests: add test for fsck-ing an unknown type, 2021-10-01): ./t1450-fsck.sh --run=84 -vixd In some sense it's not a problem, we lost the same amount of memory in terms of things malloc'd and not free'd. It just moved from the "still reachable" to "definitely lost" column in valgrind(1) nomenclature[1], since we'd have die()'d before. But now that we don't hard die() anymore in the library let's properly free() it. Doing so makes this code much easier to follow, since we'll now have one function owning the freeing of the "contents" variable, not two. For context on that memory management pattern the read_loose_object() function was added in f6371f9 (sha1_file: add read_loose_object() function, 2017-01-13) and subsequently used in c68b489 (fsck: parse loose object paths directly, 2017-01-13). The pattern of it being the task of both sides to free() the memory has been there in this form since its inception. 1. https://valgrind.org/docs/manual/mc-manual.html#mc-manual.leaks Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 168a937 commit 16235e3

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

builtin/fsck.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ static int fsck_loose(const struct object_id *oid, const char *path, void *data)
604604
struct object *obj;
605605
enum object_type type = OBJ_NONE;
606606
unsigned long size;
607-
void *contents;
607+
void *contents = NULL;
608608
int eaten;
609609
struct object_info oi = OBJECT_INFO_INIT;
610610
struct object_id real_oid = *null_oid();
@@ -629,6 +629,7 @@ static int fsck_loose(const struct object_id *oid, const char *path, void *data)
629629
path);
630630
if (err < 0) {
631631
errors_found |= ERROR_OBJECT;
632+
free(contents);
632633
return 0; /* keep checking other objects */
633634
}
634635

object-file.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,8 +2533,6 @@ int read_loose_object(const char *path,
25332533
char hdr[MAX_HEADER_LEN];
25342534
unsigned long *size = oi->sizep;
25352535

2536-
*contents = NULL;
2537-
25382536
map = map_loose_object_1(the_repository, path, NULL, &mapsize);
25392537
if (!map) {
25402538
error_errno(_("unable to mmap %s"), path);
@@ -2564,10 +2562,9 @@ int read_loose_object(const char *path,
25642562
goto out;
25652563
}
25662564
if (check_object_signature(the_repository, expected_oid,
2567-
*contents, *size, oi->type_name->buf, real_oid)) {
2568-
free(*contents);
2565+
*contents, *size,
2566+
oi->type_name->buf, real_oid))
25692567
goto out;
2570-
}
25712568
}
25722569

25732570
ret = 0; /* everything checks out */

0 commit comments

Comments
 (0)