https://github.com/naveen-seth created 
https://github.com/llvm/llvm-project/pull/166762

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.

>From d151070bae2c2ecd05117686e20b2f75c26f4f2e Mon Sep 17 00:00:00 2001
From: naveen-seth <[email protected]>
Date: Thu, 6 Nov 2025 12:59:51 +0100
Subject: [PATCH] [clang][AST] Fix crash in constevaluated switch case
 statements

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.
---
 clang/lib/AST/ExprConstant.cpp | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

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;
     }

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

Reply via email to