update-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:53 UTC
fb69a760cc9103d1f8341a3c8ea09bd15a8a412d
1 file changed
+12
-7
update-index.c
+12
-7
@@ -364,23 +364,26 @@ static void update_one(const char *path, const char *prefix, int prefix_length)
364
const char *p = prefix_path(prefix, prefix_length, path);
365
if (!verify_path(p)) {
366
fprintf(stderr, "Ignoring path %s\n", path);
367
- return;
367
+ goto free_return;
368
}
369
if (mark_valid_only) {
370
if (mark_valid(p))
371
die("Unable to mark file %s", path);
372
- return;
372
+ goto free_return;
373
}
374
375
if (force_remove) {
376
if (remove_file_from_cache(p))
377
die("git-update-index: unable to remove %s", path);
378
report("remove '%s'", path);
379
- return;
379
+ goto free_return;
380
}
381
if (add_file_to_cache(p))
382
die("Unable to process file %s", path);
383
report("add '%s'", path);
384
+ free_return:
385
+ if (p != path)
386
+ free((char*)p);
387
}
388
389
static void read_index_info(int line_termination)
@@ -735,6 +738,7 @@ int main(int argc, const char **argv)
738
strbuf_init(&buf);
739
while (1) {
740
char *path_name;
741
+ const char *p;
742
read_line(&buf, stdin, line_termination);
743
if (buf.eof)
744
break;
@@ -742,11 +746,12 @@ int main(int argc, const char **argv)
746
path_name = unquote_c_style(buf.buf, NULL);
747
else
748
path_name = buf.buf;
745
- update_one(path_name, prefix, prefix_length);
746
- if (set_executable_bit) {
747
- const char *p = prefix_path(prefix, prefix_length, path_name);
749
+ p = prefix_path(prefix, prefix_length, path_name);
750
+ update_one(p, NULL, 0);
751
+ if (set_executable_bit)
752
chmod_path(set_executable_bit, p);
749
- }
753
+ if (p != path_name)
754
+ free((char*) p);
755
if (path_name != buf.buf)
756
free(path_name);
757
}