https://github.com/mugiwaraluffy56 updated 
https://github.com/llvm/llvm-project/pull/178652

>From de9c82488d954c995301badd8ea79b8d39eefdb7 Mon Sep 17 00:00:00 2001
From: mugiwaraluffy56 <[email protected]>
Date: Thu, 29 Jan 2026 19:02:20 +0530
Subject: [PATCH] [clang][bytecode] Fix crash on __builtin_align_up with
 one-past-end pointers

Fix assertion failure when evaluating __builtin_align_up/down/is_aligned
with one-past-end pointers like `&array[size]`.

The issue was that getIndex() calls getOffset() which asserts when
Offset == PastEndMark. For one-past-end pointers, we now use
getNumElems() instead which gives the correct index.

Fixes #178647
---
 clang/lib/AST/ByteCode/InterpBuiltin.cpp      | 5 ++++-
 clang/test/AST/ByteCode/builtin-align-cxx.cpp | 3 +++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp 
b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 4cf6898df3692..fd75e212e7d5e 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1211,7 +1211,10 @@ static bool 
interp__builtin_is_aligned_up_down(InterpState &S, CodePtr OpPC,
   if (!Ptr.isBlockPointer())
     return false;
 
-  unsigned PtrOffset = Ptr.getIndex();
+  // For one-past-end pointers, we can't call getIndex() since it asserts.
+  // Use getNumElems() instead which gives the correct index for past-end.
+  unsigned PtrOffset =
+      Ptr.isElementPastEnd() ? Ptr.getNumElems() : Ptr.getIndex();
   CharUnits BaseAlignment =
       S.getASTContext().getDeclAlign(Ptr.getDeclDesc()->asValueDecl());
   CharUnits PtrAlign =
diff --git a/clang/test/AST/ByteCode/builtin-align-cxx.cpp 
b/clang/test/AST/ByteCode/builtin-align-cxx.cpp
index a1edf307d6c47..636d58383867a 100644
--- a/clang/test/AST/ByteCode/builtin-align-cxx.cpp
+++ b/clang/test/AST/ByteCode/builtin-align-cxx.cpp
@@ -240,5 +240,8 @@ static_assert(!__builtin_is_aligned(static_cast<unsigned 
long>(7), static_cast<s
 static_assert(!__builtin_is_aligned(static_cast<signed long>(7), 
static_cast<unsigned short>(4)), "");
 static_assert(!__builtin_is_aligned(static_cast<unsigned short>(7), 
static_cast<signed long>(4)), "");
 
+// Check that one-past-end pointers work correctly (GH#178647).
+static_assert(__builtin_align_up(&align32array[128], 4) == align32array + 128, 
"");
+
 // Check the diagnostic message
 _Alignas(void) char align_void_array[1]; // both-error {{invalid application 
of '_Alignas' to an incomplete type 'void'}}

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

Reply via email to