Skip to content

Commit 0e7f2b1

Browse files
tbaederrsmallp-o-p
authored andcommitted
[clang][bytecode] Diagnose negative array sizes differently (llvm#114380)
We have a special diagnostic ID for this.
1 parent 3890616 commit 0e7f2b1

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

clang/lib/AST/ByteCode/Interp.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,14 @@ bool CheckArraySize(InterpState &S, CodePtr OpPC, SizeT *NumElements,
273273
*NumElements > MaxElements) {
274274
if (!IsNoThrow) {
275275
const SourceInfo &Loc = S.Current->getSource(OpPC);
276-
S.FFDiag(Loc, diag::note_constexpr_new_too_large)
277-
<< NumElements->toDiagnosticString(S.getASTContext());
276+
277+
if (NumElements->isSigned() && NumElements->isNegative()) {
278+
S.FFDiag(Loc, diag::note_constexpr_new_negative)
279+
<< NumElements->toDiagnosticString(S.getASTContext());
280+
} else {
281+
S.FFDiag(Loc, diag::note_constexpr_new_too_large)
282+
<< NumElements->toDiagnosticString(S.getASTContext());
283+
}
278284
}
279285
return false;
280286
}

clang/test/AST/ByteCode/new-delete.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,15 @@ namespace NowThrowNew {
274274
static_assert(erroneous_array_bound_nothrow2(-1) == 0);// expected-error {{not an integral constant expression}}
275275
static_assert(!erroneous_array_bound_nothrow2(1LL << 62));// expected-error {{not an integral constant expression}}
276276

277+
constexpr bool erroneous_array_bound(long long n) {
278+
delete[] new int[n]; // both-note {{array bound -1 is negative}} both-note {{array bound 4611686018427387904 is too large}}
279+
return true;
280+
}
281+
static_assert(erroneous_array_bound(3));
282+
static_assert(erroneous_array_bound(0));
283+
static_assert(erroneous_array_bound(-1)); // both-error {{constant expression}} both-note {{in call}}
284+
static_assert(erroneous_array_bound(1LL << 62)); // both-error {{constant expression}} both-note {{in call}}
285+
277286
constexpr bool evaluate_nothrow_arg() {
278287
bool ok = false;
279288
delete new ((ok = true, std::nothrow)) int;

0 commit comments

Comments
 (0)