Merge branch 'fix-unaligned-dirent64' into 'main'

Buffer needs to be aligned correctly to receive linux_dirent64.

See merge request GNOME/glib!3582
This commit is contained in:
Philip Withnall 2023-10-03 17:43:13 +00:00
commit fdfaa993e6

View File

@ -1445,16 +1445,23 @@ safe_fdwalk (int (*cb)(void *data, int fd), void *data)
/* Avoid use of opendir/closedir since these are not async-signal-safe. */ /* Avoid use of opendir/closedir since these are not async-signal-safe. */
int dir_fd = open ("/proc/self/fd", O_RDONLY | O_DIRECTORY); int dir_fd = open ("/proc/self/fd", O_RDONLY | O_DIRECTORY);
if (dir_fd >= 0) if (dir_fd >= 0)
{
/* buf needs to be aligned correctly to receive linux_dirent64.
* C11 has _Alignof for this purpose, but for now a
* union serves the same purpose. */
union
{ {
char buf[4096]; char buf[4096];
struct linux_dirent64 alignment;
} u;
int pos, nread; int pos, nread;
struct linux_dirent64 *de; struct linux_dirent64 *de;
while ((nread = syscall (SYS_getdents64, dir_fd, buf, sizeof(buf))) > 0) while ((nread = syscall (SYS_getdents64, dir_fd, u.buf, sizeof (u.buf))) > 0)
{ {
for (pos = 0; pos < nread; pos += de->d_reclen) for (pos = 0; pos < nread; pos += de->d_reclen)
{ {
de = (struct linux_dirent64 *)(buf + pos); de = (struct linux_dirent64 *) (u.buf + pos);
fd = filename_to_fd (de->d_name); fd = filename_to_fd (de->d_name);
if (fd < 0 || fd == dir_fd) if (fd < 0 || fd == dir_fd)