{"thread":{"id":"4083","subject":"[PATCH] Fix users of prefix_path() to free() only when necessary","startedAt":"2006-05-06T22:02:53Z","lastAt":"2006-05-06T22:02:53Z","messageCount":1,"participants":["Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"19622","messageId":"Pine.LNX.4.63.0605070002310.6357@wbgn013.biozentrum.uni-wuerzburg.de","threadId":"4083","inReplyTo":null,"subject":"[PATCH] Fix users of prefix_path() to free() only when necessary","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2006-05-06T22:02:53Z","receivedAt":"2006-05-06T22:02:53Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"\nUnfortunately, prefix_path() sometimes returns a newly xmalloc()ed buffer,\nand in other cases it returns a substring!\n\nFor example, when calling\n\n\tgit update-index ./hello.txt\n\nprefix_path() returns \"hello.txt\", but does not allocate a new buffer. The\noriginal code only checked if the result of prefix_path() was different from\nwhat was passed in, and thusly trigger a segmentation fault.\n\nSigned-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>\n---\n checkout-index.c |    4 ++--\n update-index.c   |    8 ++++----\n 2 files changed, 6 insertions(+), 6 deletions(-)\n\ndiff --git a/checkout-index.c b/checkout-index.c\nindex 64bdc3b..9876af6 100644\n--- a/checkout-index.c\n+++ b/checkout-index.c\n@@ -278,7 +278,7 @@ int main(int argc, char **argv)\n \t\t\tdie(\"git-checkout-index: don't mix '--stdin' and explicit filenames\");\n \t\tp = prefix_path(prefix, prefix_length, arg);\n \t\tcheckout_file(p);\n-\t\tif (p != arg)\n+\t\tif (p < arg || p > arg + strlen(arg))\n \t\t\tfree((char*)p);\n \t}\n \n@@ -300,7 +300,7 @@ int main(int argc, char **argv)\n \t\t\t\tpath_name = buf.buf;\n \t\t\tp = prefix_path(prefix, prefix_length, path_name);\n \t\t\tcheckout_file(p);\n-\t\t\tif (p != path_name)\n+\t\t\tif (p < path_name || p > path_name + strlen(path_name))\n \t\t\t\tfree((char *)p);\n \t\t\tif (path_name != buf.buf)\n \t\t\t\tfree(path_name);\ndiff --git a/update-index.c b/update-index.c\nindex 7db67aa..f6b09a4 100644\n--- a/update-index.c\n+++ b/update-index.c\n@@ -393,7 +393,7 @@ static void update_one(const char *path,\n \t\tdie(\"Unable to process file %s\", path);\n \treport(\"add '%s'\", path);\n  free_return:\n-\tif (p != path)\n+\tif (p < path || p > path + strlen(path))\n \t\tfree((char*)p);\n }\n \n@@ -609,7 +609,7 @@ static int do_unresolve(int ac, const ch\n \t\tconst char *arg = av[i];\n \t\tconst char *p = prefix_path(prefix, prefix_length, arg);\n \t\terr |= unresolve_one(p);\n-\t\tif (p != arg)\n+\t\tif (p < arg || p > arg + strlen(arg))\n \t\t\tfree((char*)p);\n \t}\n \treturn err;\n@@ -623,7 +623,7 @@ static int do_reupdate(int ac, const cha\n \t */\n \tint pos;\n \tint has_head = 1;\n-\tchar **pathspec = get_pathspec(prefix, av + 1);\n+\tconst char **pathspec = get_pathspec(prefix, av + 1);\n \n \tif (read_ref(git_path(\"HEAD\"), head_sha1))\n \t\t/* If there is no HEAD, that means it is an initial\n@@ -815,7 +815,7 @@ int main(int argc, const char **argv)\n \t\t\tupdate_one(p, NULL, 0);\n \t\t\tif (set_executable_bit)\n \t\t\t\tchmod_path(set_executable_bit, p);\n-\t\t\tif (p != path_name)\n+\t\t\tif (p < path_name || p > path_name + strlen(path_name))\n \t\t\t\tfree((char*) p);\n \t\t\tif (path_name != buf.buf)\n \t\t\t\tfree(path_name);\n-- \n1.3.2.g9ba6-dirty\n"}]}