https://github.com/flovent created 
https://github.com/llvm/llvm-project/pull/177815

Before this PR evaluation process will stop immediately regradless of whether 
it's set to handle overflow,

this will prevent us getting value from stack, which leads to crash(with or 
without assertion).

Closes  #177758.

>From 5e6827b75c6a99626519740fd0f2abf59cb7715e Mon Sep 17 00:00:00 2001
From: flovent <[email protected]>
Date: Sun, 25 Jan 2026 10:38:22 +0800
Subject: [PATCH] [clang][bytecode] Fix crash caused by overflow of Casting
 float number to integer

---
 clang/lib/AST/ByteCode/Interp.h    | 10 ++++++----
 clang/test/AST/ByteCode/floats.cpp | 13 +++++++++++++
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index cc8844e0fe90c..d856cd7c0a2d9 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -2628,8 +2628,9 @@ static inline bool CastFloatingIntegralAP(InterpState &S, 
CodePtr OpPC,
   auto Status = F.convertToInteger(Result);
 
   // Float-to-Integral overflow check.
-  if ((Status & APFloat::opStatus::opInvalidOp) && F.isFinite())
-    return handleOverflow(S, OpPC, F.getAPFloat());
+  if ((Status & APFloat::opStatus::opInvalidOp) && F.isFinite() &&
+      !handleOverflow(S, OpPC, F.getAPFloat()))
+    return false;
 
   FPOptions FPO = FPOptions::getFromOpaqueInt(FPOI);
 
@@ -2649,8 +2650,9 @@ static inline bool CastFloatingIntegralAPS(InterpState 
&S, CodePtr OpPC,
   auto Status = F.convertToInteger(Result);
 
   // Float-to-Integral overflow check.
-  if ((Status & APFloat::opStatus::opInvalidOp) && F.isFinite())
-    return handleOverflow(S, OpPC, F.getAPFloat());
+  if ((Status & APFloat::opStatus::opInvalidOp) && F.isFinite() &&
+      !handleOverflow(S, OpPC, F.getAPFloat()))
+    return false;
 
   FPOptions FPO = FPOptions::getFromOpaqueInt(FPOI);
 
diff --git a/clang/test/AST/ByteCode/floats.cpp 
b/clang/test/AST/ByteCode/floats.cpp
index 930921d0eee1f..29e806cb542f7 100644
--- a/clang/test/AST/ByteCode/floats.cpp
+++ b/clang/test/AST/ByteCode/floats.cpp
@@ -224,3 +224,16 @@ namespace nan {
                                                            // expected-error 
{{must be initialized by a constant expression}} \
                                                            // expected-note 
{{produces a NaN}}
 }
+
+namespace ConvertToIntOverflow {
+  // should not crash
+  enum { E = (__uint128_t)-1. }; // ref-error {{expression is not an integral 
constant expression}} \
+                                 // ref-note {{outside the range of 
representable values of type}} \
+                                 // expected-error {{expression is not an 
integral constant expression}} \
+                                 // expected-note {{outside the range of 
representable values of type}}
+
+  enum { F = (__int128)(3.0e38) }; // ref-error {{expression is not an 
integral constant expression}} \
+                                   // ref-note {{outside the range of 
representable values of type}} \
+                                   // expected-error {{expression is not an 
integral constant expression}} \
+                                   // expected-note {{outside the range of 
representable values of type}}
+}

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

Reply via email to