From 90fdea95e7a0cf5a8c29e054afb02018cf7a257d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Mon, 15 Nov 2021 19:58:59 +0100 Subject: [PATCH] TestFunctionCall::formatRawParameters(): Ensure that uint8_t overload of toHex() is called - Wrong overload results in isoltest padding each char to 32 bytes --- .../format_raw_string_with_control_chars.sol | 12 ++++++++++++ test/libsolidity/util/BytesUtils.cpp | 2 +- test/libsolidity/util/TestFunctionCall.cpp | 4 +++- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 test/libsolidity/semanticTests/isoltestTesting/format_raw_string_with_control_chars.sol diff --git a/test/libsolidity/semanticTests/isoltestTesting/format_raw_string_with_control_chars.sol b/test/libsolidity/semanticTests/isoltestTesting/format_raw_string_with_control_chars.sol new file mode 100644 index 000000000000..205a4ae3dd96 --- /dev/null +++ b/test/libsolidity/semanticTests/isoltestTesting/format_raw_string_with_control_chars.sol @@ -0,0 +1,12 @@ +contract C { + function f(string memory s) external pure returns (string memory) { + return s; + } +} +// NOTE: The test is here to illustrate the problem with formatting control chars in strings in +// test expectations but unfortunately it can only be triggered manually. It does not test anything +// unless you introduce a difference in expectations to force isoltest to reformat them. +// ==== +// compileViaYul: also +// ---- +// f(string): 0x20, 16, "\xf0\x9f\x98\x83\xf0\x9f\x98\x83\xf0\x9f\x98\x83\xf0\x9f\x98\x83" -> 0x20, 16, "\xf0\x9f\x98\x83\xf0\x9f\x98\x83\xf0\x9f\x98\x83\xf0\x9f\x98\x83" # Input/Output: "😃😃😃😃" # diff --git a/test/libsolidity/util/BytesUtils.cpp b/test/libsolidity/util/BytesUtils.cpp index 6745bd1dfc5f..f74b256d0c61 100644 --- a/test/libsolidity/util/BytesUtils.cpp +++ b/test/libsolidity/util/BytesUtils.cpp @@ -219,7 +219,7 @@ string BytesUtils::formatString(bytes const& _bytes, size_t _cutOff) if (isprint(v)) os << v; else - os << "\\x" << toHex(v); + os << "\\x" << toHex(v, HexCase::Lower); } } os << "\""; diff --git a/test/libsolidity/util/TestFunctionCall.cpp b/test/libsolidity/util/TestFunctionCall.cpp index 0f28f3b99b42..43805c8f9934 100644 --- a/test/libsolidity/util/TestFunctionCall.cpp +++ b/test/libsolidity/util/TestFunctionCall.cpp @@ -322,7 +322,9 @@ string TestFunctionCall::formatRawParameters( if (param.format.newline) os << endl << _linePrefix << "// "; for (auto const c: param.rawString) - os << (c >= ' ' ? string(1, c) : "\\x" + toHex(static_cast(c))); + // NOTE: Even though we have a toHex() overload specifically for uint8_t, the compiler + // chooses the one for bytes if the second argument is omitted. + os << (c >= ' ' ? string(1, c) : "\\x" + toHex(static_cast(c), HexCase::Lower)); if (¶m != &_params.back()) os << ", "; }