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
0cc9e70c4c9adb3e79b4a492f2f989c611d85966
2 files changed
+6
-6
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
+4
-4
@@ -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
@@ -596,7 +596,7 @@ static int do_unresolve(int ac, const char **av,
596
const char *arg = av[i];
597
const char *p = prefix_path(prefix, prefix_length, arg);
598
err |= unresolve_one(p);
599
- if (p != arg)
599
+ if (p < arg || p > arg + strlen(arg))
600
free((char*)p);
601
}
602
return err;
@@ -610,7 +610,7 @@ static int do_reupdate(int ac, const char **av,
610
*/
611
int pos;
612
int has_head = 1;
613
- char **pathspec = get_pathspec(prefix, av + 1);
613
+ const char **pathspec = get_pathspec(prefix, av + 1);
614
615
if (read_ref(git_path("HEAD"), head_sha1))
616
/* If there is no HEAD, that means it is an initial
@@ -802,7 +802,7 @@ int main(int argc, const char **argv)
802
update_one(p, NULL, 0);
803
if (set_executable_bit)
804
chmod_path(set_executable_bit, p);
805
- if (p != path_name)
805
+ if (p < path_name || p > path_name + strlen(path_name))
806
free((char*) p);
807
if (path_name != buf.buf)
808
free(path_name);