From: Samuel Thibault Date: Tue, 7 Jan 2025 00:56:41 +0000 (+0100) Subject: tst-xdirent: Fix allocating dirent for readdir_r call X-Git-Tag: glibc-2.41~120 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2d196c2e10a3f75a46910210430435da1afff81f;p=thirdparty%2Fglibc.git 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. --- diff --git a/support/tst-xdirent.c b/support/tst-xdirent.c index e14fab6934..d2aa042bbb 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); }