[clang] [clang][bytecode] Reject non-number values in Rem op (PR #194309)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 27 01:14:56 PDT 2026


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

None

>From 68448ec18dc107fc3bd3d75172f67a1f9312b0b6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Mon, 27 Apr 2026 10:14:10 +0200
Subject: [PATCH] [clang][bytecode] Reject non-number values in Rem op

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

diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index b9d69f5be3c00..860072423934f 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -196,6 +196,12 @@ bool CheckShift(InterpState &S, CodePtr OpPC, const LT &LHS, const RT &RHS,
 /// Checks if Div/Rem operation on LHS and RHS is valid.
 template <typename T>
 bool CheckDivRem(InterpState &S, CodePtr OpPC, const T &LHS, const T &RHS) {
+
+  if constexpr (isIntegralOrPointer<T>()) {
+    if (!LHS.isNumber() || !RHS.isNumber())
+      return false;
+  }
+
   if (RHS.isZero()) {
     const auto *Op = cast<BinaryOperator>(S.Current->getExpr(OpPC));
     if constexpr (std::is_same_v<T, Floating>) {
diff --git a/clang/test/AST/ByteCode/c.c b/clang/test/AST/ByteCode/c.c
index 2e5c003ae3f0f..b1009c0fdf71e 100644
--- a/clang/test/AST/ByteCode/c.c
+++ b/clang/test/AST/ByteCode/c.c
@@ -455,3 +455,6 @@ float r  = (float) (intptr_t) &r; // all-error {{initializer element is not a co
 void labelAndNull(void) { int bar = &*(void *)0 - &&baz; } // all-error {{use of undeclared label 'baz'}} \\
                                                            // pedantic-warning {{use of GNU address-of-label extension}} \
                                                            // pedantic-warning {{arithmetic on pointers to void is a GNU extension}}
+
+void nonNumberRem(void) { *((int *)0) = (long)foo % 42; } // all-warning {{indirection of non-volatile null pointer will be deleted, not trap}} \
+                                                          // all-note {{consider using __builtin_trap() or qualifying pointer with 'volatile'}}



More information about the cfe-commits mailing list