about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2020-11-23 19:44:19 -0500
committerRich Felker <dalias@aerifal.cx>2020-11-23 19:44:19 -0500
commite2fa720be7024cce4fc489f3877476d35da48ee2 (patch)
tree624a4b85cc7aacccc9a359eaf0ffc00a614ffddf
parentc17cda6d61bc24d5bb51b0837d951da063a1fba5 (diff)
downloadmusl-e2fa720be7024cce4fc489f3877476d35da48ee2.tar.gz
musl-e2fa720be7024cce4fc489f3877476d35da48ee2.tar.xz
musl-e2fa720be7024cce4fc489f3877476d35da48ee2.zip
work around linux bug in readlink syscall with zero buffer size
linux fails with EINVAL when a zero buffer size is passed to the
syscall. this is non-conforming because POSIX already defines EINVAL
with a significantly different meaning: the target is not a symlink.

since the request is semantically valid, patch it up by using a dummy
buffer of length one, and truncating the return value to zero if it
succeeds.
-rw-r--r--src/unistd/readlink.c11
-rw-r--r--src/unistd/readlinkat.c9
2 files changed, 17 insertions, 3 deletions
diff --git a/src/unistd/readlink.c b/src/unistd/readlink.c
index a152d524..32f4537f 100644
--- a/src/unistd/readlink.c
+++ b/src/unistd/readlink.c
@@ -4,9 +4,16 @@
 
 ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize)
 {
+	char dummy[1];
+	if (!bufsize) {
+		buf = dummy;
+		bufsize = 1;
+	}
 #ifdef SYS_readlink
-	return syscall(SYS_readlink, path, buf, bufsize);
+	int r = __syscall(SYS_readlink, path, buf, bufsize);
 #else
-	return syscall(SYS_readlinkat, AT_FDCWD, path, buf, bufsize);
+	int r = __syscall(SYS_readlinkat, AT_FDCWD, path, buf, bufsize);
 #endif
+	if (buf == dummy && r > 0) r = 0;
+	return __syscall_ret(r);
 }
diff --git a/src/unistd/readlinkat.c b/src/unistd/readlinkat.c
index 9af45cd5..f79d3d14 100644
--- a/src/unistd/readlinkat.c
+++ b/src/unistd/readlinkat.c
@@ -3,5 +3,12 @@
 
 ssize_t readlinkat(int fd, const char *restrict path, char *restrict buf, size_t bufsize)
 {
-	return syscall(SYS_readlinkat, fd, path, buf, bufsize);
+	char dummy[1];
+	if (!bufsize) {
+		buf = dummy;
+		bufsize = 1;
+	}
+	int r = __syscall(SYS_readlinkat, fd, path, buf, bufsize);
+	if (buf == dummy && r > 0) r = 0;
+	return __syscall_ret(r);
 }