changeset 37817:b672c86363fa

fts: port to C11 alignof * doc/posix-headers/stdalign.texi (stdalign.h): Document the C11 restriction. * lib/fts.c: Include stddef.h, for max_align_t. (fts_alloc): Align using max_align_t, not FTSENT. * modules/fts (Depends-on): Add stddef.
author Paul Eggert <eggert@cs.ucla.edu>
date Sun, 18 Oct 2015 09:52:45 -0700
parents 4821de9a049f
children 8f3c70110939
files ChangeLog doc/posix-headers/stdalign.texi lib/fts.c modules/fts
diffstat 4 files changed, 23 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sun Oct 18 09:32:21 2015 -0700
+++ b/ChangeLog	Sun Oct 18 09:52:45 2015 -0700
@@ -1,3 +1,12 @@
+2015-10-18  Paul Eggert  <eggert@cs.ucla.edu>
+
+	fts: port to C11 alignof
+	* doc/posix-headers/stdalign.texi (stdalign.h):
+	Document the C11 restriction.
+	* lib/fts.c: Include stddef.h, for max_align_t.
+	(fts_alloc): Align using max_align_t, not FTSENT.
+	* modules/fts (Depends-on): Add stddef.
+
 2015-10-18  Jim Meyering  <meyering@fb.com>
 
 	time_rz: avoid warning from bleeding-edge gcc's -Wnonnull
--- a/doc/posix-headers/stdalign.texi	Sun Oct 18 09:32:21 2015 -0700
+++ b/doc/posix-headers/stdalign.texi	Sun Oct 18 09:52:45 2015 -0700
@@ -30,6 +30,11 @@
 which the operand can also be a unary expression, as with
 @code{sizeof}.  The Gnulib substitute does not support this extension.
 @item
+In ISO C11, the operand of @code{alignof}/@code{_Alignof} must be a
+complete type.  Recent versions of GCC support an extension in which
+the operand can also be structure type containing a flexible array
+member.  The Gnulib substitute does not support this extension.
+@item
 @code{_Alignas} and @code{alignas} are not always supported;
 on platforms lacking support, the
 macro @code{__alignas_is_defined} is not defined.
--- a/lib/fts.c	Sun Oct 18 09:32:21 2015 -0700
+++ b/lib/fts.c	Sun Oct 18 09:52:45 2015 -0700
@@ -64,6 +64,7 @@
 #include <errno.h>
 #include <stdalign.h>
 #include <stdbool.h>
+#include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -1907,12 +1908,16 @@
          * structure and the file name in one chunk.
          */
         len = offsetof(FTSENT, fts_name) + namelen + 1;
-        /* Round the allocation up so it has the same alignment as FTSENT,
+        /* Align the allocation size so that it works for FTSENT,
            so that trailing padding may be referenced by direct access
            to the flexible array members, without triggering undefined behavior
            by accessing bytes beyond the heap allocation.  This implicit access
-           was seen for example with ISDOT() and GCC 5.1.1 at -O2.  */
-        len = (len + alignof(FTSENT) - 1) & ~(alignof(FTSENT) - 1);
+           was seen for example with ISDOT() and GCC 5.1.1 at -O2.
+           Do not use alignof (FTSENT) here, since C11 prohibits
+           taking the alignment of a structure containing a flexible
+           array member.  */
+        len += alignof (max_align_t) - 1;
+        len &= ~ (alignof (max_align_t) - 1);
         if ((p = malloc(len)) == NULL)
                 return (NULL);
 
--- a/modules/fts	Sun Oct 18 09:32:21 2015 -0700
+++ b/modules/fts	Sun Oct 18 09:52:45 2015 -0700
@@ -31,6 +31,7 @@
 readdir
 stdalign
 stdbool
+stddef
 unistd-safer
 
 configure.ac: