Possible bug in getdents64()?
A message that is not in the archive (sent off-list or not yet received): [email protected]
[[ CC += linux-man, linux-kernel, libc-alpha, mtk ]]
Quoted 46 lines · Alejandro Colomar
The manual page for
getdents64()says the prototype should be the following:int
getdents64(unsigned int fd, struct linux_dirent64 *dirp, unsigned int count);Note the type of 'count': 'unsigned int'
(usually a 32-bit unsigned integer).
And the Linux kernel seems to use those types (fs/readdir.c:351):SYSCALL_DEFINE3(getdents64, unsigned int, fd,
structlinux_dirent64__user*, dirent,
unsigned int, count)
{
...
}But glibc uses '
size_t' (usually a 64-bit unsigned integer) for the parameter 'count' (sysdeps/unix/linux/getdents64.c:25):/* The kernel struct
linux_dirent64matches the 'struct dirent64' type. */ssize_t__getdents64(int fd, void *buf,size_tnbytes)
{
/* The system call takes an unsigned int argument, and some length checks in the kernel use an int type. */
if (nbytes >INT_MAX)
nbytes =INT_MAX;
returnINLINE_SYSCALL_CALL(getdents64, fd, buf, nbytes);
}libc_hidden_def(__getdents64)weak_alias(__getdents64, getdents64)Isn't it undefined behavior to pass a variable of a different (larger) type to a variadic function than what it expects?
Is that behavior defined in this implementation?
Wouldn't a cast to 'unsigned int' be needed?
Thanks,
Alex
* Alejandro Colomar via Libc-alpha:
Quoted 37 lines
[[ CC += linux-man, linux-kernel, libc-alpha, mtk ]]
Quoted 34 lines · Alejandro Colomar
The manual page for
getdents64()says the prototype should be the following:
intgetdents64(unsigned int fd, struct linux_dirent64 *dirp, unsigned int count);Note the type of 'count': 'unsigned int'
(usually a 32-bit unsigned integer).
And the Linux kernel seems to use those types (fs/readdir.c:351):
SYSCALL_DEFINE3(getdents64, unsigned int, fd,
structlinux_dirent64__user*, dirent, unsigned int, count)
{
...
}
But glibc uses 'size_t' (usually a 64-bit unsigned integer) for the parameter 'count' (sysdeps/unix/linux/getdents64.c:25):/* The kernel struct
linux_dirent64matches the 'struct dirent64' type. */ssize_t__getdents64(int fd, void *buf,size_tnbytes)
{
/* The system call takes an unsigned int argument, and some length checks in the kernel use an int type. */
if (nbytes >INT_MAX)
nbytes =INT_MAX;
returnINLINE_SYSCALL_CALL(getdents64, fd, buf, nbytes);
}libc_hidden_def(__getdents64)weak_alias(__getdents64, getdents64)Isn't it undefined behavior to pass a variable of a different (larger) type to a variadic function than what it expects? Is that behavior defined in this implementation? Wouldn't a cast to 'unsigned int' be needed?
There is no variadic function involved here. INLINE_SYSCALL_CALL takes care of the zero extension to the register internally, irrespective of the argument type. (The register is of type long int or long long int, depending on the architecture.)
Thanks,
Florian
Signature · 3 lines
Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn, Commercial register: Amtsgericht Muenchen, HRB 153243, Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill
Quoted 40 lines · Florian Weimer
* Alejandro Colomar via Libc-alpha:
Quoted 37 lines
[[ CC += linux-man, linux-kernel, libc-alpha, mtk ]]
Quoted 34 lines · Alejandro Colomar
The manual page for
getdents64()says the prototype should be the following:
intgetdents64(unsigned int fd, struct linux_dirent64 *dirp, unsigned int count);Note the type of 'count': 'unsigned int'
(usually a 32-bit unsigned integer).
And the Linux kernel seems to use those types (fs/readdir.c:351):
SYSCALL_DEFINE3(getdents64, unsigned int, fd,
structlinux_dirent64__user*, dirent, unsigned int, count)
{
...
}
But glibc uses 'size_t' (usually a 64-bit unsigned integer) for the parameter 'count' (sysdeps/unix/linux/getdents64.c:25):/* The kernel struct
linux_dirent64matches the 'struct dirent64' type. */ssize_t__getdents64(int fd, void *buf,size_tnbytes)
{
/* The system call takes an unsigned int argument, and some length checks in the kernel use an int type. */
if (nbytes >INT_MAX)
nbytes =INT_MAX;
returnINLINE_SYSCALL_CALL(getdents64, fd, buf, nbytes);
}libc_hidden_def(__getdents64)weak_alias(__getdents64, getdents64)Isn't it undefined behavior to pass a variable of a different (larger) type to a variadic function than what it expects? Is that behavior defined in this implementation? Wouldn't a cast to 'unsigned int' be needed?
Quoted 4 lines
There is no variadic function involved here.
INLINE_SYSCALL_CALLtakes care of the zero extension to the register internally, irrespective of the argument type. (The register is of type long int or long long int, depending on the architecture.)
Hi Florian,
Thank you very much!
Alex
Quoted 4 lines
Thanks,
Florian