aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2017-08-21 20:05:13 +0800
committerGitHub <noreply@github.com>2017-08-21 20:05:13 +0800
commit0cf6048800474fd4f69c3c94d9954a08c1709f76 (patch)
tree96fbdef7b3bf029d8f72ae9df53268375a952ca2
parent83b90f3e8a3ee168ba00eff33c4443d00bbb8a57 (diff)
parent2c5985de06b4ad35eaa77f3869e280440100c9cd (diff)
downloaddexon-solidity-0cf6048800474fd4f69c3c94d9954a08c1709f76.tar.gz
dexon-solidity-0cf6048800474fd4f69c3c94d9954a08c1709f76.tar.zst
dexon-solidity-0cf6048800474fd4f69c3c94d9954a08c1709f76.zip
Merge pull request #2769 from ethereum/assemblyNumberLiterals
Be more strict about number literals in assembly.
-rw-r--r--Changelog.md1
-rw-r--r--libsolidity/inlineasm/AsmParser.cpp21
-rw-r--r--libsolidity/inlineasm/AsmParser.h2
-rw-r--r--test/libjulia/Parser.cpp8
-rw-r--r--test/libsolidity/InlineAssembly.cpp8
5 files changed, 40 insertions, 0 deletions
diff --git a/Changelog.md b/Changelog.md
index 7d026862..a587d3ad 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -12,6 +12,7 @@ Features:
Bugfixes:
* Parser: Enforce commas between array and tuple elements.
* Parser: Limit maximum recursion depth.
+ * Assembly Parser: Be more strict about number literals.
### 0.4.15 (2017-08-08)
diff --git a/libsolidity/inlineasm/AsmParser.cpp b/libsolidity/inlineasm/AsmParser.cpp
index 133f70b1..1dcc42b8 100644
--- a/libsolidity/inlineasm/AsmParser.cpp
+++ b/libsolidity/inlineasm/AsmParser.cpp
@@ -23,6 +23,9 @@
#include <libsolidity/inlineasm/AsmParser.h>
#include <libsolidity/parsing/Scanner.h>
#include <libsolidity/interface/ErrorReporter.h>
+
+#include <boost/algorithm/string.hpp>
+
#include <ctype.h>
#include <algorithm>
@@ -297,6 +300,8 @@ assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher)
kind = LiteralKind::String;
break;
case Token::Number:
+ if (!isValidNumberLiteral(currentLiteral()))
+ fatalParserError("Invalid number literal.");
kind = LiteralKind::Number;
break;
case Token::TrueLiteral:
@@ -501,3 +506,19 @@ string Parser::expectAsmIdentifier()
expectToken(Token::Identifier);
return name;
}
+
+bool Parser::isValidNumberLiteral(string const& _literal)
+{
+ try
+ {
+ u256(_literal);
+ }
+ catch (...)
+ {
+ return false;
+ }
+ if (boost::starts_with(_literal, "0x"))
+ return true;
+ else
+ return _literal.find_first_not_of("0123456789") == string::npos;
+}
diff --git a/libsolidity/inlineasm/AsmParser.h b/libsolidity/inlineasm/AsmParser.h
index 45708afd..a48a3393 100644
--- a/libsolidity/inlineasm/AsmParser.h
+++ b/libsolidity/inlineasm/AsmParser.h
@@ -75,6 +75,8 @@ protected:
TypedName parseTypedName();
std::string expectAsmIdentifier();
+ static bool isValidNumberLiteral(std::string const& _literal);
+
private:
bool m_julia = false;
};
diff --git a/test/libjulia/Parser.cpp b/test/libjulia/Parser.cpp
index dd6f3d94..e1bf5a3a 100644
--- a/test/libjulia/Parser.cpp
+++ b/test/libjulia/Parser.cpp
@@ -214,6 +214,14 @@ BOOST_AUTO_TEST_CASE(invalid_types)
CHECK_ERROR("{ function f(a:invalid) {} }", TypeError, "\"invalid\" is not a valid type (user defined types are not yet supported).");
}
+BOOST_AUTO_TEST_CASE(number_literals)
+{
+ BOOST_CHECK(successParse("{ let x:u256 := 1:u256 }"));
+ CHECK_ERROR("{ let x:u256 := .1:u256 }", ParserError, "Invalid number literal.");
+ CHECK_ERROR("{ let x:u256 := 1e5:u256 }", ParserError, "Invalid number literal.");
+ CHECK_ERROR("{ let x:u256 := 67.235:u256 }", ParserError, "Invalid number literal.");
+}
+
BOOST_AUTO_TEST_CASE(builtin_types)
{
BOOST_CHECK(successParse("{ let x:bool := true:bool }"));
diff --git a/test/libsolidity/InlineAssembly.cpp b/test/libsolidity/InlineAssembly.cpp
index 4bf4eb48..8e1c304a 100644
--- a/test/libsolidity/InlineAssembly.cpp
+++ b/test/libsolidity/InlineAssembly.cpp
@@ -339,6 +339,14 @@ BOOST_AUTO_TEST_CASE(blocks)
BOOST_CHECK(successParse("{ let x := 7 { let y := 3 } { let z := 2 } }"));
}
+BOOST_AUTO_TEST_CASE(number_literals)
+{
+ BOOST_CHECK(successParse("{ let x := 1 }"));
+ CHECK_PARSE_ERROR("{ let x := .1 }", ParserError, "Invalid number literal.");
+ CHECK_PARSE_ERROR("{ let x := 1e5 }", ParserError, "Invalid number literal.");
+ CHECK_PARSE_ERROR("{ let x := 67.235 }", ParserError, "Invalid number literal.");
+}
+
BOOST_AUTO_TEST_CASE(function_definitions)
{
BOOST_CHECK(successParse("{ function f() { } function g(a) -> x { } }"));