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.
This commit is contained in:
Rich Felker 2020-11-23 19:44:19 -05:00
parent c17cda6d61
commit e2fa720be7
2 changed files with 17 additions and 3 deletions

View File

@ -4,9 +4,16 @@
ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize) ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize)
{ {
#ifdef SYS_readlink char dummy[1];
return syscall(SYS_readlink, path, buf, bufsize); if (!bufsize) {
#else buf = dummy;
return syscall(SYS_readlinkat, AT_FDCWD, path, buf, bufsize); bufsize = 1;
#endif }
#ifdef SYS_readlink
int r = __syscall(SYS_readlink, path, buf, bufsize);
#else
int r = __syscall(SYS_readlinkat, AT_FDCWD, path, buf, bufsize);
#endif
if (buf == dummy && r > 0) r = 0;
return __syscall_ret(r);
} }

View File

@ -3,5 +3,12 @@
ssize_t readlinkat(int fd, const char *restrict path, char *restrict buf, size_t bufsize) 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);
} }