{"thread":{"id":"28572","subject":"[PATCH 1/4] enter_repo: do not modify input","startedAt":"2011-10-04T20:02:00Z","lastAt":"2011-10-04T20:24:52Z","messageCount":2,"participants":["Phil Hord","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":4},"messages":[{"id":"176867","messageId":"CABURp0rKRb+fZG5erKz08Dz+4d7mjoL842zbcA7uzZ8N5Qtn1Q@mail.gmail.com","threadId":"28572","inReplyTo":null,"subject":"[PATCH 1/4] enter_repo: do not modify input","fromName":"Phil Hord","fromEmail":"phil.hord@gmail.com","sentAt":"2011-10-04T20:02:00Z","receivedAt":"2011-10-04T20:02:00Z","isPatch":true,"sender":{"key":"phil.hord@gmail.com","avatar":"https://avatars.githubusercontent.com/u/123908?v=4"},"body":"From: Erik Faye-Lund <kusmabite@gmail.com>\n\nentr_repo(..., 0) currently modifies the input to strip away\ntrailing slashes. This means that we some times need to copy the\ninput to keep the original.\n\nChange it to unconditionally copy it into the used_path buffer so\nwe can safely use the input without having to copy it. Also store\na working copy in validated_path up-front before we start\nresolving anything.\n\nSigned-off-by: Erik Faye-Lund <kusmabite@gmail.com>\nSigned-off-by: Phil Hord <hordp@cisco.com>\n---\nThis is Erik Faye-Lund's patch with some minor cleanup by me.  I sent\nthis to the list earlier today for Erik, but I'm including it here\nagain as part of this series.  Not sure what the etiquette is on this,\nbut this way seemed safest.\n\ndiff --git a/cache.h b/cache.h\nindex 9994a3c..7eeb8cf 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -734,7 +734,7 @@ int safe_create_leading_directories(char *path);\n int safe_create_leading_directories_const(const char *path);\n int mkdir_in_gitdir(const char *path);\n extern char *expand_user_path(const char *path);\n-char *enter_repo(char *path, int strict);\n+const char *enter_repo(const char *path, int strict);\n static inline int is_absolute_path(const char *path)\n {\n \treturn is_dir_sep(path[0]) || has_dos_drive_prefix(path);\ndiff --git a/daemon.c b/daemon.c\nindex 4c8346d..9253192 100644\n--- a/daemon.c\n+++ b/daemon.c\n@@ -108,11 +108,11 @@ static void NORETURN daemon_die(const char *err,\nva_list params)\n \texit(1);\n }\n\n-static char *path_ok(char *directory)\n+static const char *path_ok(char *directory)\n {\n \tstatic char rpath[PATH_MAX];\n \tstatic char interp_path[PATH_MAX];\n-\tchar *path;\n+\tconst char *path;\n \tchar *dir;\n\n \tdir = directory;\ndiff --git a/path.c b/path.c\nindex 6f3f5d5..01028f2 100644\n--- a/path.c\n+++ b/path.c\n@@ -283,7 +283,7 @@ return_null:\n  * links.  User relative paths are also returned as they are given,\n  * except DWIM suffixing.\n  */\n-char *enter_repo(char *path, int strict)\n+const char *enter_repo(const char *path, int strict)\n {\n \tstatic char used_path[PATH_MAX];\n \tstatic char validated_path[PATH_MAX];\n@@ -297,14 +297,16 @@ char *enter_repo(char *path, int strict)\n \t\t};\n \t\tint len = strlen(path);\n \t\tint i;\n-\t\twhile ((1 < len) && (path[len-1] == '/')) {\n-\t\t\tpath[len-1] = 0;\n+\t\twhile ((1 < len) && (path[len-1] == '/'))\n \t\t\tlen--;\n-\t\t}\n+\n \t\tif (PATH_MAX <= len)\n \t\t\treturn NULL;\n-\t\tif (path[0] == '~') {\n-\t\t\tchar *newpath = expand_user_path(path);\n+\t\tstrncpy(used_path, path, len); used_path[len] = 0 ;\n+\t\tstrcpy(validated_path, used_path);\n+\n+\t\tif (used_path[0] == '~') {\n+\t\t\tchar *newpath = expand_user_path(used_path);\n \t\t\tif (!newpath || (PATH_MAX - 10 < strlen(newpath))) {\n \t\t\t\tfree(newpath);\n \t\t\t\treturn NULL;\n@@ -316,24 +318,18 @@ char *enter_repo(char *path, int strict)\n \t\t\t * anyway.\n \t\t\t */\n \t\t\tstrcpy(used_path, newpath); free(newpath);\n-\t\t\tstrcpy(validated_path, path);\n-\t\t\tpath = used_path;\n \t\t}\n \t\telse if (PATH_MAX - 10 < len)\n \t\t\treturn NULL;\n-\t\telse {\n-\t\t\tpath = strcpy(used_path, path);\n-\t\t\tstrcpy(validated_path, path);\n-\t\t}\n-\t\tlen = strlen(path);\n+\t\tlen = strlen(used_path);\n \t\tfor (i = 0; suffix[i]; i++) {\n-\t\t\tstrcpy(path + len, suffix[i]);\n-\t\t\tif (!access(path, F_OK)) {\n+\t\t\tstrcpy(used_path + len, suffix[i]);\n+\t\t\tif (!access(used_path, F_OK)) {\n \t\t\t\tstrcat(validated_path, suffix[i]);\n \t\t\t\tbreak;\n \t\t\t}\n \t\t}\n-\t\tif (!suffix[i] || chdir(path))\n+\t\tif (!suffix[i] || chdir(used_path))\n \t\t\treturn NULL;\n \t\tpath = validated_path;\n \t}\n-- \n1.7.7.503.g26392.dirty\n"},{"id":"176876","messageId":"7vipo4bj3f.fsf@alter.siamese.dyndns.org","threadId":"28572","inReplyTo":"CABURp0rKRb+fZG5erKz08Dz+4d7mjoL842zbcA7uzZ8N5Qtn1Q@mail.gmail.com","subject":"Re: [PATCH 1/4] enter_repo: do not modify input","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2011-10-04T20:24:52Z","receivedAt":"2011-10-04T20:24:52Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phil Hord <phil.hord@gmail.com> writes:\n\n> diff --git a/daemon.c b/daemon.c\n> index 4c8346d..9253192 100644\n> --- a/daemon.c\n> +++ b/daemon.c\n> @@ -108,11 +108,11 @@ static void NORETURN daemon_die(const char *err,\n> va_list params)\n\nCorrupt and unappliable patch.\n\n> -\t\tif (path[0] == '~') {\n> -\t\t\tchar *newpath = expand_user_path(path);\n> +\t\tstrncpy(used_path, path, len); used_path[len] = 0 ;\n\nDo not write two statements on a single line; extra SP before ';'.\n"}]}