Bläddra i källkod

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.
Rich Felker 11 år sedan
förälder
incheckning
4fb7df12f5
1 ändrade filer med 7 tillägg och 1 borttagningar
  1. 7 1
      src/unistd/getcwd.c

+ 7 - 1
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;
 }