Skip to content

Commit d7a643b

Browse files
stefanbellergitster
authored andcommitted
prefix_path(): unconditionally free results in the callers
As of d089eba (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 <[email protected]> Reviewed-by: Eric Sunshine <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3d4a3ff commit d7a643b

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

builtin/checkout-index.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,15 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
241241
/* Check out named files first */
242242
for (i = 0; i < argc; i++) {
243243
const char *arg = argv[i];
244-
const char *p;
244+
char *p;
245245

246246
if (all)
247247
die("git checkout-index: don't mix '--all' and explicit filenames");
248248
if (read_from_stdin)
249249
die("git checkout-index: don't mix '--stdin' and explicit filenames");
250250
p = prefix_path(prefix, prefix_length, arg);
251251
checkout_file(p, prefix);
252-
if (p < arg || p > arg + strlen(arg))
253-
free((char *)p);
252+
free(p);
254253
}
255254

256255
if (read_from_stdin) {
@@ -260,7 +259,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
260259
die("git checkout-index: don't mix '--all' and '--stdin'");
261260

262261
while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
263-
const char *p;
262+
char *p;
264263
if (line_termination && buf.buf[0] == '"') {
265264
strbuf_reset(&nbuf);
266265
if (unquote_c_style(&nbuf, buf.buf, NULL))
@@ -269,8 +268,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
269268
}
270269
p = prefix_path(prefix, prefix_length, buf.buf);
271270
checkout_file(p, prefix);
272-
if (p < buf.buf || p > buf.buf + buf.len)
273-
free((char *)p);
271+
free(p);
274272
}
275273
strbuf_release(&nbuf);
276274
strbuf_release(&buf);

builtin/update-index.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -532,10 +532,9 @@ static int do_unresolve(int ac, const char **av,
532532

533533
for (i = 1; i < ac; i++) {
534534
const char *arg = av[i];
535-
const char *p = prefix_path(prefix, prefix_length, arg);
535+
char *p = prefix_path(prefix, prefix_length, arg);
536536
err |= unresolve_one(p);
537-
if (p < arg || p > arg + strlen(arg))
538-
free((char *)p);
537+
free(p);
539538
}
540539
return err;
541540
}
@@ -871,14 +870,14 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
871870
case PARSE_OPT_DONE:
872871
{
873872
const char *path = ctx.argv[0];
874-
const char *p;
873+
char *p;
875874

876875
setup_work_tree();
877876
p = prefix_path(prefix, prefix_length, path);
878877
update_one(p);
879878
if (set_executable_bit)
880879
chmod_path(set_executable_bit, p);
881-
free((char *)p);
880+
free(p);
882881
ctx.argc--;
883882
ctx.argv++;
884883
break;
@@ -909,7 +908,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
909908

910909
setup_work_tree();
911910
while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
912-
const char *p;
911+
char *p;
913912
if (line_termination && buf.buf[0] == '"') {
914913
strbuf_reset(&nbuf);
915914
if (unquote_c_style(&nbuf, buf.buf, NULL))
@@ -920,7 +919,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
920919
update_one(p);
921920
if (set_executable_bit)
922921
chmod_path(set_executable_bit, p);
923-
free((char *)p);
922+
free(p);
924923
}
925924
strbuf_release(&nbuf);
926925
strbuf_release(&buf);

0 commit comments

Comments
 (0)