Skip to content

Commit bc1c2ca

Browse files
stefanbellergitster
authored andcommitted
read-cache.c: free cache entry when refreshing fails
This fixes a memory leak when building the cache entries as refresh_cache_entry may decide to return NULL, but it does not free the cache entry structure which was passed in as an argument. Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 64a03e9 commit bc1c2ca

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

read-cache.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ struct cache_entry *make_cache_entry(unsigned int mode,
701701
unsigned int refresh_options)
702702
{
703703
int size, len;
704-
struct cache_entry *ce;
704+
struct cache_entry *ce, *ret;
705705

706706
if (!verify_path(path)) {
707707
error("Invalid path '%s'", path);
@@ -718,7 +718,13 @@ struct cache_entry *make_cache_entry(unsigned int mode,
718718
ce->ce_namelen = len;
719719
ce->ce_mode = create_ce_mode(mode);
720720

721-
return refresh_cache_entry(ce, refresh_options);
721+
ret = refresh_cache_entry(ce, refresh_options);
722+
if (!ret) {
723+
free(ce);
724+
return NULL;
725+
} else {
726+
return ret;
727+
}
722728
}
723729

724730
int ce_same_name(const struct cache_entry *a, const struct cache_entry *b)

0 commit comments

Comments
 (0)