Subject: Re: bug#21700: new snapshot available: grep-2.21.78-7da30

From: Paul Eggert <[email-masked]>
Date: Sun, 18 Oct 2015 10:10:36 -0700

Aaron Davies wrote:
> I ran into this trying to build current coreutils on Solaris with sun studio 11
>
> the workaround I found was to use sun studio 12

I'm also using Sun Studio 12. That being said, I now see that I was using Sun C 
5.9 (Patch 124867-12 2009/11/22), which is the original Sun Studio 12. I earlier 
reported that I was using Sun C 5.13, i.e., Oracle Solaris Studio 12.4, but I 
was mistaken. Oracle's compiler versioning is complicated; see 
<http://www.oracle.com/technetwork/server-storage/solarisstudio/training/index-jsp-141991.html>.

I just now looked at the C11 standard, and it prohibits Pádraig's 2015-06-25 
change to fts.c that uses alignof on a structure with a flexible array member. 
And I now see that IBM's xlc compiler also complains about this. though I didn't 
notice this earlier.

Although the C11 prohibition is arbitrary and perhaps not even intended, it's in 
the standard, and we should fix fts.c to conform. I installed the attached patch 
into gnulib, and this will be merged into grep the next time we update its 
gnulib version. I can see one more gnulib fix that's also needed for grep, and 
plan to follow up shortly.
From 8a9a3ff383d0db4e7df6210677119c99ea226805 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
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  <[email-masked]>
+
+	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  <[email-masked]>
 
 	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.
+@item
 @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 <errno.h>
 #include <stdalign.h>
 #include <stdbool.h>
+#include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -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