Browse Source

getInheritableMembers() does not look at BaseContracts

- Also adding tests for improper accessing members of other contracts.
cl-refactor
Lefteris Karapetsas 10 years ago
parent
commit
1681fbd885
  1. 27
      libsolidity/AST.cpp
  2. 29
      test/SolidityNameAndTypeResolution.cpp

27
libsolidity/AST.cpp

@ -215,26 +215,23 @@ vector<ASTPointer<Declaration>> const& ContractDefinition::getInheritableMembers
{ {
set<string> memberSeen; set<string> memberSeen;
m_inheritableMembers.reset(new vector<ASTPointer<Declaration>>()); m_inheritableMembers.reset(new vector<ASTPointer<Declaration>>());
for (ContractDefinition const* contract: getLinearizedBaseContracts()) auto addInheritableMember = [&](ASTPointer<Declaration> const& _decl)
{ {
auto addInheritableMember = [&](ASTPointer<Declaration> const& _decl) if (memberSeen.count(_decl->getName()) == 0 && _decl->isVisibleInDerivedContracts())
{ {
if (memberSeen.count(_decl->getName()) == 0 && _decl->isVisibleInDerivedContracts()) memberSeen.insert(_decl->getName());
{ m_inheritableMembers->push_back(_decl);
memberSeen.insert(_decl->getName()); }
m_inheritableMembers->push_back(_decl); };
}
};
for (ASTPointer<FunctionDefinition> const& f: contract->getDefinedFunctions()) for (ASTPointer<FunctionDefinition> const& f: getDefinedFunctions())
addInheritableMember(f); addInheritableMember(f);
for (ASTPointer<VariableDeclaration> const& v: contract->getStateVariables()) for (ASTPointer<VariableDeclaration> const& v: getStateVariables())
addInheritableMember(v); addInheritableMember(v);
for (ASTPointer<StructDefinition> const& s: contract->getDefinedStructs()) for (ASTPointer<StructDefinition> const& s: getDefinedStructs())
addInheritableMember(s); addInheritableMember(s);
}
} }
return *m_inheritableMembers; return *m_inheritableMembers;
} }

29
test/SolidityNameAndTypeResolution.cpp

@ -743,6 +743,35 @@ BOOST_AUTO_TEST_CASE(base_class_state_variable_internal_member)
BOOST_CHECK_NO_THROW(parseTextAndResolveNamesWithChecks(text)); BOOST_CHECK_NO_THROW(parseTextAndResolveNamesWithChecks(text));
} }
BOOST_AUTO_TEST_CASE(state_variable_member_of_wrong_class1)
{
char const* text = "contract Parent1 {\n"
" uint256 internal m_aMember1;\n"
"}\n"
"contract Parent2 is Parent1{\n"
" uint256 internal m_aMember2;\n"
"}\n"
"contract Child is Parent2{\n"
" function foo() returns (uint256) { return Parent2.m_aMember1; }\n"
"}\n";
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
}
BOOST_AUTO_TEST_CASE(state_variable_member_of_wrong_class2)
{
char const* text = "contract Parent1 {\n"
" uint256 internal m_aMember1;\n"
"}\n"
"contract Parent2 is Parent1{\n"
" uint256 internal m_aMember2;\n"
"}\n"
"contract Child is Parent2{\n"
" function foo() returns (uint256) { return Child.m_aMember2; }\n"
" uint256 public m_aMember3;\n"
"}\n";
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
}
BOOST_AUTO_TEST_CASE(fallback_function) BOOST_AUTO_TEST_CASE(fallback_function)
{ {
char const* text = R"( char const* text = R"(

Loading…
Cancel
Save