Skip to content

Commit

Permalink
read-cache: fix memleak
Browse files Browse the repository at this point in the history
`ce` is allocated in make_cache_entry and should be freed if it is not
used any more. refresh_cache_entry as a wrapper around refresh_cache_ent
will either return

 - the `ce` given as the parameter, when it was up-to-date;
 - a new updated cache entry which is allocated to new memory; or
 - a NULL when refreshing failed.

In the latter two cases, the original cache-entry `ce` is not used
and needs to be freed.  The rule can be expressed as "if the return
value from refresh is different from the original ce, ce is no
longer used."

Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Stefan Beller authored and Junio C Hamano committed Mar 23, 2015
1 parent 067178e commit 915e44c
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions read-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -748,12 +748,9 @@ struct cache_entry *make_cache_entry(unsigned int mode,
ce->ce_mode = create_ce_mode(mode);

ret = refresh_cache_entry(ce, refresh_options);
if (!ret) {
if (ret != ce)
free(ce);
return NULL;
} else {
return ret;
}
return ret;
}

int ce_same_name(const struct cache_entry *a, const struct cache_entry *b)
Expand Down

0 comments on commit 915e44c

Please sign in to comment.