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

>From 3c8778cbb11c0bfcf69bdb5a9d2bc7780321b42e Mon Sep 17 00:00:00 2001
From: Serosh <[email protected]>
Date: Sat, 17 Jan 2026 16:46:46 +0530
Subject: [PATCH 1/3] [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, 18 insertions(+), 10 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..ff6045552b0e4 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -1083,21 +1083,21 @@ 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;
   }
 
-  return false;
+  if (DiscardResult)
+    return this->emitPop(classifyPrim(E), E);
+  return true;
 }
 
 template <class Emitter>
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));
+}

>From 9c5098c3f523b62ab35a84f932c750bf5756d145 Mon Sep 17 00:00:00 2001
From: Serosh <[email protected]>
Date: Wed, 28 Jan 2026 22:02:32 +0530
Subject: [PATCH 2/3] [clang][bytecode] Fix stack corruption in pointer
 arithmetic discard (refactor to switch)

---
 clang/lib/AST/ByteCode/Compiler.cpp | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Compiler.cpp 
b/clang/lib/AST/ByteCode/Compiler.cpp
index ff6045552b0e4..a0ef1009aa085 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -1080,13 +1080,16 @@ bool Compiler<Emitter>::VisitPointerArithBinOp(const 
BinaryOperator *E) {
 
   // Do the operation and optionally transform to
   // result pointer type.
-  if (Op == BO_Add) {
+  switch (Op) {
+  case BO_Add:
     if (!this->emitAddOffset(OffsetType, E))
       return false;
-  } else if (Op == BO_Sub) {
+    break;
+  case BO_Sub:
     if (!this->emitSubOffset(OffsetType, E))
       return false;
-  } else {
+    break;
+  default:
     return false;
   }
 

>From 2ff620d707e72c66a62ff0ddae720b5211ba8440 Mon Sep 17 00:00:00 2001
From: Serosh <[email protected]>
Date: Wed, 28 Jan 2026 22:54:57 +0530
Subject: [PATCH 3/3] Update test to expect comma operator warning

---
 clang/test/AST/ByteCode/gh176549.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/clang/test/AST/ByteCode/gh176549.cpp 
b/clang/test/AST/ByteCode/gh176549.cpp
index b56f762b7ede4..42d055e9608c7 100644
--- a/clang/test/AST/ByteCode/gh176549.cpp
+++ b/clang/test/AST/ByteCode/gh176549.cpp
@@ -1,8 +1,7 @@
 // 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));
+  i = 1 > (a + 1, sizeof(a)); // expected-warning {{left operand of comma 
operator has no effect}}
 }

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

Reply via email to