enter_repo: do not modify input

entr_repo(..., 0) currently modifies the input to strip away trailing slashes. This means that we some times need to copy the input to keep the original. Change it to unconditionally copy it into the used_path buffer so we can safely use the input without having to copy it. Also store a working copy in validated_path up-front before we start resolving anything. Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com> Signed-off-by: Phil Hord <hordp@cisco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Erik Faye-Lund committed Oct 4, 2011 at 16:02 UTC 1c64b48e67c2c83508c45817002468a9a633c991
3 files changed +15 -19
cache.h
+1 -1
@@ -734,7 +734,7 @@ int safe_create_leading_directories(char *path);
734 int safe_create_leading_directories_const(const char *path);
735 int mkdir_in_gitdir(const char *path);
736 extern char *expand_user_path(const char *path);
737 -char *enter_repo(char *path, int strict);
737 +const char *enter_repo(const char *path, int strict);
738 static inline int is_absolute_path(const char *path)
739 {
740 return is_dir_sep(path[0]) || has_dos_drive_prefix(path);
daemon.c
+2 -2
@@ -108,11 +108,11 @@ static void NORETURN daemon_die(const char *err, va_list params)
108 exit(1);
109 }
110
111 -static char *path_ok(char *directory)
111 +static const char *path_ok(char *directory)
112 {
113 static char rpath[PATH_MAX];
114 static char interp_path[PATH_MAX];
115 - char *path;
115 + const char *path;
116 char *dir;
117
118 dir = directory;
path.c
+12 -16
@@ -283,7 +283,7 @@ return_null:
283 * links. User relative paths are also returned as they are given,
284 * except DWIM suffixing.
285 */
286 -char *enter_repo(char *path, int strict)
286 +const char *enter_repo(const char *path, int strict)
287 {
288 static char used_path[PATH_MAX];
289 static char validated_path[PATH_MAX];
@@ -297,14 +297,16 @@ char *enter_repo(char *path, int strict)
297 };
298 int len = strlen(path);
299 int i;
300 - while ((1 < len) && (path[len-1] == '/')) {
301 - path[len-1] = 0;
300 + while ((1 < len) && (path[len-1] == '/'))
301 len--;
303 - }
302 +
303 if (PATH_MAX <= len)
304 return NULL;
306 - if (path[0] == '~') {
307 - char *newpath = expand_user_path(path);
305 + strncpy(used_path, path, len); used_path[len] = 0 ;
306 + strcpy(validated_path, used_path);
307 +
308 + if (used_path[0] == '~') {
309 + char *newpath = expand_user_path(used_path);
310 if (!newpath || (PATH_MAX - 10 < strlen(newpath))) {
311 free(newpath);
312 return NULL;
@@ -316,24 +318,18 @@ char *enter_repo(char *path, int strict)
318 * anyway.
319 */
320 strcpy(used_path, newpath); free(newpath);
319 - strcpy(validated_path, path);
320 - path = used_path;
321 }
322 else if (PATH_MAX - 10 < len)
323 return NULL;
324 - else {
325 - path = strcpy(used_path, path);
326 - strcpy(validated_path, path);
327 - }
328 - len = strlen(path);
324 + len = strlen(used_path);
325 for (i = 0; suffix[i]; i++) {
330 - strcpy(path + len, suffix[i]);
331 - if (!access(path, F_OK)) {
326 + strcpy(used_path + len, suffix[i]);
327 + if (!access(used_path, F_OK)) {
328 strcat(validated_path, suffix[i]);
329 break;
330 }
331 }
336 - if (!suffix[i] || chdir(path))
332 + if (!suffix[i] || chdir(used_path))
333 return NULL;
334 path = validated_path;
335 }