Merge pull request #6542 from hydai/yul/string_literal_too_long

[Yul] Output an error of a switch case which contains string literals…
This commit is contained in:
chriseth 2019-04-18 12:13:28 +02:00 committed by GitHub
commit 27d0481958
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -442,15 +442,21 @@ bool AsmAnalyzer::operator()(Switch const& _switch)
if (_case.value) if (_case.value)
{ {
int const initialStackHeight = m_stackHeight; int const initialStackHeight = m_stackHeight;
bool isCaseValueValid = true;
// We cannot use "expectExpression" here because *_case.value is not a // We cannot use "expectExpression" here because *_case.value is not a
// Statement and would be converted to a Statement otherwise. // Statement and would be converted to a Statement otherwise.
if (!(*this)(*_case.value)) if (!(*this)(*_case.value))
{
isCaseValueValid = false;
success = false; success = false;
}
expectDeposit(1, initialStackHeight, _case.value->location); expectDeposit(1, initialStackHeight, _case.value->location);
m_stackHeight--; m_stackHeight--;
// If the case value is not valid, we should not insert it into cases.
yulAssert(isCaseValueValid || m_errorReporter.hasErrors(), "Invalid case value.");
/// Note: the parser ensures there is only one default case /// Note: the parser ensures there is only one default case
if (!cases.insert(valueOfLiteral(*_case.value)).second) if (isCaseValueValid && !cases.insert(valueOfLiteral(*_case.value)).second)
{ {
m_errorReporter.declarationError( m_errorReporter.declarationError(
_case.location, _case.location,

View File

@ -390,6 +390,12 @@ BOOST_AUTO_TEST_CASE(switch_duplicate_case_different_literal)
BOOST_CHECK(successParse("{ switch 1:u256 case \"1\":u256 {} case \"2\":u256 {} }")); BOOST_CHECK(successParse("{ switch 1:u256 case \"1\":u256 {} case \"2\":u256 {} }"));
} }
BOOST_AUTO_TEST_CASE(switch_case_string_literal_too_long)
{
BOOST_CHECK(successParse("{let x:u256 switch x case \"01234567890123456789012345678901\":u256 {}}"));
CHECK_ERROR("{let x:u256 switch x case \"012345678901234567890123456789012\":u256 {}}", TypeError, "String literal too long (33 > 32)");
}
BOOST_AUTO_TEST_CASE(function_shadowing_outside_vars) BOOST_AUTO_TEST_CASE(function_shadowing_outside_vars)
{ {
CHECK_ERROR("{ let x:u256 function f() -> x:u256 {} }", DeclarationError, "already taken in this scope"); CHECK_ERROR("{ let x:u256 function f() -> x:u256 {} }", DeclarationError, "already taken in this scope");