r201254 - Sema: Restrict alignment to 2**28.

David Majnemer david.majnemer at gmail.com
Wed Feb 12 12:36:10 PST 2014


Author: majnemer
Date: Wed Feb 12 14:36:10 2014
New Revision: 201254

URL: http://llvm.org/viewvc/llvm-project?rev=201254&view=rev
Log:
Sema: Restrict alignment to 2**28.

Allowing alignment past this point causes wrap around within clang.

N.B.  GCC has the same restriction.

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

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=201254&r1=201253&r2=201254&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Feb 12 14:36:10 2014
@@ -1982,8 +1982,8 @@ def error_cannot_find_suitable_accessor
 
 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">,

Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=201254&r1=201253&r2=201254&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Wed Feb 12 14:36:10 2014
@@ -2814,14 +2814,12 @@ void Sema::AddAlignedAttr(SourceRange At
     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,

Modified: cfe/trunk/test/Sema/attr-aligned.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-aligned.c?rev=201254&r1=201253&r2=201254&view=diff
==============================================================================
--- cfe/trunk/test/Sema/attr-aligned.c (original)
+++ cfe/trunk/test/Sema/attr-aligned.c Wed Feb 12 14:36:10 2014
@@ -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));





More information about the cfe-commits mailing list