Skip to content

Commit 2d9426b

Browse files
stefanbellergitster
authored andcommitted
read-cache: free cache entry in add_to_index in case of early return
This frees `ce` would be leaking in the error path. Additionally a free is moved towards the return. This helps code readability as we often have this pattern of freeing resources just before return/exit and not in between the code. Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fd2014d commit 2d9426b

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

read-cache.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,15 +681,18 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
681681
alias = index_file_exists(istate, ce->name, ce_namelen(ce), ignore_case);
682682
if (alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) {
683683
/* Nothing changed, really */
684-
free(ce);
685684
if (!S_ISGITLINK(alias->ce_mode))
686685
ce_mark_uptodate(alias);
687686
alias->ce_flags |= CE_ADDED;
687+
688+
free(ce);
688689
return 0;
689690
}
690691
if (!intent_only) {
691-
if (index_path(ce->sha1, path, st, HASH_WRITE_OBJECT))
692+
if (index_path(ce->sha1, path, st, HASH_WRITE_OBJECT)) {
693+
free(ce);
692694
return error("unable to index file %s", path);
695+
}
693696
} else
694697
set_object_name_for_intent_to_add_entry(ce);
695698

0 commit comments

Comments
 (0)