From e136ec87041cce9d8d472e45f4dfbed9a8d02469 Mon Sep 17 00:00:00 2001 From: Yoichi Hirai Date: Fri, 25 Nov 2016 13:36:06 +0100 Subject: [PATCH] ast: string literals that are not valid UTF are not convertible to strings --- libdevcore/UTF8.h | 2 +- libsolidity/ast/Types.cpp | 9 ++++++++- libsolidity/ast/Types.h | 2 ++ test/libsolidity/SolidityNameAndTypeResolution.cpp | 2 +- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/libdevcore/UTF8.h b/libdevcore/UTF8.h index 3e39273cd..9bdc2b4f2 100644 --- a/libdevcore/UTF8.h +++ b/libdevcore/UTF8.h @@ -29,7 +29,7 @@ namespace dev { /// Validate an input for UTF8 encoding -/// @returns true if it is invalid and the first invalid position in invalidPosition +/// @returns false if it is invalid and the first invalid position in invalidPosition bool validate(std::string const& _input, size_t& _invalidPosition); } diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index b7de3646c..002ad657b 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -879,7 +879,8 @@ bool StringLiteralType::isImplicitlyConvertibleTo(Type const& _convertTo) const else if (auto arrayType = dynamic_cast(&_convertTo)) return arrayType->isByteArray() && - !(arrayType->dataStoredIn(DataLocation::Storage) && arrayType->isPointer()); + !(arrayType->dataStoredIn(DataLocation::Storage) && arrayType->isPointer()) && + !(arrayType->isString() && !isValidUTF8()); else return false; } @@ -906,6 +907,12 @@ TypePointer StringLiteralType::mobileType() const return make_shared(DataLocation::Memory, true); } +bool StringLiteralType::isValidUTF8() const +{ + size_t dontCare {}; + return dev::validate(m_value, dontCare); +} + shared_ptr FixedBytesType::smallestTypeForLiteral(string const& _literal) { if (_literal.length() <= 32) diff --git a/libsolidity/ast/Types.h b/libsolidity/ast/Types.h index b713a7c01..72640a1c4 100644 --- a/libsolidity/ast/Types.h +++ b/libsolidity/ast/Types.h @@ -425,6 +425,8 @@ public: virtual std::string toString(bool) const override; virtual TypePointer mobileType() const override; + bool isValidUTF8() const; + std::string const& value() const { return m_value; } private: diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 6181a8a95..cc4da61b0 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2045,7 +2045,7 @@ BOOST_AUTO_TEST_CASE(invalid_utf8) string s = "\xa0\x00"; } )"; - CHECK_ERROR(sourceCode, TypeError, "Invalid UTF-8"); + CHECK_ERROR(sourceCode, TypeError, "invalid UTF-8"); } BOOST_AUTO_TEST_CASE(string_index)