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

via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 27 01:15:34 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/194309.diff


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/Interp.h (+6) 
- (modified) clang/test/AST/ByteCode/c.c (+3) 


``````````diff
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'}}

``````````

</details>


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


More information about the cfe-commits mailing list