Skip to content

Commit

Permalink
Fix off-by-one error in check for max object size
Browse files Browse the repository at this point in the history
  • Loading branch information
MikePopoloski committed Mar 12, 2023
1 parent b32e6ee commit d9c8300
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions source/ast/types/AllTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,9 +691,10 @@ const Type& FixedSizeUnpackedArrayType::fromDim(const Scope& scope, const Type&

auto& comp = scope.getCompilation();
auto width = checkedMulU32(elementType.getSelectableWidth(), dim.width());
if (!width || width > uint32_t(INT32_MAX)) {
const uint32_t maxWidth = uint32_t(INT32_MAX) + 1;
if (!width || width > maxWidth) {
uint64_t fullWidth = uint64_t(elementType.getSelectableWidth()) * dim.width();
scope.addDiag(diag::ObjectTooLarge, sourceRange.get()) << fullWidth << INT32_MAX;
scope.addDiag(diag::ObjectTooLarge, sourceRange.get()) << fullWidth << maxWidth;
return comp.getErrorType();
}

Expand Down Expand Up @@ -886,9 +887,10 @@ const Type& UnpackedStructType::fromSyntax(const ASTContext& context,
fields.push_back(field);

bitOffset += field->getType().getSelectableWidth();
if (bitOffset > uint32_t(INT32_MAX)) {
const uint32_t maxWidth = uint32_t(INT32_MAX) + 1;
if (bitOffset > maxWidth) {
context.addDiag(diag::ObjectTooLarge, syntax.sourceRange())
<< bitOffset << INT32_MAX;
<< bitOffset << maxWidth;
return comp.getErrorType();
}
}
Expand Down

0 comments on commit d9c8300

Please sign in to comment.