From 8a9a3ff383d0db4e7df6210677119c99ea226805 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 18 Oct 2015 09:52:45 -0700 Subject: [PATCH] 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. --- ChangeLog | 9 +++++++++ doc/posix-headers/stdalign.texi | 5 +++++ lib/fts.c | 11 ++++++++--- modules/fts | 1 + 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index fcfab70..01a0536 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2015-10-18 Paul Eggert + + 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 time_rz: avoid warning from bleeding-edge gcc's -Wnonnull diff --git a/doc/posix-headers/stdalign.texi b/doc/posix-headers/stdalign.texi index 036560d..d8a9bee 100644 --- a/doc/posix-headers/stdalign.texi +++ b/doc/posix-headers/stdalign.texi @@ -30,6 +30,11 @@ parenthesized type. Recent versions of GCC support an extension in 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. address@hidden @code{_Alignas} and @code{alignas} are not always supported; on platforms lacking support, the macro @code{__alignas_is_defined} is not defined. diff --git a/lib/fts.c b/lib/fts.c index a2f65eb..ea73675 100644 --- a/lib/fts.c +++ b/lib/fts.c @@ -64,6 +64,7 @@ static char sccsid[] = "@(#)fts.c 8.6 (Berkeley) 8/14/94"; #include #include #include +#include #include #include #include @@ -1907,12 +1908,16 @@ fts_alloc (FTS *sp, const char *name, register size_t namelen) * 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); diff --git a/modules/fts b/modules/fts index b5dcd32..55c09e7 100644 --- a/modules/fts +++ b/modules/fts @@ -31,6 +31,7 @@ opendir readdir stdalign stdbool +stddef unistd-safer configure.ac: -- 2.1.0