Commit e296cede authored by Guido van Rossum's avatar Guido van Rossum

Be more rigorous about making pathnames absolute, to address SF bug

#424002.

Refactor init_path_from_argv0() and rename to copy_absolute(); add
absolutize() which does the same in-place.

Clean up whitespace (leading tabs -> spaces, delete trailing
spaces/tabs).
parent 746fe0fa
...@@ -213,28 +213,33 @@ joinpath(char *buffer, char *stuff) ...@@ -213,28 +213,33 @@ joinpath(char *buffer, char *stuff)
buffer[n+k] = '\0'; buffer[n+k] = '\0';
} }
/* init_path_from_argv0 requires that path be allocated at least /* copy_absolute requires that path be allocated at least
MAXPATHLEN + 1 bytes and that argv0_path be no more than MAXPATHLEN MAXPATHLEN + 1 bytes and that p be no more than MAXPATHLEN bytes. */
bytes.
*/
static void static void
init_path_from_argv0(char *path, char *argv0_path) copy_absolute(char *path, char *p)
{ {
if (argv0_path[0] == '/') if (p[0] == SEP)
strcpy(path, argv0_path); strcpy(path, p);
else if (argv0_path[0] == '.') {
getcwd(path, MAXPATHLEN);
if (argv0_path[1] == '/')
joinpath(path, argv0_path + 2);
else
joinpath(path, argv0_path);
}
else { else {
getcwd(path, MAXPATHLEN); getcwd(path, MAXPATHLEN);
joinpath(path, argv0_path); if (p[0] == '.' && p[1] == SEP)
p += 2;
joinpath(path, p);
} }
} }
/* absolutize() requires that path be allocated at least MAXPATHLEN+1 bytes. */
static void
absolutize(char *path)
{
char buffer[MAXPATHLEN + 1];
if (path[0] == SEP)
return;
copy_absolute(buffer, path);
strcpy(path, buffer);
}
/* search_for_prefix requires that argv0_path be no more than MAXPATHLEN /* search_for_prefix requires that argv0_path be no more than MAXPATHLEN
bytes long. bytes long.
*/ */
...@@ -271,7 +276,7 @@ search_for_prefix(char *argv0_path, char *home) ...@@ -271,7 +276,7 @@ search_for_prefix(char *argv0_path, char *home)
} }
/* Search from argv0_path, until root is found */ /* Search from argv0_path, until root is found */
init_path_from_argv0(prefix, argv0_path); copy_absolute(prefix, argv0_path);
do { do {
n = strlen(prefix); n = strlen(prefix);
joinpath(prefix, lib_python); joinpath(prefix, lib_python);
...@@ -324,7 +329,7 @@ search_for_exec_prefix(char *argv0_path, char *home) ...@@ -324,7 +329,7 @@ search_for_exec_prefix(char *argv0_path, char *home)
} }
/* Search from argv0_path, until root is found */ /* Search from argv0_path, until root is found */
init_path_from_argv0(exec_prefix, argv0_path); copy_absolute(exec_prefix, argv0_path);
do { do {
n = strlen(exec_prefix); n = strlen(exec_prefix);
joinpath(exec_prefix, lib_python); joinpath(exec_prefix, lib_python);
...@@ -409,20 +414,18 @@ calculate_path(void) ...@@ -409,20 +414,18 @@ calculate_path(void)
if (strchr(prog, SEP)) if (strchr(prog, SEP))
strncpy(progpath, prog, MAXPATHLEN); strncpy(progpath, prog, MAXPATHLEN);
else if (path) { else if (path) {
int bufspace = MAXPATHLEN;
while (1) { while (1) {
char *delim = strchr(path, DELIM); char *delim = strchr(path, DELIM);
if (delim) { if (delim) {
size_t len = delim - path; size_t len = delim - path;
if (len > bufspace) if (len > MAXPATHLEN)
len = bufspace; len = MAXPATHLEN;
strncpy(progpath, path, len); strncpy(progpath, path, len);
*(progpath + len) = '\0'; *(progpath + len) = '\0';
bufspace -= len;
} }
else else
strncpy(progpath, path, bufspace); strncpy(progpath, path, MAXPATHLEN);
joinpath(progpath, prog); joinpath(progpath, prog);
if (isxfile(progpath)) if (isxfile(progpath))
...@@ -437,6 +440,8 @@ calculate_path(void) ...@@ -437,6 +440,8 @@ calculate_path(void)
} }
else else
progpath[0] = '\0'; progpath[0] = '\0';
if (progpath[0] != SEP)
absolutize(progpath);
#ifdef WITH_NEXT_FRAMEWORK #ifdef WITH_NEXT_FRAMEWORK
} }
#endif #endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment