diff options
author | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2025-01-07 01:56:41 +0100 |
---|---|---|
committer | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2025-01-07 01:56:41 +0100 |
commit | 2d196c2e10a3f75a46910210430435da1afff81f (patch) | |
tree | 35edee2a05c7723c2a852df69b12d9c734b78065 | |
parent | 75fed76dfd8e1e7f45fb9b346d0f70f687d736cd (diff) | |
download | glibc-2d196c2e10a3f75a46910210430435da1afff81f.zip glibc-2d196c2e10a3f75a46910210430435da1afff81f.tar.gz glibc-2d196c2e10a3f75a46910210430435da1afff81f.tar.bz2 |
tst-xdirent: Fix allocating dirent for readdir_r call
As documented in the glibc manual, “Some systems don’t define the d_name
element sufficiently long”, and it provides an example of using a union to
properly allocate the storage under the dirent.
-rw-r--r-- | support/tst-xdirent.c | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/support/tst-xdirent.c b/support/tst-xdirent.c index e14fab6..d2aa042 100644 --- a/support/tst-xdirent.c +++ b/support/tst-xdirent.c @@ -50,10 +50,14 @@ do_test (void) { DIR *d = xopendir ("."); - struct dirent buf = { 0, }; - TEST_VERIFY (xreaddir_r (d, &buf)); - TEST_COMPARE_STRING (buf.d_name, "."); - while (xreaddir_r (d, &buf)) + union + { + struct dirent d; + char b[offsetof (struct dirent, d_name) + NAME_MAX + 1]; + } buf; + TEST_VERIFY (xreaddir_r (d, &buf.d)); + TEST_COMPARE_STRING (buf.d.d_name, "."); + while (xreaddir_r (d, &buf.d)) ; xclosedir (d); } |