https://github.com/Serosh-commits created 
https://github.com/llvm/llvm-project/pull/176555

The bytecode compiler was ignoring the DiscardResult flag in 
VisitPointerArithBinOp
, causing pointer addition and subtraction results to persist on the stack when 
they should have been popped (e.g., in comma expressions). This led to stack 
corruption and assertion failures in subsequent operations that encountered an 
unexpected pointer on the stack.

This patch refactors the unified addition/subtraction logic to ensure the 
result is properly popped when DiscardResult is true.

Fixes #176549

>From c01243bcbc79068bce0a3f7ad5a3779d53ea97c1 Mon Sep 17 00:00:00 2001
From: Serosh <[email protected]>
Date: Sat, 17 Jan 2026 16:46:46 +0530
Subject: [PATCH] [clang][bytecode] Fix stack corruption in pointer arithmetic
 discard

In VisitPointerArithBinOp, the result of pointer addition or subtraction
was always left on the stack, even if the result was intended to be
discarded (e.g. in a comma expression). This led to stack corruption
where subsequent operations would find an unexpected pointer on the
stack, causing an assertion failure in the InterpStack.

This patch ensures that we correctly respect the DiscardResult flag
for pointer arithmetic operations.

Fixes #176549
---
 clang/lib/AST/ByteCode/Compiler.cpp  | 20 +++++++++++---------
 clang/test/AST/ByteCode/gh176549.cpp |  8 ++++++++
 2 files changed, 19 insertions(+), 9 deletions(-)
 create mode 100644 clang/test/AST/ByteCode/gh176549.cpp

diff --git a/clang/lib/AST/ByteCode/Compiler.cpp 
b/clang/lib/AST/ByteCode/Compiler.cpp
index 21f8db06919ed..66b0cc4b5f6ab 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -1083,20 +1083,22 @@ bool Compiler<Emitter>::VisitPointerArithBinOp(const 
BinaryOperator *E) {
   if (Op == BO_Add) {
     if (!this->emitAddOffset(OffsetType, E))
       return false;
-
-    if (classifyPrim(E) != PT_Ptr)
-      return this->emitDecayPtr(PT_Ptr, classifyPrim(E), E);
-    return true;
-  }
-  if (Op == BO_Sub) {
+  } else if (Op == BO_Sub) {
     if (!this->emitSubOffset(OffsetType, E))
       return false;
+  } else {
+    return false;
+  }
 
-    if (classifyPrim(E) != PT_Ptr)
-      return this->emitDecayPtr(PT_Ptr, classifyPrim(E), E);
-    return true;
+  if (classifyPrim(E) != PT_Ptr) {
+    if (!this->emitDecayPtr(PT_Ptr, classifyPrim(E), E))
+      return false;
   }
 
+  if (DiscardResult)
+    return this->emitPop(classifyPrim(E), E);
+  return true;
+
   return false;
 }
 
diff --git a/clang/test/AST/ByteCode/gh176549.cpp 
b/clang/test/AST/ByteCode/gh176549.cpp
new file mode 100644
index 0000000000000..b56f762b7ede4
--- /dev/null
+++ b/clang/test/AST/ByteCode/gh176549.cpp
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify %s
+// expected-no-diagnostics
+
+const char a[4] = "abc";
+void foo() {
+  int i = 0;
+  i = 1 > (a + 1, sizeof(a));
+}

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

Reply via email to