llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Naveen Seth Hanig (naveen-seth)

<details>
<summary>Changes</summary>

Fixes #<!-- -->166328.

Fixes a crash that occurs when constevaluating a function in a switch case with 
a missing non-type template parameter.
This change guards against calling `EvaluateKnownConstInt` on value-dependent 
expressions in switch cases to prevent the crash.

---
Full diff: https://github.com/llvm/llvm-project/pull/166762.diff


1 Files Affected:

- (modified) clang/lib/AST/ExprConstant.cpp (+9-4) 


``````````diff
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 8fab6efafb983..5320031f585e7 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -5452,10 +5452,15 @@ static EvalStmtResult EvaluateSwitch(StmtResult 
&Result, EvalInfo &Info,
     }
 
     const CaseStmt *CS = cast<CaseStmt>(SC);
-    APSInt LHS = CS->getLHS()->EvaluateKnownConstInt(Info.Ctx);
-    APSInt RHS = CS->getRHS() ? CS->getRHS()->EvaluateKnownConstInt(Info.Ctx)
-                              : LHS;
-    if (LHS <= Value && Value <= RHS) {
+    const Expr *LHSExpr = CS->getLHS();
+    const Expr *RHSExpr = CS->getLHS();
+    if (LHSExpr->isValueDependent() || (RHSExpr && 
RHSExpr->isValueDependent()))
+      return ESR_Failed;
+
+    APSInt LHSValue = LHSExpr->EvaluateKnownConstInt(Info.Ctx);
+    APSInt RHSValue =
+        RHSExpr ? RHSExpr->EvaluateKnownConstInt(Info.Ctx) : LHSValue;
+    if (LHSValue <= Value && Value <= RHSValue) {
       Found = SC;
       break;
     }

``````````

</details>


https://github.com/llvm/llvm-project/pull/166762
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to