Skip to content

Commit 65cede2

Browse files
authored
[clang][bytecode] Fix emitting dtors of zero-sized arrays (#134672)
Desc->getNumElems() returning 0 made us underflow here.
1 parent fb9915a commit 65cede2

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6823,15 +6823,17 @@ bool Compiler<Emitter>::emitDestruction(const Descriptor *Desc,
68236823
return true;
68246824
}
68256825

6826-
for (ssize_t I = Desc->getNumElems() - 1; I >= 0; --I) {
6827-
if (!this->emitConstUint64(I, Loc))
6828-
return false;
6829-
if (!this->emitArrayElemPtrUint64(Loc))
6830-
return false;
6831-
if (!this->emitDestruction(ElemDesc, Loc))
6832-
return false;
6833-
if (!this->emitPopPtr(Loc))
6834-
return false;
6826+
if (size_t N = Desc->getNumElems()) {
6827+
for (ssize_t I = N - 1; I >= 0; --I) {
6828+
if (!this->emitConstUint64(I, Loc))
6829+
return false;
6830+
if (!this->emitArrayElemPtrUint64(Loc))
6831+
return false;
6832+
if (!this->emitDestruction(ElemDesc, Loc))
6833+
return false;
6834+
if (!this->emitPopPtr(Loc))
6835+
return false;
6836+
}
68356837
}
68366838
return true;
68376839
}

clang/test/AST/ByteCode/cxx23.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,15 @@ namespace NonLiteralDtorInParam {
304304
// expected23-note {{non-constexpr function '~NonLiteral' cannot be used in a constant expression}}
305305
}
306306
}
307+
308+
namespace ZeroSizedArray {
309+
struct S {
310+
constexpr ~S() {
311+
}
312+
};
313+
constexpr int foo() {
314+
S s[0];
315+
return 1;
316+
}
317+
static_assert(foo() == 1);
318+
}

0 commit comments

Comments
 (0)