From 6a9e2a4d3ea26b141484909a94359c1a5353b416 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Fri, 15 Apr 2016 13:26:26 +0200 Subject: [PATCH] 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 9693d320..cc3696dd 100644 --- a/src/files.c +++ b/src/files.c @@ -1412,7 +1412,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; @@ -1422,7 +1422,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("..")); @@ -1441,8 +1442,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); @@ -1488,7 +1491,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) { @@ -1500,11 +1504,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()