{"thread":{"id":"28578","subject":"Re: [PATCH 1/4] enter_repo: do not modify input","startedAt":"2011-10-04T22:51:22Z","lastAt":"2011-10-04T23:06:27Z","messageCount":3,"participants":["Phil Hord"],"isPatch":true,"patchVersion":1,"patchTotal":4},"messages":[{"id":"176887","messageId":"4E8B8DEA.1000606@cisco.com","threadId":"28578","inReplyTo":null,"subject":"Re: [PATCH 1/4] enter_repo: do not modify input","fromName":"Phil Hord","fromEmail":"hordp@cisco.com","sentAt":"2011-10-04T22:51:22Z","receivedAt":"2011-10-04T22:51:22Z","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\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     return 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     exit(1);\n }\n \n-static char *path_ok(char *directory)\n+static const char *path_ok(char *directory)\n {\n     static char rpath[PATH_MAX];\n     static char interp_path[PATH_MAX];\n-    char *path;\n+    const char *path;\n     char *dir;\n \n     dir = directory;\ndiff --git a/path.c b/path.c\nindex 6f3f5d5..f3d96aa 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     static char used_path[PATH_MAX];\n     static char validated_path[PATH_MAX];\n@@ -297,14 +297,17 @@ char *enter_repo(char *path, int strict)\n         };\n         int len = strlen(path);\n         int i;\n-        while ((1 < len) && (path[len-1] == '/')) {\n-            path[len-1] = 0;\n+        while ((1 < len) && (path[len-1] == '/'))\n             len--;\n-        }\n+\n         if (PATH_MAX <= len)\n             return NULL;\n-        if (path[0] == '~') {\n-            char *newpath = expand_user_path(path);\n+        strncpy(used_path, path, len);\n+        used_path[len] = 0;\n+        strcpy(validated_path, used_path);\n+\n+        if (used_path[0] == '~') {\n+            char *newpath = expand_user_path(used_path);\n             if (!newpath || (PATH_MAX - 10 < strlen(newpath))) {\n                 free(newpath);\n                 return NULL;\n@@ -316,24 +319,18 @@ char *enter_repo(char *path, int strict)\n              * anyway.\n              */\n             strcpy(used_path, newpath); free(newpath);\n-            strcpy(validated_path, path);\n-            path = used_path;\n         }\n         else if (PATH_MAX - 10 < len)\n             return NULL;\n-        else {\n-            path = strcpy(used_path, path);\n-            strcpy(validated_path, path);\n-        }\n-        len = strlen(path);\n+        len = strlen(used_path);\n         for (i = 0; suffix[i]; i++) {\n-            strcpy(path + len, suffix[i]);\n-            if (!access(path, F_OK)) {\n+            strcpy(used_path + len, suffix[i]);\n+            if (!access(used_path, F_OK)) {\n                 strcat(validated_path, suffix[i]);\n                 break;\n             }\n         }\n-        if (!suffix[i] || chdir(path))\n+        if (!suffix[i] || chdir(used_path))\n             return NULL;\n         path = validated_path;\n     }\n-- \n1.7.7.503.g26392.dirty\n\n \n"},{"id":"176888","messageId":"CABURp0qEQdjLsUxQkf3jSAHU80NOZEqm9iEksZKST_JfK590Qw@mail.gmail.com","threadId":"28578","inReplyTo":"4E8B8DEA.1000606@cisco.com","subject":"Re: [PATCH 1/4] enter_repo: do not modify input","fromName":"Phil Hord","fromEmail":"phil.hord@gmail.com","sentAt":"2011-10-04T23:01:22Z","receivedAt":"2011-10-04T23:01:22Z","isPatch":true,"sender":{"key":"phil.hord@gmail.com","avatar":"https://avatars.githubusercontent.com/u/123908?v=4"},"body":"On Tue, Oct 4, 2011 at 6:51 PM, Phil Hord <hordp@cisco.com> wrote:\n> From: Erik Faye-Lund <kusmabite@gmail.com>\n>\n> +++ b/daemon.c\n> @@ -108,11 +108,11 @@ static void NORETURN daemon_die(const char *err,\n> va_list params)\n>     exit(1);\n\nBlast!  Borked again.\n\nI'll re-work on this and try again tomorrow.\n\nP\n"},{"id":"176890","messageId":"4E8B9173.4070000@cisco.com","threadId":"28578","inReplyTo":"CABURp0qEQdjLsUxQkf3jSAHU80NOZEqm9iEksZKST_JfK590Qw@mail.gmail.com","subject":"Re: [PATCH 1/4] enter_repo: do not modify input","fromName":"Phil Hord","fromEmail":"hordp@cisco.com","sentAt":"2011-10-04T23:06:27Z","receivedAt":"2011-10-04T23:06:27Z","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\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, va_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..f3d96aa 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,17 @@ 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);\n+\t\tused_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 +319,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"}]}