From 8ba446cb1ea0e915edf3cecde7ce668f2a6a0352 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Fri, 15 Apr 2016 12:27:48 +0200 Subject: [PATCH 3/4] files: limit the number of attempts at climbing up the directory tree Doing a chdir("..") will not fail when the root directory is reached, and when getcwd() keeps failing too, we have no way of knowing when to stop. So, simply limit the number of attempted chdirs, to avoid getting into an endless loop. This avoids the hang in https://savannah.gnu.org/bugs/index.php?47659. Reported-by: Chris Renshaw Signed-off-by: Benno Schulenberg --- src/files.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/files.c b/src/files.c index cc07fe8..4b6e841 100644 --- a/src/files.c +++ b/src/files.c @@ -1411,6 +1411,8 @@ void do_insertfile_void(void) * able to go there. */ 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; const char *last_slash; @@ -1424,11 +1426,10 @@ char *get_full_path(const char *origpath) * current directory. */ d_here = getcwd(NULL, PATH_MAX + 1); - while (d_here == NULL) { - if (chdir("..") == -1) - break; - + while (d_here == NULL && attempts < 20) { + IGNORE_CALL_RESULT(chdir("..")); d_here = getcwd(NULL, PATH_MAX + 1); + attempts++; } /* If we succeeded, canonicalize it in d_here. */ -- 2.8.1