[PATCH] D92969: [AST] Fix a constexpr-evaluator crash on error-dependent returnstmt.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 9 13:53:15 PST 2020


hokein created this revision.
hokein added reviewers: sammccall, rsmith.
hokein requested review of this revision.
Herald added a project: clang.

When the evaluator encounters an error-dependent returnstmt, before this patch
it returned a ESR_Returned without setting the result, the callsides think this
is a successful execution, and try to access the Result which causes the crash.

The fix is to always return failed as we don't know the result of the
error-dependent return stmt.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92969

Files:
  clang/lib/AST/ExprConstant.cpp
  clang/test/SemaCXX/constexpr-function-recovery-crash.cpp


Index: clang/test/SemaCXX/constexpr-function-recovery-crash.cpp
===================================================================
--- clang/test/SemaCXX/constexpr-function-recovery-crash.cpp
+++ clang/test/SemaCXX/constexpr-function-recovery-crash.cpp
@@ -66,3 +66,6 @@
 constexpr int test9(int x) {
   return f<1>(f<x>(1)); // expected-error {{no matching function for call to 'f'}}
 }
+
+constexpr int test10() { return undef(); } // expected-error {{use of undeclared identifier 'undef'}}
+static_assert(test10() <= 1, "should not crash"); // expected-error {{static_assert expression is not an integral constant expression}}
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -5142,8 +5142,10 @@
   case Stmt::ReturnStmtClass: {
     const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue();
     FullExpressionRAII Scope(Info);
-    if (RetExpr && RetExpr->isValueDependent())
-      return EvaluateDependentExpr(RetExpr, Info) ? ESR_Returned : ESR_Failed;
+    if (RetExpr && RetExpr->isValueDependent()) {
+      EvaluateDependentExpr(RetExpr, Info);
+      return ESR_Failed;
+    }
     if (RetExpr &&
         !(Result.Slot
               ? EvaluateInPlace(Result.Value, Info, *Result.Slot, RetExpr)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92969.310651.patch
Type: text/x-patch
Size: 1340 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201209/c61ba1d0/attachment.bin>


More information about the cfe-commits mailing list