Merge pull request #1604 from ethereum/checksums

Warn about invalid checksums of addresses.
This commit is contained in:
Alex Beregszaszi
2017-01-26 14:42:34 +00:00
committed by GitHub
15 changed files with 294 additions and 18 deletions
+10
View File
@@ -1565,6 +1565,16 @@ void TypeChecker::endVisit(ElementaryTypeNameExpression const& _expr)
void TypeChecker::endVisit(Literal const& _literal)
{
if (_literal.looksLikeAddress())
{
if (_literal.passesAddressChecksum())
{
_literal.annotation().type = make_shared<IntegerType>(0, IntegerType::Modifier::Address);
return;
}
else
warning(_literal.location(), "This looks like an address but has an invalid checksum.");
}
_literal.annotation().type = Type::forLiteral(_literal);
if (!_literal.annotation().type)
fatalTypeError(_literal.location(), "Invalid literal value.");
+21 -2
View File
@@ -20,8 +20,6 @@
* Solidity abstract syntax tree.
*/
#include <algorithm>
#include <functional>
#include <libsolidity/interface/Utils.h>
#include <libsolidity/ast/AST.h>
#include <libsolidity/ast/ASTVisitor.h>
@@ -30,6 +28,11 @@
#include <libdevcore/SHA3.h>
#include <boost/algorithm/string.hpp>
#include <algorithm>
#include <functional>
using namespace std;
using namespace dev;
using namespace dev::solidity;
@@ -522,3 +525,19 @@ IdentifierAnnotation& Identifier::annotation() const
m_annotation = new IdentifierAnnotation();
return static_cast<IdentifierAnnotation&>(*m_annotation);
}
bool Literal::looksLikeAddress() const
{
if (subDenomination() != SubDenomination::None)
return false;
string lit = value();
return lit.substr(0, 2) == "0x" && abs(int(lit.length()) - 42) <= 1;
}
bool Literal::passesAddressChecksum() const
{
string lit = value();
solAssert(lit.substr(0, 2) == "0x", "Expected hex prefix");
return dev::passesAddressChecksum(lit, true);
}
+5
View File
@@ -1584,6 +1584,11 @@ public:
SubDenomination subDenomination() const { return m_subDenomination; }
/// @returns true if this looks like a checksummed address.
bool looksLikeAddress() const;
/// @returns true if it passes the address checksum test.
bool passesAddressChecksum() const;
private:
Token::Value m_token;
ASTPointer<ASTString> m_value;
+8
View File
@@ -405,6 +405,14 @@ string IntegerType::toString(bool) const
return prefix + dev::toString(m_bits);
}
u256 IntegerType::literalValue(Literal const* _literal) const
{
solAssert(m_modifier == Modifier::Address, "");
solAssert(_literal, "");
solAssert(_literal->value().substr(0, 2) == "0x", "");
return u256(_literal->value());
}
TypePointer IntegerType::binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const
{
if (
+2
View File
@@ -314,6 +314,8 @@ public:
virtual std::string toString(bool _short) const override;
virtual u256 literalValue(Literal const* _literal) const override;
virtual TypePointer encodingType() const override { return shared_from_this(); }
virtual TypePointer interfaceType(bool) const override { return shared_from_this(); }
@@ -1308,6 +1308,7 @@ void ExpressionCompiler::endVisit(Literal const& _literal)
{
case Type::Category::RationalNumber:
case Type::Category::Bool:
case Type::Category::Integer:
m_context << type->literalValue(&_literal);
break;
case Type::Category::StringLiteral: