[clang] 0c9e65d - [clang][bytecode] Decay pointers when adding a zero offset (#242785)

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 17 02:43:19 PST 2026


Author: Timm Baeder
Date: 2026-11-17T11:43:14+01:00
New Revision: 0c9e65d8f3d67c946058390eeec5d5a24125aaaf

URL: https://github.com/llvm/llvm-project/commit/0c9e65d8f3d67c946058390eeec5d5a24125aaaf
DIFF: https://github.com/llvm/llvm-project/commit/0c9e65d8f3d67c946058390eeec5d5a24125aaaf.diff

LOG: [clang][bytecode] Decay pointers when adding a zero offset (#242785)

Let zero offsets go through the `OffsetHelper` path, but return early
for non-arrays so we don't change those.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Interp.h
    clang/test/AST/ByteCode/arrays.cpp
    clang/test/AST/ByteCode/placement-new.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 18c2421a67231..80927ecc77c0a 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 b40307531cafc..535cb91481b7e 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 33dc2e595f485..47085d4276c85 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);
 }


        


More information about the cfe-commits mailing list