Fix users of prefix_path() to free() only when necessary
Unfortunately, prefix_path() sometimes returns a newly xmalloc()ed buffer, and in other cases it returns a substring! For example, when calling git update-index ./hello.txt prefix_path() returns "hello.txt", but does not allocate a new buffer. The original code only checked if the result of prefix_path() was different from what was passed in, and thusly trigger a segmentation fault. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Johannes Schindelin committed
May 7, 2006 at 00:02 UTC
be65e7d9fbd3ae6fc097cedade2afe39805fcf4e
2 files changed
+5
-5
checkout-index.c
+2
-2
@@ -277,7 +277,7 @@ int main(int argc, char **argv)
277
die("git-checkout-index: don't mix '--stdin' and explicit filenames");
278
p = prefix_path(prefix, prefix_length, arg);
279
checkout_file(p);
280
- if (p != arg)
280
+ if (p < arg || p > arg + strlen(arg))
281
free((char*)p);
282
}
283
@@ -299,7 +299,7 @@ int main(int argc, char **argv)
299
path_name = buf.buf;
300
p = prefix_path(prefix, prefix_length, path_name);
301
checkout_file(p);
302
- if (p != path_name)
302
+ if (p < path_name || p > path_name + strlen(path_name))
303
free((char *)p);
304
if (path_name != buf.buf)
305
free(path_name);
update-index.c
+3
-3
@@ -382,7 +382,7 @@ static void update_one(const char *path, const char *prefix, int prefix_length)
382
die("Unable to process file %s", path);
383
report("add '%s'", path);
384
free_return:
385
- if (p != path)
385
+ if (p < path || p > path + strlen(path))
386
free((char*)p);
387
}
388
@@ -594,7 +594,7 @@ static int do_unresolve(int ac, const char **av,
594
const char *arg = av[i];
595
const char *p = prefix_path(prefix, prefix_length, arg);
596
err |= unresolve_one(p);
597
- if (p != arg)
597
+ if (p < arg || p > arg + strlen(arg))
598
free((char*)p);
599
}
600
return err;
@@ -750,7 +750,7 @@ int main(int argc, const char **argv)
750
update_one(p, NULL, 0);
751
if (set_executable_bit)
752
chmod_path(set_executable_bit, p);
753
- if (p != path_name)
753
+ if (p < path_name || p > path_name + strlen(path_name))
754
free((char*) p);
755
if (path_name != buf.buf)
756
free(path_name);