From 35169ddf478c2d18e4ce4bbd006f08bca5e21095 Mon Sep 17 00:00:00 2001 From: Andreas Kempf Date: Mon, 23 Dec 2019 14:33:48 +0100 Subject: Fix error handling for getcwd Quoting from the Linux man page for errno, "The value in errno is significant only when the return value of the call indicated an error (i.e., -1 from most system calls; -1 or NULL from most library functions); a function that succeeds is allowed to change errno. The value of errno is never set to zero by any system call or library function." Successful calls to getcwd are allowed to set errno causing the compilation database not to be written. Spurious failures of this nature were observed on AIX. Adjust the error handling for getcwd so that errno is only checked if the call returned NULL. --- src/ninja.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ninja.cc b/src/ninja.cc index c24f09d..f39d1a6 100644 --- a/src/ninja.cc +++ b/src/ninja.cc @@ -803,12 +803,14 @@ int NinjaMain::ToolCompilationDatabase(const Options* options, int argc, bool first = true; vector cwd; + char* success = NULL; do { cwd.resize(cwd.size() + 1024); errno = 0; - } while (!getcwd(&cwd[0], cwd.size()) && errno == ERANGE); - if (errno != 0 && errno != ERANGE) { + success = getcwd(&cwd[0], cwd.size()); + } while (!success && errno == ERANGE); + if (!success) { Error("cannot determine working directory: %s", strerror(errno)); return 1; } -- cgit v0.12