[Bug c/69502] New: attribute aligned reduces alignment contrary to documentation

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Jan 26 21:42:00 GMT 2016


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69502

            Bug ID: 69502
           Summary: attribute aligned reduces alignment contrary to
                    documentation
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

The GCC manual says that:

"The aligned attribute can only increase the alignment; but you can decrease it
by specifying packed as well. See below."

However, GCC accepts the attribute in typedef definitions that decrease the
alignment of the defined type.  The program below fails with all versions of
GCC I've tried, all the way back to 4.5.

Either the documentation should be adjusted to reflect this feature or the
implementation should be changed to reject (or ignore with a warning)
definitions of types that would reduce a type's alignment requirement.

As a data point, the current versions of C and C++ disallow alignment
specification on typedefs.  As another data point, of the three GCC-compatible
compilers I tested, Sun cc successfully compiles the code below (with
warnings), while Clang and Intel CC fail in the same assertions as GCC.

$ cat u.c && /home/msebor/build/gcc-trunk-git/gcc/xgcc
-B/home/msebor/build/gcc-trunk-git/gcc -S -Wall -Wextra -Wpedantic -o/dev/null
-std=c11 u.c

#define Align(N)    __attribute__ ((aligned (N)))
#define Assert(e)   typedef int A [1 - 2 * !(e)]

void f (void)
{
  typedef int I32 Align (32);
  Assert (__alignof__ (I32) == 32);

  typedef I32 Align (1) I1;
  Assert (__alignof__ (I1) == __alignof__ (I32));

  typedef int Align (2) I2;
  Assert (__alignof__ (I2) == __alignof__ (int));
}
u.c: In function ‘f’:
u.c:2:33: error: size of array ‘A’ is negative
 #define Assert(e)   typedef int A [1 - 2 * !(e)]
                                 ^

u.c:10:3: note: in expansion of macro ‘Assert’
   Assert (__alignof__ (I1) == __alignof__ (I32));
   ^~~~~~

u.c:2:33: error: size of array ‘A’ is negative
 #define Assert(e)   typedef int A [1 - 2 * !(e)]
                                 ^

u.c:13:3: note: in expansion of macro ‘Assert’
   Assert (__alignof__ (I2) == __alignof__ (int));
   ^~~~~~


More information about the Gcc-bugs mailing list