Skip to content

[clang][bytecode] Fix subtracting zero-sized pointers #135929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2141,12 +2141,25 @@ static inline bool DecPtr(InterpState &S, CodePtr OpPC) {

/// 1) Pops a Pointer from the stack.
/// 2) Pops another Pointer from the stack.
/// 3) Pushes the different of the indices of the two pointers on the stack.
/// 3) Pushes the difference of the indices of the two pointers on the stack.
template <PrimType Name, class T = typename PrimConv<Name>::T>
inline bool SubPtr(InterpState &S, CodePtr OpPC) {
const Pointer &LHS = S.Stk.pop<Pointer>();
const Pointer &RHS = S.Stk.pop<Pointer>();

if (!Pointer::hasSameBase(LHS, RHS) && S.getLangOpts().CPlusPlus) {
S.FFDiag(S.Current->getSource(OpPC),
diag::note_constexpr_pointer_arith_unspecified)
<< LHS.toDiagnosticString(S.getASTContext())
<< RHS.toDiagnosticString(S.getASTContext());
return false;
}

if (LHS == RHS) {
S.Stk.push<T>();
return true;
}

for (const Pointer &P : {LHS, RHS}) {
if (P.isZeroSizeArray()) {
QualType PtrT = P.getType();
Expand All @@ -2163,21 +2176,6 @@ inline bool SubPtr(InterpState &S, CodePtr OpPC) {
}
}

if (RHS.isZero()) {
S.Stk.push<T>(T::from(LHS.getIndex()));
return true;
}

if (!Pointer::hasSameBase(LHS, RHS) && S.getLangOpts().CPlusPlus) {
// TODO: Diagnose.
return false;
}

if (LHS.isZero() && RHS.isZero()) {
S.Stk.push<T>();
return true;
}

T A = LHS.isBlockPointer()
? (LHS.isElementPastEnd() ? T::from(LHS.getNumElems())
: T::from(LHS.getIndex()))
Expand Down
9 changes: 7 additions & 2 deletions clang/lib/AST/ByteCode/Pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,17 @@ class Pointer {
return false;
if (isIntegralPointer())
return P.asIntPointer().Value == asIntPointer().Value &&
Offset == P.Offset;
P.asIntPointer().Desc == asIntPointer().Desc && P.Offset == Offset;

if (isFunctionPointer())
return P.asFunctionPointer().getFunction() ==
asFunctionPointer().getFunction() &&
P.Offset == Offset;

assert(isBlockPointer());
return P.asBlockPointer().Pointee == asBlockPointer().Pointee &&
P.asBlockPointer().Base == asBlockPointer().Base &&
Offset == P.Offset;
P.Offset == Offset;
}

bool operator!=(const Pointer &P) const { return !(P == *this); }
Expand Down
6 changes: 5 additions & 1 deletion clang/test/AST/ByteCode/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ constexpr int k1 = &arr[1] - &arr[0];
static_assert(k1 == 1, "");
static_assert((&arr[0] - &arr[1]) == -1, "");

constexpr int k2 = &arr2[1] - &arr[0]; // both-error {{must be initialized by a constant expression}}
constexpr int k2 = &arr2[1] - &arr[0]; // both-error {{must be initialized by a constant expression}} \
// expected-note {{arithmetic involving unrelated objects}}

static_assert((arr + 0) == arr, "");
static_assert(&arr[0] == arr, "");
Expand Down Expand Up @@ -735,6 +736,9 @@ namespace ZeroSizeTypes {
return &arr[3] - &arr[0]; // both-note {{subtraction of pointers to type 'int[0]' of zero size}} \
// both-warning {{subtraction of pointers to type 'int[0]' of zero size has undefined behavior}}
}

constexpr int z[0]{};
static_assert((z - z) == 0);
}

namespace InvalidIndex {
Expand Down
25 changes: 25 additions & 0 deletions clang/test/AST/ByteCode/new-delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,31 @@ namespace PR45350 {
static_assert(f(6) == 543210);
}

namespace ZeroSizeSub {
consteval unsigned ptr_diff1() {
int *b = new int[0];
unsigned d = 0;
d = b - b;
delete[] b;

return d;
}
static_assert(ptr_diff1() == 0);


consteval unsigned ptr_diff2() { // both-error {{never produces a constant expression}}
int *a = new int[0];
int *b = new int[0];

unsigned d = a - b; // both-note 2{{arithmetic involving unrelated objects}}
delete[] b;
delete[] a;
return d;
}
static_assert(ptr_diff2() == 0); // both-error {{not an integral constant expression}} \
// both-note {{in call to}}
}

#else
/// Make sure we reject this prior to C++20
constexpr int a() { // both-error {{never produces a constant expression}}
Expand Down
Loading