[clang] 0ca1051 - Check whether the allocated type is an array type before calling

Akira Hatanaka via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 26 14:21:33 PDT 2022


Author: Akira Hatanaka
Date: 2022-09-26T14:21:21-07:00
New Revision: 0ca1051bfc6e9548763f0a52641733393c33a427

URL: https://github.com/llvm/llvm-project/commit/0ca1051bfc6e9548763f0a52641733393c33a427
DIFF: https://github.com/llvm/llvm-project/commit/0ca1051bfc6e9548763f0a52641733393c33a427.diff

LOG: Check whether the allocated type is an array type before calling
checkArrayElementAlignment in Sema::BuildCXXNew

This commit fixes a bug that was introduced by adaf62ced and reported
here: https://reviews.llvm.org/D133711#3814717

Added: 
    

Modified: 
    clang/lib/Sema/SemaExprCXX.cpp
    clang/test/SemaCXX/array-alignment.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 3e65b3e254f3d..d6d8a7494cb00 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -2079,6 +2079,9 @@ Sema::BuildCXXNew(SourceRange Range, bool UseGlobal,
   if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange))
     return ExprError();
 
+  if (ArraySize && !checkArrayElementAlignment(AllocType, TypeRange.getBegin()))
+    return ExprError();
+
   // In ARC, infer 'retaining' for the allocated
   if (getLangOpts().ObjCAutoRefCount &&
       AllocType.getObjCLifetime() == Qualifiers::OCL_None &&
@@ -2449,8 +2452,6 @@ bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc,
   else if (RequireNonAbstractType(Loc, AllocType,
                                   diag::err_allocation_of_abstract_type))
     return true;
-  else if (!checkArrayElementAlignment(AllocType, Loc))
-    return true;
   else if (AllocType->isVariablyModifiedType())
     return Diag(Loc, diag::err_variably_modified_new_type)
              << AllocType;

diff  --git a/clang/test/SemaCXX/array-alignment.cpp b/clang/test/SemaCXX/array-alignment.cpp
index e6ff005ec3edf..d98d773d3ed2a 100644
--- a/clang/test/SemaCXX/array-alignment.cpp
+++ b/clang/test/SemaCXX/array-alignment.cpp
@@ -33,4 +33,5 @@ void test(char *p) {
   auto p3 = new AlignedStruct[1];
   auto p4 = (AlignedPackedStruct(*)[1])p; // expected-error {{size of array element}}
   auto p5 = new AlignedPackedStruct[1]; // expected-error {{size of array element}}
+  auto p6 = new AlignedPackedStruct;
 }


        


More information about the cfe-commits mailing list