diff options
author | Timm Baeder <tbaeder@redhat.com> | 2025-04-08 06:09:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-08 06:09:21 +0200 |
commit | 65cede26a6b06ba02c08284fada06c46c0289704 (patch) | |
tree | df5b29b7763d8b78123e0d07cf8b375514cfeda2 | |
parent | fb9915a3918e3a9659a7f2825ee35bada3a2baf1 (diff) | |
download | llvm-65cede26a6b06ba02c08284fada06c46c0289704.zip llvm-65cede26a6b06ba02c08284fada06c46c0289704.tar.gz llvm-65cede26a6b06ba02c08284fada06c46c0289704.tar.bz2 |
[clang][bytecode] Fix emitting dtors of zero-sized arrays (#134672)
Desc->getNumElems() returning 0 made us underflow here.
-rw-r--r-- | clang/lib/AST/ByteCode/Compiler.cpp | 20 | ||||
-rw-r--r-- | clang/test/AST/ByteCode/cxx23.cpp | 12 |
2 files changed, 23 insertions, 9 deletions
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index db87ea1..e4f87d8 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -6823,15 +6823,17 @@ bool Compiler<Emitter>::emitDestruction(const Descriptor *Desc, return true; } - for (ssize_t I = Desc->getNumElems() - 1; I >= 0; --I) { - if (!this->emitConstUint64(I, Loc)) - return false; - if (!this->emitArrayElemPtrUint64(Loc)) - return false; - if (!this->emitDestruction(ElemDesc, Loc)) - return false; - if (!this->emitPopPtr(Loc)) - return false; + if (size_t N = Desc->getNumElems()) { + for (ssize_t I = N - 1; I >= 0; --I) { + if (!this->emitConstUint64(I, Loc)) + return false; + if (!this->emitArrayElemPtrUint64(Loc)) + return false; + if (!this->emitDestruction(ElemDesc, Loc)) + return false; + if (!this->emitPopPtr(Loc)) + return false; + } } return true; } diff --git a/clang/test/AST/ByteCode/cxx23.cpp b/clang/test/AST/ByteCode/cxx23.cpp index 6a62ac1..d0ade4f 100644 --- a/clang/test/AST/ByteCode/cxx23.cpp +++ b/clang/test/AST/ByteCode/cxx23.cpp @@ -304,3 +304,15 @@ namespace NonLiteralDtorInParam { // expected23-note {{non-constexpr function '~NonLiteral' cannot be used in a constant expression}} } } + +namespace ZeroSizedArray { + struct S { + constexpr ~S() { + } + }; + constexpr int foo() { + S s[0]; + return 1; + } + static_assert(foo() == 1); +} |