Skip to content

Commit d94a828

Browse files
authored
[clang][bytecode] Don't crash on a null Descriptor (llvm#160506)
... for dynamic memory allocation. This happens when the requested array size is too large. Fixes llvm#152951
1 parent 2d6ce51 commit d94a828

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

clang/lib/AST/ByteCode/Interp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3534,6 +3534,9 @@ inline bool AllocCN(InterpState &S, CodePtr OpPC, const Descriptor *ElementDesc,
35343534
if (!CheckDynamicMemoryAllocation(S, OpPC))
35353535
return false;
35363536

3537+
if (!ElementDesc)
3538+
return false;
3539+
35373540
SizeT NumElements = S.Stk.pop<SizeT>();
35383541
if (!CheckArraySize(S, OpPC, &NumElements, ElementDesc->getSize(),
35393542
IsNoThrow)) {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,19 @@ namespace NewNegSizeNothrow {
10911091
static_assert(test_nothrow_neg_size(), "expected nullptr");
10921092
} // namespace NewNegSizeNothrow
10931093

1094+
#if __SIZEOF_SIZE_T == 8
1095+
/// We can't allocate the array here as it is too big.
1096+
/// Make sure we're not crashing by assuming an non-null
1097+
/// Descriptor.
1098+
namespace HugeAllocation {
1099+
void *p;
1100+
void foo ()
1101+
{
1102+
p = new char [256][256][256][256][256];
1103+
}
1104+
}
1105+
#endif
1106+
10941107
#else
10951108
/// Make sure we reject this prior to C++20
10961109
constexpr int a() { // both-error {{never produces a constant expression}}

0 commit comments

Comments
 (0)