Detect duplicate cases based on integer value of case label.

This commit is contained in:
chriseth 2019-03-25 12:30:05 +01:00
parent b17daee20e
commit caddce6ef0
3 changed files with 9 additions and 2 deletions

View File

@ -19,6 +19,7 @@ Bugfixes:
* Code Generator: Defensively pad memory for ``type(Contract).name`` to multiples of 32.
* Type System: Detect and disallow internal function pointers as parameters for public/external library functions, even when they are nested/wrapped in structs, arrays or other types.
* Yul Optimizer: Properly determine whether a variable can be eliminated during stack compression pass.
* Yul / Inline Assembly Parser: Disallow more than one case statement with the same label inside a switch based on the label's integer value.
Build System:

View File

@ -436,7 +436,7 @@ bool AsmAnalyzer::operator()(Switch const& _switch)
);
}
set<Literal const*, Less<Literal*>> cases;
set<u256> cases;
for (auto const& _case: _switch.cases)
{
if (_case.value)
@ -450,7 +450,7 @@ bool AsmAnalyzer::operator()(Switch const& _switch)
m_stackHeight--;
/// Note: the parser ensures there is only one default case
if (!cases.insert(_case.value.get()).second)
if (!cases.insert(valueOfLiteral(*_case.value)).second)
{
m_errorReporter.declarationError(
_case.location,

View File

@ -384,6 +384,12 @@ BOOST_AUTO_TEST_CASE(switch_duplicate_case)
BOOST_CHECK(successParse("{ switch 0:u256 case 42:u256 {} case 0x42:u256 {} }"));
}
BOOST_AUTO_TEST_CASE(switch_duplicate_case_different_literal)
{
CHECK_ERROR("{ switch 0:u256 case 0:u256 {} case \"\":u256 {} }", DeclarationError, "Duplicate case defined.");
BOOST_CHECK(successParse("{ switch 1:u256 case \"1\":u256 {} case \"2\":u256 {} }"));
}
BOOST_AUTO_TEST_CASE(builtins_parser)
{
struct SimpleDialect: public Dialect