[clang] [clang][bytecode] Fix stack corruption in pointer arithmetic discard (PR #176555)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 17 03:23:00 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Serosh (Serosh-commits)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/176555.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+11-9)
- (added) clang/test/AST/ByteCode/gh176549.cpp (+8)
``````````diff
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));
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/176555
More information about the cfe-commits
mailing list