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>

Stefan Beller committed May 4, 2015 at 12:11 UTC d7a643b73ff4d54e2ad746e3a65a50bf21ac71ce
2 files changed +10 -13
builtin/checkout-index.c
+4 -6
@@ -241,7 +241,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
241 /* Check out named files first */
242 for (i = 0; i < argc; i++) {
243 const char *arg = argv[i];
244 - const char *p;
244 + char *p;
245
246 if (all)
247 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)
249 die("git checkout-index: don't mix '--stdin' and explicit filenames");
250 p = prefix_path(prefix, prefix_length, arg);
251 checkout_file(p, prefix);
252 - if (p < arg || p > arg + strlen(arg))
253 - free((char *)p);
252 + free(p);
253 }
254
255 if (read_from_stdin) {
@@ -260,7 +259,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
259 die("git checkout-index: don't mix '--all' and '--stdin'");
260
261 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
263 - const char *p;
262 + char *p;
263 if (line_termination && buf.buf[0] == '"') {
264 strbuf_reset(&nbuf);
265 if (unquote_c_style(&nbuf, buf.buf, NULL))
@@ -269,8 +268,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
268 }
269 p = prefix_path(prefix, prefix_length, buf.buf);
270 checkout_file(p, prefix);
272 - if (p < buf.buf || p > buf.buf + buf.len)
273 - free((char *)p);
271 + free(p);
272 }
273 strbuf_release(&nbuf);
274 strbuf_release(&buf);
builtin/update-index.c
+6 -7
@@ -532,10 +532,9 @@ static int do_unresolve(int ac, const char **av,
532
533 for (i = 1; i < ac; i++) {
534 const char *arg = av[i];
535 - const char *p = prefix_path(prefix, prefix_length, arg);
535 + char *p = prefix_path(prefix, prefix_length, arg);
536 err |= unresolve_one(p);
537 - if (p < arg || p > arg + strlen(arg))
538 - free((char *)p);
537 + free(p);
538 }
539 return err;
540 }
@@ -871,14 +870,14 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
870 case PARSE_OPT_DONE:
871 {
872 const char *path = ctx.argv[0];
874 - const char *p;
873 + char *p;
874
875 setup_work_tree();
876 p = prefix_path(prefix, prefix_length, path);
877 update_one(p);
878 if (set_executable_bit)
879 chmod_path(set_executable_bit, p);
881 - free((char *)p);
880 + free(p);
881 ctx.argc--;
882 ctx.argv++;
883 break;
@@ -909,7 +908,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
908
909 setup_work_tree();
910 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
912 - const char *p;
911 + char *p;
912 if (line_termination && buf.buf[0] == '"') {
913 strbuf_reset(&nbuf);
914 if (unquote_c_style(&nbuf, buf.buf, NULL))
@@ -920,7 +919,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
919 update_one(p);
920 if (set_executable_bit)
921 chmod_path(set_executable_bit, p);
923 - free((char *)p);
922 + free(p);
923 }
924 strbuf_release(&nbuf);
925 strbuf_release(&buf);