aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2018-10-02 22:37:24 +0800
committerGitHub <noreply@github.com>2018-10-02 22:37:24 +0800
commit23773bbb1f6b5ef5cec36a8d7f8fdc00be50186e (patch)
tree8e8787835a7a683e45cde3a975a95a330439c7ac
parent7609710d4f8e169784c7a8418da702ac236f5e23 (diff)
parent9fb835b710f7f0dc9f2089df6301f0ce55d1e4aa (diff)
downloaddexon-solidity-23773bbb1f6b5ef5cec36a8d7f8fdc00be50186e.tar
dexon-solidity-23773bbb1f6b5ef5cec36a8d7f8fdc00be50186e.tar.gz
dexon-solidity-23773bbb1f6b5ef5cec36a8d7f8fdc00be50186e.tar.bz2
dexon-solidity-23773bbb1f6b5ef5cec36a8d7f8fdc00be50186e.tar.lz
dexon-solidity-23773bbb1f6b5ef5cec36a8d7f8fdc00be50186e.tar.xz
dexon-solidity-23773bbb1f6b5ef5cec36a8d7f8fdc00be50186e.tar.zst
dexon-solidity-23773bbb1f6b5ef5cec36a8d7f8fdc00be50186e.zip
Merge pull request #5125 from ethereum/fix_address_with_underscores
Fixes #5051 (introduced in #4684), effectively allowing underscores in address literals.
-rw-r--r--libsolidity/analysis/TypeChecker.cpp4
-rw-r--r--libsolidity/ast/AST.cpp11
-rw-r--r--libsolidity/ast/AST.h2
-rw-r--r--libsolidity/ast/Types.cpp2
-rw-r--r--test/libsolidity/syntaxTests/types/address/literal_to_address.sol1
5 files changed, 14 insertions, 6 deletions
diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp
index 3d119c82..bc040623 100644
--- a/libsolidity/analysis/TypeChecker.cpp
+++ b/libsolidity/analysis/TypeChecker.cpp
@@ -2398,11 +2398,11 @@ void TypeChecker::endVisit(Literal const& _literal)
_literal.annotation().type = make_shared<AddressType>(StateMutability::Payable);
string msg;
- if (_literal.value().length() != 42) // "0x" + 40 hex digits
+ if (_literal.valueWithoutUnderscores().length() != 42) // "0x" + 40 hex digits
// looksLikeAddress enforces that it is a hex literal starting with "0x"
msg =
"This looks like an address but is not exactly 40 hex digits. It is " +
- to_string(_literal.value().length() - 2) +
+ to_string(_literal.valueWithoutUnderscores().length() - 2) +
" hex digits.";
else if (!_literal.passesAddressChecksum())
{
diff --git a/libsolidity/ast/AST.cpp b/libsolidity/ast/AST.cpp
index a11b1146..d9264230 100644
--- a/libsolidity/ast/AST.cpp
+++ b/libsolidity/ast/AST.cpp
@@ -633,6 +633,11 @@ IdentifierAnnotation& Identifier::annotation() const
return dynamic_cast<IdentifierAnnotation&>(*m_annotation);
}
+ASTString Literal::valueWithoutUnderscores() const
+{
+ return boost::erase_all_copy(value(), "_");
+}
+
bool Literal::isHexNumber() const
{
if (token() != Token::Number)
@@ -648,20 +653,20 @@ bool Literal::looksLikeAddress() const
if (!isHexNumber())
return false;
- return abs(int(value().length()) - 42) <= 1;
+ return abs(int(valueWithoutUnderscores().length()) - 42) <= 1;
}
bool Literal::passesAddressChecksum() const
{
solAssert(isHexNumber(), "Expected hex number");
- return dev::passesAddressChecksum(value(), true);
+ return dev::passesAddressChecksum(valueWithoutUnderscores(), true);
}
string Literal::getChecksummedAddress() const
{
solAssert(isHexNumber(), "Expected hex number");
/// Pad literal to be a proper hex address.
- string address = value().substr(2);
+ string address = valueWithoutUnderscores().substr(2);
if (address.length() > 40)
return string();
address.insert(address.begin(), 40 - address.size(), '0');
diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h
index f3464f92..b84f9730 100644
--- a/libsolidity/ast/AST.h
+++ b/libsolidity/ast/AST.h
@@ -1679,6 +1679,8 @@ public:
/// @returns the non-parsed value of the literal
ASTString const& value() const { return *m_value; }
+ ASTString valueWithoutUnderscores() const;
+
SubDenomination subDenomination() const { return m_subDenomination; }
/// @returns true if this is a number with a hex prefix.
diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp
index fd72bf41..e45fc81d 100644
--- a/libsolidity/ast/Types.cpp
+++ b/libsolidity/ast/Types.cpp
@@ -499,7 +499,7 @@ u256 AddressType::literalValue(Literal const* _literal) const
{
solAssert(_literal, "");
solAssert(_literal->value().substr(0, 2) == "0x", "");
- return u256(_literal->value());
+ return u256(_literal->valueWithoutUnderscores());
}
TypePointer AddressType::unaryOperatorResult(Token::Value _operator) const
diff --git a/test/libsolidity/syntaxTests/types/address/literal_to_address.sol b/test/libsolidity/syntaxTests/types/address/literal_to_address.sol
index 9d599ea5..20ee56de 100644
--- a/test/libsolidity/syntaxTests/types/address/literal_to_address.sol
+++ b/test/libsolidity/syntaxTests/types/address/literal_to_address.sol
@@ -4,5 +4,6 @@ contract C {
a = address(1);
address b = 0x0123456789012345678901234567890123456789;
b = 0x9876543210987654321098765432109876543210;
+ b = 0x9876_5432_1098_7654_3210_9876_5432_1098_7654_3210;
}
}