Skip to content

Commit

Permalink
BytesUtils: Don't format parameters shorter than 32 bytes as integers
Browse files Browse the repository at this point in the history
  • Loading branch information
cameel committed Aug 17, 2023
1 parent 368ea01 commit 0f5aace
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions test/libsolidity/util/BytesUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,20 @@ string BytesUtils::formatRawBytes(
auto it = _bytes.begin();

if (_bytes.size() != ContractABIUtils::encodingSize(_parameters))
parameters = ContractABIUtils::defaultParameters((_bytes.size() + 31) / 32);
{
// Interpret all full 32-byte values as integers.
parameters = ContractABIUtils::defaultParameters(_bytes.size() / 32);

// We'd introduce trailing zero bytes if we interpreted the final bit as an integer.
// We want a right-aligned sequence of bytes instead.
if (_bytes.size() % 32 != 0)
parameters.push_back({
bytes(),
"",
ABIType{ABIType::HexString, ABIType::AlignRight, _bytes.size() % 32},
FormatInfo{},
});
}
else
parameters = _parameters;
soltestAssert(ContractABIUtils::encodingSize(parameters) >= _bytes.size());
Expand Down Expand Up @@ -371,7 +384,20 @@ string BytesUtils::formatBytesRange(
auto it = _bytes.begin();

if (_bytes.size() != ContractABIUtils::encodingSize(_parameters))
parameters = ContractABIUtils::defaultParameters((_bytes.size() + 31) / 32);
{
// Interpret all full 32-byte values as integers.
parameters = ContractABIUtils::defaultParameters(_bytes.size() / 32);

// We'd introduce trailing zero bytes if we interpreted the final bit as an integer.
// We want a right-aligned sequence of bytes instead.
if (_bytes.size() % 32 != 0)
parameters.push_back({
bytes(),
"",
ABIType{ABIType::HexString, ABIType::AlignRight, _bytes.size() % 32},
FormatInfo{},
});
}
else
parameters = _parameters;
soltestAssert(ContractABIUtils::encodingSize(parameters) >= _bytes.size());
Expand Down

0 comments on commit 0f5aace

Please sign in to comment.