ast: string literals that are not valid UTF are not convertible to strings

This commit is contained in:
Yoichi Hirai 2016-11-25 13:36:06 +01:00
parent aaf58a8c4e
commit e136ec8704
No known key found for this signature in database
GPG Key ID: E7B75D080FCF7992
4 changed files with 12 additions and 3 deletions

View File

@ -29,7 +29,7 @@ namespace dev
{ {
/// Validate an input for UTF8 encoding /// 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); bool validate(std::string const& _input, size_t& _invalidPosition);
} }

View File

@ -879,7 +879,8 @@ bool StringLiteralType::isImplicitlyConvertibleTo(Type const& _convertTo) const
else if (auto arrayType = dynamic_cast<ArrayType const*>(&_convertTo)) else if (auto arrayType = dynamic_cast<ArrayType const*>(&_convertTo))
return return
arrayType->isByteArray() && arrayType->isByteArray() &&
!(arrayType->dataStoredIn(DataLocation::Storage) && arrayType->isPointer()); !(arrayType->dataStoredIn(DataLocation::Storage) && arrayType->isPointer()) &&
!(arrayType->isString() && !isValidUTF8());
else else
return false; return false;
} }
@ -906,6 +907,12 @@ TypePointer StringLiteralType::mobileType() const
return make_shared<ArrayType>(DataLocation::Memory, true); return make_shared<ArrayType>(DataLocation::Memory, true);
} }
bool StringLiteralType::isValidUTF8() const
{
size_t dontCare {};
return dev::validate(m_value, dontCare);
}
shared_ptr<FixedBytesType> FixedBytesType::smallestTypeForLiteral(string const& _literal) shared_ptr<FixedBytesType> FixedBytesType::smallestTypeForLiteral(string const& _literal)
{ {
if (_literal.length() <= 32) if (_literal.length() <= 32)

View File

@ -425,6 +425,8 @@ public:
virtual std::string toString(bool) const override; virtual std::string toString(bool) const override;
virtual TypePointer mobileType() const override; virtual TypePointer mobileType() const override;
bool isValidUTF8() const;
std::string const& value() const { return m_value; } std::string const& value() const { return m_value; }
private: private:

View File

@ -2045,7 +2045,7 @@ BOOST_AUTO_TEST_CASE(invalid_utf8)
string s = "\xa0\x00"; string s = "\xa0\x00";
} }
)"; )";
CHECK_ERROR(sourceCode, TypeError, "Invalid UTF-8"); CHECK_ERROR(sourceCode, TypeError, "invalid UTF-8");
} }
BOOST_AUTO_TEST_CASE(string_index) BOOST_AUTO_TEST_CASE(string_index)