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>
This commit is contained in:
Erik Faye-Lund 2011-10-04 16:02:00 -04:00 committed by Junio C Hamano
parent 703f05ad58
commit 1c64b48e67
3 changed files with 15 additions and 19 deletions

View File

@ -734,7 +734,7 @@ int safe_create_leading_directories(char *path);
int safe_create_leading_directories_const(const char *path); int safe_create_leading_directories_const(const char *path);
int mkdir_in_gitdir(const char *path); int mkdir_in_gitdir(const char *path);
extern char *expand_user_path(const char *path); extern char *expand_user_path(const char *path);
char *enter_repo(char *path, int strict); const char *enter_repo(const char *path, int strict);
static inline int is_absolute_path(const char *path) static inline int is_absolute_path(const char *path)
{ {
return is_dir_sep(path[0]) || has_dos_drive_prefix(path); return is_dir_sep(path[0]) || has_dos_drive_prefix(path);

View File

@ -108,11 +108,11 @@ static void NORETURN daemon_die(const char *err, va_list params)
exit(1); exit(1);
} }
static char *path_ok(char *directory) static const char *path_ok(char *directory)
{ {
static char rpath[PATH_MAX]; static char rpath[PATH_MAX];
static char interp_path[PATH_MAX]; static char interp_path[PATH_MAX];
char *path; const char *path;
char *dir; char *dir;
dir = directory; dir = directory;

28
path.c
View File

@ -283,7 +283,7 @@ return_null:
* links. User relative paths are also returned as they are given, * links. User relative paths are also returned as they are given,
* except DWIM suffixing. * except DWIM suffixing.
*/ */
char *enter_repo(char *path, int strict) const char *enter_repo(const char *path, int strict)
{ {
static char used_path[PATH_MAX]; static char used_path[PATH_MAX];
static char validated_path[PATH_MAX]; static char validated_path[PATH_MAX];
@ -297,14 +297,16 @@ char *enter_repo(char *path, int strict)
}; };
int len = strlen(path); int len = strlen(path);
int i; int i;
while ((1 < len) && (path[len-1] == '/')) { while ((1 < len) && (path[len-1] == '/'))
path[len-1] = 0;
len--; len--;
}
if (PATH_MAX <= len) if (PATH_MAX <= len)
return NULL; return NULL;
if (path[0] == '~') { strncpy(used_path, path, len); used_path[len] = 0 ;
char *newpath = expand_user_path(path); strcpy(validated_path, used_path);
if (used_path[0] == '~') {
char *newpath = expand_user_path(used_path);
if (!newpath || (PATH_MAX - 10 < strlen(newpath))) { if (!newpath || (PATH_MAX - 10 < strlen(newpath))) {
free(newpath); free(newpath);
return NULL; return NULL;
@ -316,24 +318,18 @@ char *enter_repo(char *path, int strict)
* anyway. * anyway.
*/ */
strcpy(used_path, newpath); free(newpath); strcpy(used_path, newpath); free(newpath);
strcpy(validated_path, path);
path = used_path;
} }
else if (PATH_MAX - 10 < len) else if (PATH_MAX - 10 < len)
return NULL; return NULL;
else { len = strlen(used_path);
path = strcpy(used_path, path);
strcpy(validated_path, path);
}
len = strlen(path);
for (i = 0; suffix[i]; i++) { for (i = 0; suffix[i]; i++) {
strcpy(path + len, suffix[i]); strcpy(used_path + len, suffix[i]);
if (!access(path, F_OK)) { if (!access(used_path, F_OK)) {
strcat(validated_path, suffix[i]); strcat(validated_path, suffix[i]);
break; break;
} }
} }
if (!suffix[i] || chdir(path)) if (!suffix[i] || chdir(used_path))
return NULL; return NULL;
path = validated_path; path = validated_path;
} }