[clang] [clang][bytecode] Reject float-to-int casts on non-numbers (PR #193968)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 24 06:34:20 PDT 2026


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/193968

None

>From c2ddb13c45ba758e89f40863437aa077de4782b6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 24 Apr 2026 15:33:39 +0200
Subject: [PATCH] [clang][bytecode] Reject float-to-int casts on non-numbers

---
 clang/lib/AST/ByteCode/Interp.h | 6 ++++++
 clang/test/AST/ByteCode/c.c     | 2 ++
 2 files changed, 8 insertions(+)

diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 0f7f2c4e1fdec..49364a2ee2a62 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -2832,6 +2832,12 @@ template <PrimType Name, class T = typename PrimConv<Name>::T>
 bool CastIntegralFloating(InterpState &S, CodePtr OpPC,
                           const llvm::fltSemantics *Sem, uint32_t FPOI) {
   const T &From = S.Stk.pop<T>();
+
+  if constexpr (isIntegralOrPointer<T>()) {
+    if (!From.isNumber())
+      return false;
+  }
+
   APSInt FromAP = From.toAPSInt();
 
   FPOptions FPO = FPOptions::getFromOpaqueInt(FPOI);
diff --git a/clang/test/AST/ByteCode/c.c b/clang/test/AST/ByteCode/c.c
index 78820d04ba3f5..31368d0d9d62f 100644
--- a/clang/test/AST/ByteCode/c.c
+++ b/clang/test/AST/ByteCode/c.c
@@ -449,3 +449,5 @@ void strcpyDouble(void) {
 
 int *iptr;
 void ignoredConditional(void) { *iptr = (((_Complex double)1.0 ? 2 : 3), a); } // all-warning {{left operand of comma operator has no effect}}
+
+float r  = (float) (intptr_t) &r; // all-error {{initializer element is not a compile-time constant}}



More information about the cfe-commits mailing list