From b1c37c439146d8fd31e38e42448d7baa91d4ba13 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Fri, 15 Apr 2016 13:26:26 +0200 Subject: [PATCH 4/4] files: handle systems that disallow NULL as first parameter of getcwd (This change will be made superfluous when we start using gnulib.) This prevents getcwd() from failing on Android and thus completes the fix for https://savannah.gnu.org/bugs/index.php?47659. Reported-by: Chris Renshaw Signed-off-by: Benno Schulenberg --- src/files.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/files.c b/src/files.c index 4b6e841..90a0d49 100644 --- a/src/files.c +++ b/src/files.c @@ -1414,7 +1414,7 @@ char *get_full_path(const char *origpath) int attempts = 0; /* How often we've tried climing back up the tree. */ struct stat fileinfo; - char *d_here, *d_there, *d_there_file = NULL; + char *currentdir, *d_here, *d_there, *d_there_file = NULL; const char *last_slash; bool path_only; @@ -1424,7 +1424,8 @@ char *get_full_path(const char *origpath) /* Get the current directory. If it doesn't exist, back up and try * again until we get a directory that does, and use that as the * current directory. */ - d_here = getcwd(NULL, PATH_MAX + 1); + currentdir = charalloc(PATH_MAX + 1); + d_here = getcwd(currentdir, PATH_MAX + 1); while (d_here == NULL && attempts < 20) { IGNORE_CALL_RESULT(chdir("..")); @@ -1443,8 +1444,10 @@ char *get_full_path(const char *origpath) strcat(d_here, "/"); } /* Otherwise, set d_here to "". */ - } else + } else { d_here = mallocstrcpy(NULL, ""); + free(currentdir); + } d_there = real_dir_from_tilde(origpath); @@ -1490,7 +1493,8 @@ char *get_full_path(const char *origpath) free(d_there); /* Get the full path. */ - d_there = getcwd(NULL, PATH_MAX + 1); + currentdir = charalloc(PATH_MAX + 1); + d_there = getcwd(currentdir, PATH_MAX + 1); /* If we succeeded, canonicalize it in d_there. */ if (d_there != NULL) { @@ -1502,11 +1506,11 @@ char *get_full_path(const char *origpath) d_there = charealloc(d_there, strlen(d_there) + 2); strcat(d_there, "/"); } - } else - /* Otherwise, set path_only to TRUE, so that we clean up - * correctly, free all allocated memory, and return - * NULL. */ + /* Otherwise, make sure that we return NULL. */ + } else { path_only = TRUE; + free(currentdir); + } /* Finally, go back to the path specified in d_here, * where we were before. We don't check for a chdir() -- 2.8.1