BytesUtils: Don't introduce garbage when formatting byte vectors shorter than expected

This commit is contained in:
Kamil Śliwak 2023-08-17 18:35:20 +02:00
parent ef5f131862
commit 368ea01bb2

View File

@ -26,6 +26,8 @@
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <algorithm>
#include <iterator>
#include <iomanip> #include <iomanip>
#include <memory> #include <memory>
#include <regex> #include <regex>
@ -253,7 +255,8 @@ std::string BytesUtils::formatFixedPoint(bytes const& _bytes, bool _signed, size
string BytesUtils::formatRawBytes( string BytesUtils::formatRawBytes(
bytes const& _bytes, bytes const& _bytes,
solidity::frontend::test::ParameterList const& _parameters, solidity::frontend::test::ParameterList const& _parameters,
string _linePrefix) string _linePrefix
)
{ {
stringstream os; stringstream os;
ParameterList parameters; ParameterList parameters;
@ -263,16 +266,19 @@ string BytesUtils::formatRawBytes(
parameters = ContractABIUtils::defaultParameters((_bytes.size() + 31) / 32); parameters = ContractABIUtils::defaultParameters((_bytes.size() + 31) / 32);
else else
parameters = _parameters; parameters = _parameters;
soltestAssert(ContractABIUtils::encodingSize(parameters) >= _bytes.size());
for (auto const& parameter: parameters) for (auto const& parameter: parameters)
{ {
bytes byteRange{it, it + static_cast<long>(parameter.abiType.size)}; long actualSize = min(distance(it, _bytes.end()), static_cast<long>(parameter.abiType.size));
bytes byteRange(parameter.abiType.size, 0);
copy(it, it + actualSize, byteRange.begin());
os << _linePrefix << byteRange; os << _linePrefix << byteRange;
if (&parameter != &parameters.back()) if (&parameter != &parameters.back())
os << endl; os << endl;
it += static_cast<long>(parameter.abiType.size); it += actualSize;
} }
return os.str(); return os.str();
@ -368,11 +374,13 @@ string BytesUtils::formatBytesRange(
parameters = ContractABIUtils::defaultParameters((_bytes.size() + 31) / 32); parameters = ContractABIUtils::defaultParameters((_bytes.size() + 31) / 32);
else else
parameters = _parameters; parameters = _parameters;
soltestAssert(ContractABIUtils::encodingSize(parameters) >= _bytes.size());
for (auto const& parameter: parameters) for (auto const& parameter: parameters)
{ {
bytes byteRange{it, it + static_cast<long>(parameter.abiType.size)}; long actualSize = min(distance(it, _bytes.end()), static_cast<long>(parameter.abiType.size));
bytes byteRange(parameter.abiType.size, 0);
copy(it, it + actualSize, byteRange.begin());
if (!parameter.matchesBytes(byteRange)) if (!parameter.matchesBytes(byteRange))
AnsiColorized( AnsiColorized(
@ -386,7 +394,7 @@ string BytesUtils::formatBytesRange(
if (&parameter != &parameters.back()) if (&parameter != &parameters.back())
os << ", "; os << ", ";
it += static_cast<long>(parameter.abiType.size); it += actualSize;
} }
return os.str(); return os.str();