[clang] c5b890e - PR44268: Fix crash if __builtin_object_size is applied to a heap

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 13 18:42:05 PST 2019


Author: Richard Smith
Date: 2019-12-13T18:41:54-08:00
New Revision: c5b890e922432bd80a5e3c6d82994ef4cdc41900

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

LOG: PR44268: Fix crash if __builtin_object_size is applied to a heap
allocation.

Added: 
    

Modified: 
    clang/lib/AST/ExprConstant.cpp
    clang/test/SemaCXX/builtin-object-size-cxx14.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index fbc706b25d15..f3856f58b113 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -10218,7 +10218,7 @@ static QualType getObjectType(APValue::LValueBase B) {
   if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
     if (const VarDecl *VD = dyn_cast<VarDecl>(D))
       return VD->getType();
-  } else if (const Expr *E = B.get<const Expr*>()) {
+  } else if (const Expr *E = B.dyn_cast<const Expr*>()) {
     if (isa<CompoundLiteralExpr>(E))
       return E->getType();
   } else if (B.is<TypeInfoLValue>()) {

diff  --git a/clang/test/SemaCXX/builtin-object-size-cxx14.cpp b/clang/test/SemaCXX/builtin-object-size-cxx14.cpp
index a57bb6b53787..b7c6f6be01f5 100644
--- a/clang/test/SemaCXX/builtin-object-size-cxx14.cpp
+++ b/clang/test/SemaCXX/builtin-object-size-cxx14.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -std=c++14 %s
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx14 -std=c++14 %s
 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++2a %s
 
 typedef __SIZE_TYPE__ size_t;
@@ -113,3 +113,9 @@ namespace InvalidBase {
   constexpr size_t bos_dtor = __builtin_object_size(&(T&)(T&&)invalid_base_2(), 0);
   static_assert(bos_dtor == -1, "");
 }
+
+// PR44268
+constexpr int bos_new() { // cxx14-error {{constant expression}}
+  void *p = new int; // cxx14-note {{until C++20}}
+  return __builtin_object_size(p, 0);
+}


        


More information about the cfe-commits mailing list