@matheusbsbs / matheus-bagliano / commits / dc46da2286
Fork 0

checkout-index: plug memory leak from prefix_path()

prefix_path() sometimes allocates new memory and returns it, and other times returns the incoming path argument intact. The callers need to be a bit careful not to leak memory. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed May 5, 2006 at 22:38 UTC dc46da2286cf0d42e1e5ebbd272c201dbc3a626a
1 file changed +11 -2
checkout-index.c
+11 -2
@@ -269,12 +269,16 @@ int main(int argc, char **argv)
269 /* Check out named files first */
270 for ( ; i < argc; i++) {
271 const char *arg = argv[i];
272 + const char *p;
273
274 if (all)
275 die("git-checkout-index: don't mix '--all' and explicit filenames");
276 if (read_from_stdin)
277 die("git-checkout-index: don't mix '--stdin' and explicit filenames");
277 - checkout_file(prefix_path(prefix, prefix_length, arg));
278 + p = prefix_path(prefix, prefix_length, arg);
279 + checkout_file(p);
280 + if (p != arg)
281 + free((char*)p);
282 }
283
284 if (read_from_stdin) {
@@ -284,6 +288,8 @@ int main(int argc, char **argv)
288 strbuf_init(&buf);
289 while (1) {
290 char *path_name;
291 + const char *p;
292 +
293 read_line(&buf, stdin, line_termination);
294 if (buf.eof)
295 break;
@@ -291,7 +297,10 @@ int main(int argc, char **argv)
297 path_name = unquote_c_style(buf.buf, NULL);
298 else
299 path_name = buf.buf;
294 - checkout_file(prefix_path(prefix, prefix_length, path_name));
300 + p = prefix_path(prefix, prefix_length, path_name);
301 + checkout_file(p);
302 + if (p != path_name)
303 + free((char *)p);
304 if (path_name != buf.buf)
305 free(path_name);
306 }