Skip to content

Commit

Permalink
ast: string literals that are not valid UTF are not convertible to st…
Browse files Browse the repository at this point in the history
…rings
  • Loading branch information
pirapira committed Nov 25, 2016
1 parent aaf58a8 commit e136ec8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion libdevcore/UTF8.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
9 changes: 8 additions & 1 deletion libsolidity/ast/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,8 @@ bool StringLiteralType::isImplicitlyConvertibleTo(Type const& _convertTo) const
else if (auto arrayType = dynamic_cast<ArrayType const*>(&_convertTo))
return
arrayType->isByteArray() &&
!(arrayType->dataStoredIn(DataLocation::Storage) && arrayType->isPointer());
!(arrayType->dataStoredIn(DataLocation::Storage) && arrayType->isPointer()) &&
!(arrayType->isString() && !isValidUTF8());
else
return false;
}
Expand All @@ -906,6 +907,12 @@ TypePointer StringLiteralType::mobileType() const
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)
{
if (_literal.length() <= 32)
Expand Down
2 changes: 2 additions & 0 deletions libsolidity/ast/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ class StringLiteralType: public Type
virtual std::string toString(bool) const override;
virtual TypePointer mobileType() const override;

bool isValidUTF8() const;

std::string const& value() const { return m_value; }

private:
Expand Down
2 changes: 1 addition & 1 deletion test/libsolidity/SolidityNameAndTypeResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e136ec8

Please sign in to comment.