Author: flovent
Date: 2026-01-26T07:54:22+01:00
New Revision: a9b7b4d957aa5df863145cd4e70fe558a56e2f3d

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

LOG: [clang][bytecode] Fix crash caused by overflow of Casting float number to 
integer (#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  #177751.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Interp.h
    clang/test/AST/ByteCode/floats.cpp

Removed: 
    


################################################################################
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..7da4bf884e3e3 100644
--- a/clang/test/AST/ByteCode/floats.cpp
+++ b/clang/test/AST/ByteCode/floats.cpp
@@ -224,3 +224,18 @@ namespace nan {
                                                            // expected-error 
{{must be initialized by a constant expression}} \
                                                            // expected-note 
{{produces a NaN}}
 }
+
+#ifdef __SIZEOF_INT128__
+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}}
+}
+#endif


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

Reply via email to