Skip to content

Commit 72b3d7b

Browse files
committed
[clang][Interp] Makre sure we don't overflow Descriptor::AllocSize
We allocate the metadata and the array elements in one allocation, and we save its size in a field of type 'unsigned'. Makre sure the full size of the allocation doesn't overflow the field.
1 parent 8bf952d commit 72b3d7b

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

clang/lib/AST/Interp/Descriptor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ Descriptor::Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD,
303303
IsArray(true), CtorFn(getCtorArrayPrim(Type)),
304304
DtorFn(getDtorArrayPrim(Type)), MoveFn(getMoveArrayPrim(Type)) {
305305
assert(Source && "Missing source");
306+
assert(NumElems <= (MaxArrayElemBytes / ElemSize));
306307
}
307308

308309
/// Primitive unknown-size arrays.

clang/lib/AST/Interp/Descriptor.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef LLVM_CLANG_AST_INTERP_DESCRIPTOR_H
1414
#define LLVM_CLANG_AST_INTERP_DESCRIPTOR_H
1515

16+
#include "PrimType.h"
1617
#include "clang/AST/Decl.h"
1718
#include "clang/AST/Expr.h"
1819

@@ -125,6 +126,11 @@ struct Descriptor final {
125126
static constexpr MetadataSize InlineDescMD = sizeof(InlineDescriptor);
126127
static constexpr MetadataSize GlobalMD = sizeof(GlobalInlineDescriptor);
127128

129+
/// Maximum number of bytes to be used for array elements.
130+
static constexpr unsigned MaxArrayElemBytes =
131+
std::numeric_limits<decltype(AllocSize)>::max() - sizeof(InitMapPtr) -
132+
align(std::max(*InlineDescMD, *GlobalMD));
133+
128134
/// Pointer to the record, if block contains records.
129135
const Record *const ElemRecord = nullptr;
130136
/// Descriptor of the array element.

0 commit comments

Comments
 (0)