From 64f57ac3c7a46d8f442bfc89ed352c256bef25cf Mon Sep 17 00:00:00 2001 From: Matheus Aguiar Date: Mon, 17 Apr 2023 16:10:06 -0300 Subject: [PATCH] Fix null dereference in using for directive when operator name is empty --- libsolidity/parsing/Parser.cpp | 8 +++++++- .../operator_parsing_operator_name_empty_string.sol | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/libsolidity/syntaxTests/operators/userDefined/operator_parsing_operator_name_empty_string.sol diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index b146790e0..8b0f00e7b 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -995,11 +995,17 @@ ASTPointer Parser::parseUsingDirective() Token operator_ = m_scanner->currentToken(); if (!util::contains(userDefinableOperators, operator_)) { + string operatorName; + if (!m_scanner->currentLiteral().empty()) + operatorName = m_scanner->currentLiteral(); + else if (char const* tokenString = TokenTraits::toString(operator_)) + operatorName = string(tokenString); + parserError( 4403_error, fmt::format( "Not a user-definable operator: {}. Only the following operators can be user-defined: {}", - (!m_scanner->currentLiteral().empty() ? m_scanner->currentLiteral() : string(TokenTraits::toString(operator_))), + operatorName, util::joinHumanReadable(userDefinableOperators | ranges::views::transform([](Token _t) { return string{TokenTraits::toString(_t)}; })) ) ); diff --git a/test/libsolidity/syntaxTests/operators/userDefined/operator_parsing_operator_name_empty_string.sol b/test/libsolidity/syntaxTests/operators/userDefined/operator_parsing_operator_name_empty_string.sol new file mode 100644 index 000000000..8435de6d9 --- /dev/null +++ b/test/libsolidity/syntaxTests/operators/userDefined/operator_parsing_operator_name_empty_string.sol @@ -0,0 +1,7 @@ +using {f as ''} for uint; +using {f as ""} for int; +using {f as hex""} for address; +// ---- +// ParserError 4403: (12-14): Not a user-definable operator: . Only the following operators can be user-defined: |, &, ^, ~, +, -, *, /, %, ==, !=, <, >, <=, >= +// ParserError 4403: (38-40): Not a user-definable operator: . Only the following operators can be user-defined: |, &, ^, ~, +, -, *, /, %, ==, !=, <, >, <=, >= +// ParserError 4403: (63-68): Not a user-definable operator: . Only the following operators can be user-defined: |, &, ^, ~, +, -, *, /, %, ==, !=, <, >, <=, >=