]> granicus.if.org Git - clang/commitdiff
Sema: Restrict alignment to 2**28.
authorDavid Majnemer <david.majnemer@gmail.com>
Wed, 12 Feb 2014 20:36:10 +0000 (20:36 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Wed, 12 Feb 2014 20:36:10 +0000 (20:36 +0000)
Allowing alignment past this point causes wrap around within clang.

N.B.  GCC has the same restriction.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@201254 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDeclAttr.cpp
test/Sema/attr-aligned.c

index 93d5948ce8ff8a0c834697bbb9519bbce237b852..b570639af019cd43506cb57363b261823e429bcc 100644 (file)
@@ -1982,8 +1982,8 @@ def error_cannot_find_suitable_accessor : Error<
 
 def err_attribute_aligned_not_power_of_two : Error<
   "requested alignment is not a power of 2">;
-def err_attribute_aligned_greater_than_8192 : Error<
-  "requested alignment must be 8192 bytes or smaller">;
+def err_attribute_aligned_too_great : Error<
+  "requested alignment must be %0 bytes or smaller">;
 def warn_redeclaration_without_attribute_prev_attribute_ignored : Warning<
   "%0 redeclared without %1 attribute: previous %1 ignored">;
 def warn_attribute_ignored : Warning<"%0 attribute ignored">,
index 1bc34aa85f9dbb281381fa17d99df9cd37b2e9f0..ccfb7bc0ab75321a0d26ac559f09f67372cbfe5f 100644 (file)
@@ -2814,14 +2814,12 @@ void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
     return;
   }
 
-  if (TmpAttr.isDeclspec()) {
-    // We've already verified it's a power of 2, now let's make sure it's
-    // 8192 or less.
-    if (Alignment.getZExtValue() > 8192) {
-      Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
-        << E->getSourceRange();
-      return;
-    }
+  // Alignment calculations can wrap around if it's greater than 2**28.
+  unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456;
+  if (Alignment.getZExtValue() > MaxValidAlignment) {
+    Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
+                                                         << E->getSourceRange();
+    return;
   }
 
   AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
index c094ff172dd59cfd3fddb9c1cadef7236d6b0b5e..11d9e9eea35c2c2a09d22db44075791ee9fd943e 100644 (file)
@@ -1,6 +1,7 @@
 // RUN: %clang_cc1 -triple i386-apple-darwin9 -fsyntax-only -verify %s
 
 int x __attribute__((aligned(3))); // expected-error {{requested alignment is not a power of 2}}
+int y __attribute__((aligned(1 << 29))); // expected-error {{requested alignment must be 268435456 bytes or smaller}}
 
 // PR3254
 short g0[3] __attribute__((aligned));