read-cache: fix memory leak in do_write_index

The previous_name_buf was never getting released when there was an error in ce_write_entry or allow was false and execution was returned to the caller. Signed-off-by: Kevin Willford <kewillf@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kevin Willford committed Aug 21, 2017 at 15:24 UTC b50386c7c038ccea8f70d2a66ac78f189240ef05
1 file changed +9 -3
read-cache.c
+9 -3
@@ -2192,7 +2192,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
2192 int newfd = tempfile->fd;
2193 git_SHA_CTX c;
2194 struct cache_header hdr;
2195 - int i, err, removed, extended, hdr_version;
2195 + int i, err = 0, removed, extended, hdr_version;
2196 struct cache_entry **cache = istate->cache;
2197 int entries = istate->cache_nr;
2198 struct stat st;
@@ -2247,15 +2247,21 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
2247 if (allow)
2248 warning(msg, ce->name);
2249 else
2250 - return error(msg, ce->name);
2250 + err = error(msg, ce->name);
2251
2252 drop_cache_tree = 1;
2253 }
2254 if (ce_write_entry(&c, newfd, ce, previous_name) < 0)
2255 - return -1;
2255 + err = -1;
2256 +
2257 + if (err)
2258 + break;
2259 }
2260 strbuf_release(&previous_name_buf);
2261
2262 + if (err)
2263 + return err;
2264 +
2265 /* Write extension data here */
2266 if (!strip_extensions && istate->split_index) {
2267 struct strbuf sb = STRBUF_INIT;