From 368ea01bb25f835d9221d2b574535c2883f928bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Thu, 17 Aug 2023 18:35:20 +0200 Subject: [PATCH] BytesUtils: Don't introduce garbage when formatting byte vectors shorter than expected --- test/libsolidity/util/BytesUtils.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/test/libsolidity/util/BytesUtils.cpp b/test/libsolidity/util/BytesUtils.cpp index f2a5db7af..47cdc1783 100644 --- a/test/libsolidity/util/BytesUtils.cpp +++ b/test/libsolidity/util/BytesUtils.cpp @@ -26,6 +26,8 @@ #include +#include +#include #include #include #include @@ -253,7 +255,8 @@ std::string BytesUtils::formatFixedPoint(bytes const& _bytes, bool _signed, size string BytesUtils::formatRawBytes( bytes const& _bytes, solidity::frontend::test::ParameterList const& _parameters, - string _linePrefix) + string _linePrefix +) { stringstream os; ParameterList parameters; @@ -263,16 +266,19 @@ string BytesUtils::formatRawBytes( parameters = ContractABIUtils::defaultParameters((_bytes.size() + 31) / 32); else parameters = _parameters; + soltestAssert(ContractABIUtils::encodingSize(parameters) >= _bytes.size()); for (auto const& parameter: parameters) { - bytes byteRange{it, it + static_cast(parameter.abiType.size)}; + long actualSize = min(distance(it, _bytes.end()), static_cast(parameter.abiType.size)); + bytes byteRange(parameter.abiType.size, 0); + copy(it, it + actualSize, byteRange.begin()); os << _linePrefix << byteRange; if (¶meter != ¶meters.back()) os << endl; - it += static_cast(parameter.abiType.size); + it += actualSize; } return os.str(); @@ -368,11 +374,13 @@ string BytesUtils::formatBytesRange( parameters = ContractABIUtils::defaultParameters((_bytes.size() + 31) / 32); else parameters = _parameters; - + soltestAssert(ContractABIUtils::encodingSize(parameters) >= _bytes.size()); for (auto const& parameter: parameters) { - bytes byteRange{it, it + static_cast(parameter.abiType.size)}; + long actualSize = min(distance(it, _bytes.end()), static_cast(parameter.abiType.size)); + bytes byteRange(parameter.abiType.size, 0); + copy(it, it + actualSize, byteRange.begin()); if (!parameter.matchesBytes(byteRange)) AnsiColorized( @@ -386,7 +394,7 @@ string BytesUtils::formatBytesRange( if (¶meter != ¶meters.back()) os << ", "; - it += static_cast(parameter.abiType.size); + it += actualSize; } return os.str();