[clang] 1c818b0 - [clang][Interp] Fix a crash when calling invalid constexpr functions

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 8 06:38:27 PDT 2023


Author: Timm Bäder
Date: 2023-04-08T15:28:47+02:00
New Revision: 1c818b0a4f92abd6a450841ebca37f3ef5dac0bc

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

LOG: [clang][Interp] Fix a crash when calling invalid constexpr functions

Differential Revision: https://reviews.llvm.org/D147845

Added: 
    

Modified: 
    clang/lib/AST/Interp/InterpFrame.cpp
    clang/test/AST/Interp/functions.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/InterpFrame.cpp b/clang/lib/AST/Interp/InterpFrame.cpp
index 897d420e91a3a..a8c4aab84ef8d 100644
--- a/clang/lib/AST/Interp/InterpFrame.cpp
+++ b/clang/lib/AST/Interp/InterpFrame.cpp
@@ -127,7 +127,8 @@ void print(llvm::raw_ostream &OS, const Pointer &P, ASTContext &Ctx,
   }
 
   // Drop the first pointer since we print it unconditionally anyway.
-  Levels.erase(Levels.begin());
+  if (!Levels.empty())
+    Levels.erase(Levels.begin());
 
   printDesc(P.getDeclDesc());
   for (const auto &It : Levels) {

diff  --git a/clang/test/AST/Interp/functions.cpp b/clang/test/AST/Interp/functions.cpp
index 06edcdeffa705..8b6b1bcd6f5c3 100644
--- a/clang/test/AST/Interp/functions.cpp
+++ b/clang/test/AST/Interp/functions.cpp
@@ -202,3 +202,20 @@ constexpr int nyd(int m);
 constexpr int doit() { return nyd(10); }
 constexpr int nyd(int m) { return m; }
 static_assert(doit() == 10, "");
+
+namespace InvalidCall {
+  struct S {
+    constexpr int a() const { // expected-error {{never produces a constant expression}} \
+                              // ref-error {{never produces a constant expression}}
+      return 1 / 0; // expected-note 2{{division by zero}} \
+                    // expected-warning {{is undefined}} \
+                    // ref-note 2{{division by zero}} \
+                    // ref-warning {{is undefined}}
+    }
+  };
+  constexpr S s;
+  static_assert(s.a() == 1); // expected-error {{not an integral constant expression}} \
+                             // expected-note {{in call to}} \
+                             // ref-error {{not an integral constant expression}} \
+                             // ref-note {{in call to}}
+}


        


More information about the cfe-commits mailing list