From 1a6f0fe52f2019cad6dd748933c67b7411f34181 Mon Sep 17 00:00:00 2001 From: hrkrshnn Date: Wed, 11 Nov 2020 12:10:25 +0100 Subject: [PATCH] Disallow declarations to have names "this", "super" and "_". There will be a declaration error 3726 if these names are used in the contract. Note that there is an existing warning about shadowing a built-in for "this" and "super". --- libsolidity/analysis/NameAndTypeResolver.cpp | 27 ++++++++++++++++++++ libsolidity/analysis/ReferencesResolver.cpp | 7 +++++ 2 files changed, 34 insertions(+) diff --git a/libsolidity/analysis/NameAndTypeResolver.cpp b/libsolidity/analysis/NameAndTypeResolver.cpp index 0885cc530..80167e3f4 100644 --- a/libsolidity/analysis/NameAndTypeResolver.cpp +++ b/libsolidity/analysis/NameAndTypeResolver.cpp @@ -509,6 +509,33 @@ bool DeclarationRegistrationHelper::registerDeclaration( // We use "invisible" for both inactive variables in blocks and for members invisible in contracts. // They cannot both be true at the same time. solAssert(!(_inactive && !_declaration.isVisibleInContract()), ""); + + static set illegalNames{"_", "super", "this"}; + + if (illegalNames.count(name)) + { + auto isPublicFunctionOrEvent = [](Declaration const* _d) -> bool + { + if (auto functionDefinition = dynamic_cast(_d)) + { + if (!functionDefinition->isFree() && functionDefinition->isPublic()) + return true; + } + else if (dynamic_cast(_d)) + return true; + + return false; + }; + + // We allow an exception for public functions or events. + if (!isPublicFunctionOrEvent(&_declaration)) + _errorReporter.declarationError( + 3726_error, + *_errorLocation, + "The name \"" + name + "\" is reserved." + ); + } + if (!_container.registerDeclaration(_declaration, _name, _errorLocation, !_declaration.isVisibleInContract() || _inactive, false)) { SourceLocation firstDeclarationLocation; diff --git a/libsolidity/analysis/ReferencesResolver.cpp b/libsolidity/analysis/ReferencesResolver.cpp index 0560957d7..3cdb6245c 100644 --- a/libsolidity/analysis/ReferencesResolver.cpp +++ b/libsolidity/analysis/ReferencesResolver.cpp @@ -380,4 +380,11 @@ void ReferencesResolver::validateYulIdentifierName(yul::YulString _name, SourceL _location, "User-defined identifiers in inline assembly cannot contain '.'." ); + + if (set{"this", "super", "_"}.count(_name.str())) + m_errorReporter.declarationError( + 4113_error, + _location, + "The identifier name \"" + _name.str() + "\" is reserved." + ); }