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 <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Stefan Beller committed
Feb 17, 2015 at 10:06 UTC
bc1c2caa7397e7e462f0cc7631a40f6e7cd480ab
1 file changed
+8
-2
read-cache.c
+8
-2
@@ -701,7 +701,7 @@ struct cache_entry *make_cache_entry(unsigned int mode,
701
unsigned int refresh_options)
702
{
703
int size, len;
704
- struct cache_entry *ce;
704
+ struct cache_entry *ce, *ret;
705
706
if (!verify_path(path)) {
707
error("Invalid path '%s'", path);
@@ -718,7 +718,13 @@ struct cache_entry *make_cache_entry(unsigned int mode,
718
ce->ce_namelen = len;
719
ce->ce_mode = create_ce_mode(mode);
720
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
+ }
728
}
729
730
int ce_same_name(const struct cache_entry *a, const struct cache_entry *b)