Skip to content

Commit dcb572a

Browse files
Martin Ågrengitster
authored andcommitted
object_array: use object_array_clear(), not free()
Instead of freeing `foo.objects` for an object array `foo` (sometimes conditionally), call `object_array_clear(&foo)`. This means we don't poke as much into the implementation, which is already a good thing, but also that we release the individual entries as well, thereby fixing at least one memory-leak (in diff-lib.c). If someone is holding on to a pointer to an element's `name` or `path`, that is now a dangling pointer, i.e., we'd be turning an unpleasant situation into an outright bug. To the best of my understanding no such long-term pointers are being taken. The way we handle `study` in builting/reflog.c still looks like it might leak. That will be addressed in the next commit. Signed-off-by: Martin Ågren <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b2ccdf7 commit dcb572a

File tree

4 files changed

+6
-7
lines changed

4 files changed

+6
-7
lines changed

builtin/reflog.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ static int commit_is_complete(struct commit *commit)
182182
found.objects[i].item->flags |= SEEN;
183183
}
184184
/* free object arrays */
185-
free(study.objects);
186-
free(found.objects);
185+
object_array_clear(&study);
186+
object_array_clear(&found);
187187
return !is_incomplete;
188188
}
189189

diff-lib.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,6 @@ int index_differs_from(const char *def, int diff_flags,
549549
rev.diffopt.flags |= diff_flags;
550550
rev.diffopt.ita_invisible_in_index = ita_invisible_in_index;
551551
run_diff_index(&rev, 1);
552-
if (rev.pending.alloc)
553-
free(rev.pending.objects);
552+
object_array_clear(&rev.pending);
554553
return (DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES) != 0);
555554
}

submodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,7 +1728,7 @@ static int find_first_merges(struct object_array *result, const char *path,
17281728
add_object_array(merges.objects[i].item, NULL, result);
17291729
}
17301730

1731-
free(merges.objects);
1731+
object_array_clear(&merges);
17321732
return result->nr;
17331733
}
17341734

@@ -1833,7 +1833,7 @@ int merge_submodule(struct object_id *result, const char *path,
18331833
print_commit((struct commit *) merges.objects[i].item);
18341834
}
18351835

1836-
free(merges.objects);
1836+
object_array_clear(&merges);
18371837
return 0;
18381838
}
18391839

upload-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ static void receive_needs(void)
888888
}
889889

890890
shallow_nr += shallows.nr;
891-
free(shallows.objects);
891+
object_array_clear(&shallows);
892892
}
893893

894894
/* return non-zero if the ref is hidden, otherwise 0 */

0 commit comments

Comments
 (0)