]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
tst-xdirent: Fix allocating dirent for readdir_r call
authorSamuel Thibault <samuel.thibault@ens-lyon.org>
Tue, 7 Jan 2025 00:56:41 +0000 (01:56 +0100)
committerSamuel Thibault <samuel.thibault@ens-lyon.org>
Tue, 7 Jan 2025 00:56:41 +0000 (01:56 +0100)
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.

support/tst-xdirent.c

index e14fab6934305c7bbb8d0e5b82125f9f273f5eb5..d2aa042bbb1f6126055dfefadde92a0d032278e1 100644 (file)
@@ -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);
   }