From 894122c508c4de07553c7e2908ae36821e812a9f Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 7 May 2018 22:34:31 +0200 Subject: Warn/enforce single bytes argument for certain builtins (hashing functions). In 0.5.0 mode, only accept a single bytes argument for ``.call``, ``keccak256`` and others and do not pad when encoding. --- Changelog.md | 1 + libsolidity/analysis/TypeChecker.cpp | 27 +++++++++++++++++++++++++++ libsolidity/ast/Types.h | 16 ++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/Changelog.md b/Changelog.md index 72c27f8e..ba7f4544 100644 --- a/Changelog.md +++ b/Changelog.md @@ -15,6 +15,7 @@ Features: * Type Checker: Deprecate the ``years`` unit denomination and raise a warning for it (or an error as experimental 0.5.0 feature). * Type Checker: Make literals (without explicit type casting) an error for tight packing as experimental 0.5.0 feature. * Type Checker: Warn about wildcard tuple assignments (this will turn into an error with version 0.5.0). + * Type Checker: Warn when ``keccak256``, ``sha256`` and ``ripemd160`` are not used with a single bytes argument (suggest to use ``abi.encodePacked(...)``). This will turn into an error with version 0.5.0. Bugfixes: * Code Generator: Fix ``revert`` with reason coming from a state or local string variable. diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index e8694e88..f77cc60c 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -1760,6 +1760,33 @@ bool TypeChecker::visit(FunctionCall const& _functionCall) } } + if (functionType->takesSinglePackedBytesParameter()) + { + string generalMessage = + "This function only accepts a single \"bytes\" argument. Please use " + "\"abi.encodePacked(...)\" or a similar function to encode the data."; + + if (arguments.size() > 1) + { + if (v050) + m_errorReporter.typeError(_functionCall.location(), generalMessage); + else + m_errorReporter.warning(_functionCall.location(), generalMessage); + } + else if (arguments.size() == 1 && !type(*arguments.front())->isImplicitlyConvertibleTo(ArrayType(DataLocation::Memory))) + { + string msg = + generalMessage + + " The provided argument of type " + + type(*arguments.front())->toString() + + " is not implicitly convertible to expected type bytes memory."; + if (v050) + m_errorReporter.typeError(_functionCall.location(), msg); + else + m_errorReporter.warning(_functionCall.location(), msg); + } + } + if (functionType->takesArbitraryParameters() && arguments.size() < parameterTypes.size()) { solAssert(_functionCall.annotation().kind == FunctionCallKind::FunctionCall, ""); diff --git a/libsolidity/ast/Types.h b/libsolidity/ast/Types.h index 4884696d..95821634 100644 --- a/libsolidity/ast/Types.h +++ b/libsolidity/ast/Types.h @@ -1058,6 +1058,22 @@ public: /// true iff arguments are to be padded to multiples of 32 bytes for external calls bool padArguments() const { return !(m_kind == Kind::SHA3 || m_kind == Kind::SHA256 || m_kind == Kind::RIPEMD160 || m_kind == Kind::ABIEncodePacked); } bool takesArbitraryParameters() const { return m_arbitraryParameters; } + /// true iff the function takes a single bytes parameter and it is passed on without padding. + /// @todo until 0.5.0, this is just a "recommendation". + bool takesSinglePackedBytesParameter() const + { + // @todo add the call kinds here with 0.5.0 and perhaps also log0. + switch (m_kind) + { + case FunctionType::Kind::SHA3: + case FunctionType::Kind::SHA256: + case FunctionType::Kind::RIPEMD160: + return true; + default: + return false; + } + } + bool gasSet() const { return m_gasSet; } bool valueSet() const { return m_valueSet; } bool bound() const { return m_bound; } -- cgit