From 4fb7df12f52982412e60afeaf9c4463f78b396fc Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Tue, 8 Oct 2013 19:49:10 -0400 Subject: [PATCH] fix errno value for getcwd when size argument is zero based on patch by Michael Forney. at the same time, I've changed the if branch to be more clear, avoiding the comma operator. the underlying issue is that Linux always returns ERANGE when size is too short, even when it's zero, rather than returning EINVAL for the special case of zero as required by POSIX. --- src/unistd/getcwd.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/unistd/getcwd.c b/src/unistd/getcwd.c index 2e540cd4..a7b925d2 100644 --- a/src/unistd/getcwd.c +++ b/src/unistd/getcwd.c @@ -7,7 +7,13 @@ char *getcwd(char *buf, size_t size) { char tmp[PATH_MAX]; - if (!buf) buf = tmp, size = PATH_MAX; + if (!buf) { + buf = tmp; + size = PATH_MAX; + } else if (!size) { + errno = EINVAL; + return 0; + } if (syscall(SYS_getcwd, buf, size) < 0) return 0; return buf == tmp ? strdup(buf) : buf; } -- 2.25.1