Bug 84639

Summary: gcc/c-family/c-attribs.c:1822:27: runtime error: shift exponent -1 is negative
Product: gcc Reporter: Martin Liška <marxin>
Component: c++Assignee: Marek Polacek <mpolacek>
Status: RESOLVED FIXED    
Severity: normal CC: jason, nathan
Priority: P3    
Version: 8.0.1   
Target Milestone: ---   
Host: Target: x86_64-*-*, i?86-*-*
Build: Known to work:
Known to fail: Last reconfirmed: 2018-03-01 00:00:00
Bug Depends on:    
Bug Blocks: 63426    

Description Martin Liška 2018-03-01 08:02:12 UTC
UBSAN gcc prints the error:

$ UBSAN_OPTIONS="print_stacktrace=1" ./xg++ -B. /home/marxin/Programming/gcc/gcc/testsuite/g++.dg/cpp0x/alignas12.C
/home/marxin/Programming/gcc/gcc/testsuite/g++.dg/cpp0x/alignas12.C:6:10: error: ‘alignas’ argument has non-integral type ‘<unresolved overloaded function type>’
 alignas (f < int >) char c;  // { dg-error "non-integral type" }
          ^~~~~~~~~
../../gcc/c-family/c-attribs.c:1822:27: runtime error: shift exponent -1 is negative
    #0 0x5e311c in common_handle_aligned_attribute ../../gcc/c-family/c-attribs.c:1822
    #1 0xe29d06 in decl_attributes(tree_node**, tree_node*, int, tree_node*) ../../gcc/attribs.c:685
    #2 0x991bd9 in cplus_decl_attributes(tree_node**, tree_node*, int) ../../gcc/cp/decl2.c:1546
    #3 0x95790d in start_decl(cp_declarator const*, cp_decl_specifier_seq*, int, tree_node*, tree_node*, tree_node**) ../../gcc/cp/decl.c:5047
    #4 0xb95381 in cp_parser_init_declarator ../../gcc/cp/parser.c:19587
    #5 0xbaef4d in cp_parser_simple_declaration ../../gcc/cp/parser.c:13053
    #6 0xbb230e in cp_parser_block_declaration ../../gcc/cp/parser.c:12878
    #7 0xbc4460 in cp_parser_declaration ../../gcc/cp/parser.c:12776
    #8 0xbc66a1 in cp_parser_declaration_seq_opt ../../gcc/cp/parser.c:12652
    #9 0xbc7628 in cp_parser_translation_unit ../../gcc/cp/parser.c:4559
    #10 0xbc7628 in c_parse_file() ../../gcc/cp/parser.c:38880
    #11 0xeec35c in c_common_parse_file() ../../gcc/c-family/c-opts.c:1132
    #12 0x25a2cdc in compile_file ../../gcc/toplev.c:455
    #13 0x77e2e9 in do_compile ../../gcc/toplev.c:2132
    #14 0x77e2e9 in toplev::main(int, char**) ../../gcc/toplev.c:2267
    #15 0x78117a in main ../../gcc/main.c:39
    #16 0x7fd52c3026e4 in __libc_start_main (/lib64/libc.so.6+0x206e4)
    #17 0x7812a8 in _start (/home/marxin/Programming/gcc/objdir/gcc/cc1plus+0x7812a8)
Comment 1 Marek Polacek 2018-03-01 09:14:55 UTC
Confirmed with an assert.
Comment 2 Marek Polacek 2018-03-01 09:25:17 UTC
Can be fixed by moving the checking a bit above:

--- a/gcc/c-family/c-attribs.c
+++ b/gcc/c-family/c-attribs.c
@@ -1818,6 +1818,13 @@ common_handle_aligned_attribute (tree *node, tree name, tree args, int flags,
   /* Log2 of specified alignment.  */
   int pow2align = check_user_alignment (align_expr, true);
 
+  if (pow2align == -1
+      || !check_cxx_fundamental_alignment_constraints (*node, pow2align, flags))
+    {
+      *no_add_attrs = true;
+      return NULL_TREE;
+    }
+
   /* The alignment in bits corresponding to the specified alignment.  */
   unsigned bitalign = (1U << pow2align) * BITS_PER_UNIT;
 
@@ -1826,10 +1833,7 @@ common_handle_aligned_attribute (tree *node, tree name, tree args, int flags,
   unsigned curalign = 0;
   unsigned lastalign = 0;
 
-  if (pow2align == -1
-      || !check_cxx_fundamental_alignment_constraints (*node, pow2align, flags))
-    *no_add_attrs = true;
-  else if (is_type)
+  if (is_type)
     {
       if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
    /* OK, modify the type in place.  */;
Comment 3 Marek Polacek 2018-03-01 19:16:20 UTC
Author: mpolacek
Date: Thu Mar  1 19:15:42 2018
New Revision: 258109

URL: https://gcc.gnu.org/viewcvs?rev=258109&root=gcc&view=rev
Log:
	PR c++/84639
	* c-attribs.c (common_handle_aligned_attribute): Don't use invalid
	alignment in computation.

Modified:
    trunk/gcc/c-family/ChangeLog
    trunk/gcc/c-family/c-attribs.c
Comment 4 Marek Polacek 2018-03-01 19:16:41 UTC
Fixed in GCC 8.