prefix_path(): unconditionally free results in the callers

As of d089ebaa (setup: sanitize absolute and funny paths in
get_pathspec(), 2008-01-28), prefix_path() always returns a
newly allocated string, so callers should free its result.

Additionally, drop the const from variables to which the result of
the prefix_path() is assigned, so they can be free()'d without
having to cast-away the constness.

Signed-off-by: Stefan Beller <sbeller@google.com>
Reviewed-by: Eric Sunshine <sunshine@sunshineco.com>
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Stefan Beller 2015-05-04 12:11:54 -07:00 committed by Junio C Hamano
parent 3d4a3ffe64
commit d7a643b73f
2 changed files with 10 additions and 13 deletions

View File

@ -241,7 +241,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
/* Check out named files first */ /* Check out named files first */
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
const char *arg = argv[i]; const char *arg = argv[i];
const char *p; char *p;
if (all) if (all)
die("git checkout-index: don't mix '--all' and explicit filenames"); die("git checkout-index: don't mix '--all' and explicit filenames");
@ -249,8 +249,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
die("git checkout-index: don't mix '--stdin' and explicit filenames"); die("git checkout-index: don't mix '--stdin' and explicit filenames");
p = prefix_path(prefix, prefix_length, arg); p = prefix_path(prefix, prefix_length, arg);
checkout_file(p, prefix); checkout_file(p, prefix);
if (p < arg || p > arg + strlen(arg)) free(p);
free((char *)p);
} }
if (read_from_stdin) { if (read_from_stdin) {
@ -260,7 +259,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
die("git checkout-index: don't mix '--all' and '--stdin'"); die("git checkout-index: don't mix '--all' and '--stdin'");
while (strbuf_getline(&buf, stdin, line_termination) != EOF) { while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
const char *p; char *p;
if (line_termination && buf.buf[0] == '"') { if (line_termination && buf.buf[0] == '"') {
strbuf_reset(&nbuf); strbuf_reset(&nbuf);
if (unquote_c_style(&nbuf, buf.buf, NULL)) if (unquote_c_style(&nbuf, buf.buf, NULL))
@ -269,8 +268,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
} }
p = prefix_path(prefix, prefix_length, buf.buf); p = prefix_path(prefix, prefix_length, buf.buf);
checkout_file(p, prefix); checkout_file(p, prefix);
if (p < buf.buf || p > buf.buf + buf.len) free(p);
free((char *)p);
} }
strbuf_release(&nbuf); strbuf_release(&nbuf);
strbuf_release(&buf); strbuf_release(&buf);

View File

@ -532,10 +532,9 @@ static int do_unresolve(int ac, const char **av,
for (i = 1; i < ac; i++) { for (i = 1; i < ac; i++) {
const char *arg = av[i]; const char *arg = av[i];
const char *p = prefix_path(prefix, prefix_length, arg); char *p = prefix_path(prefix, prefix_length, arg);
err |= unresolve_one(p); err |= unresolve_one(p);
if (p < arg || p > arg + strlen(arg)) free(p);
free((char *)p);
} }
return err; return err;
} }
@ -871,14 +870,14 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
case PARSE_OPT_DONE: case PARSE_OPT_DONE:
{ {
const char *path = ctx.argv[0]; const char *path = ctx.argv[0];
const char *p; char *p;
setup_work_tree(); setup_work_tree();
p = prefix_path(prefix, prefix_length, path); p = prefix_path(prefix, prefix_length, path);
update_one(p); update_one(p);
if (set_executable_bit) if (set_executable_bit)
chmod_path(set_executable_bit, p); chmod_path(set_executable_bit, p);
free((char *)p); free(p);
ctx.argc--; ctx.argc--;
ctx.argv++; ctx.argv++;
break; break;
@ -909,7 +908,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
setup_work_tree(); setup_work_tree();
while (strbuf_getline(&buf, stdin, line_termination) != EOF) { while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
const char *p; char *p;
if (line_termination && buf.buf[0] == '"') { if (line_termination && buf.buf[0] == '"') {
strbuf_reset(&nbuf); strbuf_reset(&nbuf);
if (unquote_c_style(&nbuf, buf.buf, NULL)) if (unquote_c_style(&nbuf, buf.buf, NULL))
@ -920,7 +919,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
update_one(p); update_one(p);
if (set_executable_bit) if (set_executable_bit)
chmod_path(set_executable_bit, p); chmod_path(set_executable_bit, p);
free((char *)p); free(p);
} }
strbuf_release(&nbuf); strbuf_release(&nbuf);
strbuf_release(&buf); strbuf_release(&buf);