From 6ac12afd0623b9ae7b92123374f67f0b752d494d Mon Sep 17 00:00:00 2001 From: Mathias Baumann Date: Tue, 22 Oct 2019 12:26:26 +0200 Subject: [PATCH] Disallow call of same-class-ctor as modifier --- Changelog.md | 1 + libsolidity/analysis/TypeChecker.cpp | 8 +++++--- .../syntaxTests/modifiers/constructor_as_modifier.sol | 5 +++++ 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 test/libsolidity/syntaxTests/modifiers/constructor_as_modifier.sol diff --git a/Changelog.md b/Changelog.md index e4baf02db..bcb3966fb 100644 --- a/Changelog.md +++ b/Changelog.md @@ -8,6 +8,7 @@ Compiler Features: Bugfixes: + * Type Checker: Disallow constructor of the same class to be used as modifier diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index b5abe59f4..42b1be500 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -390,11 +390,13 @@ bool TypeChecker::visit(FunctionDefinition const& _function) set modifiers; for (ASTPointer const& modifier: _function.modifiers()) { + auto baseContracts = dynamic_cast(*_function.scope()).annotation().linearizedBaseContracts; + // Delete first base which is just the main contract itself + baseContracts.erase(baseContracts.begin()); + visitManually( *modifier, - _function.isConstructor() ? - dynamic_cast(*_function.scope()).annotation().linearizedBaseContracts : - vector() + _function.isConstructor() ? baseContracts : vector() ); Declaration const* decl = &dereference(*modifier->name()); if (modifiers.count(decl)) diff --git a/test/libsolidity/syntaxTests/modifiers/constructor_as_modifier.sol b/test/libsolidity/syntaxTests/modifiers/constructor_as_modifier.sol new file mode 100644 index 000000000..a65e0e84c --- /dev/null +++ b/test/libsolidity/syntaxTests/modifiers/constructor_as_modifier.sol @@ -0,0 +1,5 @@ +contract C { + constructor() C() public {} +} +// ---- +// TypeError: (31-34): Referenced declaration is neither modifier nor base class.