From 5a75581f6643a229ab12a861d0b0663ea3e5158e Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 23 Jun 2017 18:55:47 +0200 Subject: Disallow invoking the same modifier multiple times. --- Changelog.md | 1 + libsolidity/analysis/TypeChecker.cpp | 13 +++++++++++++ libsolidity/codegen/CompilerContext.cpp | 1 + libsolidity/codegen/ContractCompiler.cpp | 4 ++++ test/libsolidity/SolidityNameAndTypeResolution.cpp | 22 ++++++++++++++++++++++ 5 files changed, 41 insertions(+) diff --git a/Changelog.md b/Changelog.md index ee952a37..6f2c50f2 100644 --- a/Changelog.md +++ b/Changelog.md @@ -15,6 +15,7 @@ Features: Bugfixes: * Type Checker: Fix address literals not being treated as compile-time constants. + * Type Checker: Disallow invoking the same modifier multiple times. * Type Checker: Make UTF8-validation a bit more sloppy to include more valid sequences. * Fixed crash concerning non-callable types. * Unused variable warnings no longer issued for variables used inside inline assembly. diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 40853608..e1d90e68 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -466,13 +466,26 @@ bool TypeChecker::visit(FunctionDefinition const& _function) var->accept(*this); } + set modifiers; for (ASTPointer const& modifier: _function.modifiers()) + { visitManually( *modifier, _function.isConstructor() ? dynamic_cast(*_function.scope()).annotation().linearizedBaseContracts : vector() ); + Declaration const* decl = &dereference(*modifier->name()); + if (modifiers.count(decl)) + { + if (dynamic_cast(decl)) + m_errorReporter.declarationError(modifier->location(), "Base constructor already provided."); + else + m_errorReporter.declarationError(modifier->location(), "Modifier already used for this function."); + } + else + modifiers.insert(decl); + } if (m_scope->contractKind() == ContractDefinition::ContractKind::Interface) { if (_function.isImplemented()) diff --git a/libsolidity/codegen/CompilerContext.cpp b/libsolidity/codegen/CompilerContext.cpp index 9d0d6d37..729fb9cc 100644 --- a/libsolidity/codegen/CompilerContext.cpp +++ b/libsolidity/codegen/CompilerContext.cpp @@ -124,6 +124,7 @@ void CompilerContext::addVariable(VariableDeclaration const& _declaration, unsigned _offsetToCurrent) { solAssert(m_asm->deposit() >= 0 && unsigned(m_asm->deposit()) >= _offsetToCurrent, ""); + solAssert(m_localVariables.count(&_declaration) == 0, "Variable already present"); m_localVariables[&_declaration] = unsigned(m_asm->deposit()) - _offsetToCurrent; } diff --git a/libsolidity/codegen/ContractCompiler.cpp b/libsolidity/codegen/ContractCompiler.cpp index dc090634..8724521b 100644 --- a/libsolidity/codegen/ContractCompiler.cpp +++ b/libsolidity/codegen/ContractCompiler.cpp @@ -879,6 +879,7 @@ void ContractCompiler::appendModifierOrFunctionCode() solAssert(m_currentFunction, ""); unsigned stackSurplus = 0; Block const* codeBlock = nullptr; + vector addedVariables; m_modifierDepth++; @@ -902,6 +903,7 @@ void ContractCompiler::appendModifierOrFunctionCode() for (unsigned i = 0; i < modifier.parameters().size(); ++i) { m_context.addVariable(*modifier.parameters()[i]); + addedVariables.push_back(modifier.parameters()[i].get()); compileExpression( *modifierInvocation->arguments()[i], modifier.parameters()[i]->annotation().type @@ -928,6 +930,8 @@ void ContractCompiler::appendModifierOrFunctionCode() m_returnTags.pop_back(); CompilerUtils(m_context).popStackSlots(stackSurplus); + for (auto var: addedVariables) + m_context.removeVariable(*var); } m_modifierDepth--; } diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 9775d6d0..d9bdad02 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -1049,6 +1049,28 @@ BOOST_AUTO_TEST_CASE(function_modifier_invocation_local_variables) CHECK_SUCCESS(text); } +BOOST_AUTO_TEST_CASE(function_modifier_double_invocation) +{ + char const* text = R"( + contract B { + function f(uint x) mod(x) mod(2) { } + modifier mod(uint a) { if (a > 0) _; } + } + )"; + CHECK_ERROR(text, DeclarationError, "Modifier already used for this function"); +} + +BOOST_AUTO_TEST_CASE(base_constructor_double_invocation) +{ + char const* text = R"( + contract C { function C(uint a) {} } + contract B is C { + function B() C(2) C(2) {} + } + )"; + CHECK_ERROR(text, DeclarationError, "Base constructor already provided"); +} + BOOST_AUTO_TEST_CASE(legal_modifier_override) { char const* text = R"( -- cgit