aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/analysis/SyntaxChecker.cpp
diff options
context:
space:
mode:
authorBalajiganapathi S <balajiganapathi.s@gmail.com>2017-10-25 16:12:07 +0800
committerChristian Parpart <christian@ethereum.org>2018-08-08 18:05:51 +0800
commit09a36cba0223c16248335703412cee019c7aa59f (patch)
treeae444498ba2265538c54d4af63742b8bec32da43 /libsolidity/analysis/SyntaxChecker.cpp
parent0000bfc604b985c47ab153f4172596b860c7cce8 (diff)
downloaddexon-solidity-09a36cba0223c16248335703412cee019c7aa59f.tar
dexon-solidity-09a36cba0223c16248335703412cee019c7aa59f.tar.gz
dexon-solidity-09a36cba0223c16248335703412cee019c7aa59f.tar.bz2
dexon-solidity-09a36cba0223c16248335703412cee019c7aa59f.tar.lz
dexon-solidity-09a36cba0223c16248335703412cee019c7aa59f.tar.xz
dexon-solidity-09a36cba0223c16248335703412cee019c7aa59f.tar.zst
dexon-solidity-09a36cba0223c16248335703412cee019c7aa59f.zip
Add stricter hex underscore rules
Diffstat (limited to 'libsolidity/analysis/SyntaxChecker.cpp')
-rw-r--r--libsolidity/analysis/SyntaxChecker.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/libsolidity/analysis/SyntaxChecker.cpp b/libsolidity/analysis/SyntaxChecker.cpp
index 77492499..dc2e35e5 100644
--- a/libsolidity/analysis/SyntaxChecker.cpp
+++ b/libsolidity/analysis/SyntaxChecker.cpp
@@ -24,6 +24,9 @@
#include <libsolidity/interface/Version.h>
#include <boost/algorithm/cxx11/all_of.hpp>
+#include <boost/algorithm/string.hpp>
+#include <string>
+
using namespace std;
using namespace dev;
using namespace dev::solidity;
@@ -183,6 +186,46 @@ bool SyntaxChecker::visit(Throw const& _throwStatement)
return true;
}
+bool SyntaxChecker::visit(Literal const& _literal)
+{
+ if (!_literal.isHexNumber())
+ return true;
+ // We have a hex literal. Do underscore validation
+ solAssert(_literal.value().substr(0, 2) == "0x", "");
+ ASTString value = _literal.value().substr(2); // Skip the 0x
+ vector<ASTString> parts;
+ boost::split(parts, value, boost::is_any_of("_"));
+
+ if (parts.size() == 1) // no underscores
+ return true;
+ // Everything except first and last part must be 4 chars in length
+ for (size_t i = 1; i + 1 < parts.size(); ++i)
+ {
+ if (parts[i].size() != 4)
+ m_errorReporter.syntaxError(_literal.location(), "Invalid use of underscores in hex literal. Found inner part with " + to_string(parts[i].size()) + " digits (has to be 4 digits).");
+ }
+
+ // Validate rightmost block
+ if (parts.back().size() == 4) // If ends with 4 digits, then no need to validate first block
+ return true;
+
+ // Validate leftmost block
+ // If first part is 4 digits then last part's length has to be even to avoid ambiguity over zero padding
+ if (parts.front().size() == 4)
+ {
+ if (parts.back().size() % 2 == 0)
+ return true;
+ m_errorReporter.syntaxError(_literal.location(), "Invalid use of underscores in hex literal. If the first part has 4 digits, it is assumed to be a byte sequence instead of a number and thus the last part should have an even number of digits.");
+ }
+ else
+ {
+ // Both first and last part is invalid
+ m_errorReporter.syntaxError(_literal.location(), "Invalid use of underscores in hex literal. First or last part must have 4 digits.");
+ }
+
+ return true;
+}
+
bool SyntaxChecker::visit(UnaryOperation const& _operation)
{
if (_operation.getOperator() == Token::Add)