https://github.com/tbaederr updated 
https://github.com/llvm/llvm-project/pull/242785

>From da332f262ea7e910966c139ac614e2d814f6488b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]>
Date: Sun, 15 Nov 2026 07:15:11 +0100
Subject: [PATCH] [clang][bytecode] Decay pointers when adding a zero offset

---
 clang/lib/AST/ByteCode/Interp.h           | 15 ++++++++------
 clang/test/AST/ByteCode/arrays.cpp        | 25 +++++++++++++++++++++++
 clang/test/AST/ByteCode/placement-new.cpp | 19 +++++++++++++++++
 3 files changed, 53 insertions(+), 6 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 18c2421a67231e..80927ecc77c0ae 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -2621,12 +2621,14 @@ std::optional<Pointer> OffsetHelper(InterpState &S, 
CodePtr OpPC,
 std::optional<Pointer> addSubOffsetOpaque(InterpState &S, CodePtr OpPC,
                                           const Pointer &Ptr, APSInt &&Offset,
                                           ArithOp Op);
+
 template <PrimType Name, class T = typename PrimConv<Name>::T>
 bool AddOffset(InterpState &S, CodePtr OpPC) {
   const T &Offset = S.Stk.pop<T>();
   const Pointer &Ptr = S.Stk.pop<Pointer>().expand();
 
-  if (Offset.isZero()) {
+  // Adding zero to a non-array doesn't change anything.
+  if (Offset.isZero() && !Ptr.inArray()) {
     S.Stk.push<Pointer>(Ptr);
     return true;
   }
@@ -2656,6 +2658,12 @@ bool SubOffset(InterpState &S, CodePtr OpPC) {
   const T &Offset = S.Stk.pop<T>();
   const Pointer &Ptr = S.Stk.pop<Pointer>().expand();
 
+  // Subtracting zero from a non-array doesn't change anything.
+  if (Offset.isZero() && !Ptr.inArray()) {
+    S.Stk.push<Pointer>(Ptr);
+    return true;
+  }
+
   if (Ptr.isOpaquePointer()) {
     if (std::optional<Pointer> Result =
             addSubOffsetOpaque(S, OpPC, Ptr, Offset.toAPSInt(), ArithOp::Sub)) 
{
@@ -2665,11 +2673,6 @@ bool SubOffset(InterpState &S, CodePtr OpPC) {
     return false;
   }
 
-  if (Offset.isZero()) {
-    S.Stk.push<Pointer>(Ptr);
-    return true;
-  }
-
   if (!CheckNull(S, OpPC, Ptr, CSK_ArrayIndex) && S.getLangOpts().CPlusPlus)
     return false;
 
diff --git a/clang/test/AST/ByteCode/arrays.cpp 
b/clang/test/AST/ByteCode/arrays.cpp
index b40307531cafc2..535cb91481b7ee 100644
--- a/clang/test/AST/ByteCode/arrays.cpp
+++ b/clang/test/AST/ByteCode/arrays.cpp
@@ -855,3 +855,28 @@ namespace ErroneousArraySubscriptExpr {
     foo(val, 2) = 42;
   }
 }
+
+namespace ZeroNonArray {
+  struct __bit_iterator {
+    unsigned* __seg_;
+  };
+
+  struct bitset {
+    unsigned __first_;
+    constexpr bitset() : __first_() {}
+
+    constexpr void set() {
+      __bit_iterator  __trans_tmp_4{&__first_};
+      long __trans_tmp_3 = 0;
+      __trans_tmp_4.__seg_ += 0;
+      __trans_tmp_3 = *__trans_tmp_4.__seg_;
+    }
+  };
+
+  constexpr bool test() {
+    bitset v;
+    v.set();
+    return true;
+  }
+  static_assert(test(), "");
+}
diff --git a/clang/test/AST/ByteCode/placement-new.cpp 
b/clang/test/AST/ByteCode/placement-new.cpp
index 33dc2e595f4858..47085d4276c85a 100644
--- a/clang/test/AST/ByteCode/placement-new.cpp
+++ b/clang/test/AST/ByteCode/placement-new.cpp
@@ -584,6 +584,25 @@ namespace PrimArray {
   }
   static_assert(primElem() == 30); // both-error {{not an integral constant 
expression}} \
                                    // both-note {{in call to}}
+}
+
+namespace ZeroOffset {
+  struct S {};
+  constexpr int test() {
+    S *p = std::allocator<S>{}.allocate(1);
+    new (p + 0) S{};
 
+    std::allocator<S>{}.deallocate(p);
+    return 0;
+  }
+  static_assert(test() == 0);
 
+  constexpr int test2() {
+    S *p = std::allocator<S>{}.allocate(1);
+    new (p - 0) S{};
+
+    std::allocator<S>{}.deallocate(p);
+    return 0;
+  }
+  static_assert(test2() == 0);
 }

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

Reply via email to