mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #4439 from ethereum/address_members
[BREAKING] Enforce address members not accessible by contract instance
This commit is contained in:
@@ -1930,6 +1930,9 @@ bool TypeChecker::visit(MemberAccess const& _memberAccess)
|
||||
else
|
||||
++it;
|
||||
}
|
||||
|
||||
auto& annotation = _memberAccess.annotation();
|
||||
|
||||
if (possibleMembers.size() == 0)
|
||||
{
|
||||
if (initialMemberCount == 0)
|
||||
@@ -1947,11 +1950,21 @@ bool TypeChecker::visit(MemberAccess const& _memberAccess)
|
||||
" outside of storage."
|
||||
);
|
||||
}
|
||||
string errorMsg = "Member \"" + memberName + "\" not found or not visible "
|
||||
"after argument-dependent lookup in " + exprType->toString() +
|
||||
(memberName == "value" ? " - did you forget the \"payable\" modifier?" : ".");
|
||||
if (exprType->category() == Type::Category::Contract)
|
||||
for (auto const& addressMember: IntegerType(160, IntegerType::Modifier::Address).nativeMembers(nullptr))
|
||||
if (addressMember.name == memberName)
|
||||
{
|
||||
Identifier const* var = dynamic_cast<Identifier const*>(&_memberAccess.expression());
|
||||
string varName = var ? var->name() : "...";
|
||||
errorMsg += " Use \"address(" + varName + ")." + memberName + "\" to access this address member.";
|
||||
break;
|
||||
}
|
||||
m_errorReporter.fatalTypeError(
|
||||
_memberAccess.location(),
|
||||
"Member \"" + memberName + "\" not found or not visible "
|
||||
"after argument-dependent lookup in " + exprType->toString() +
|
||||
(memberName == "value" ? " - did you forget the \"payable\" modifier?" : ".")
|
||||
errorMsg
|
||||
);
|
||||
}
|
||||
else if (possibleMembers.size() > 1)
|
||||
@@ -1962,7 +1975,6 @@ bool TypeChecker::visit(MemberAccess const& _memberAccess)
|
||||
(memberName == "value" ? " - did you forget the \"payable\" modifier?" : ".")
|
||||
);
|
||||
|
||||
auto& annotation = _memberAccess.annotation();
|
||||
annotation.referencedDeclaration = possibleMembers.front().declaration;
|
||||
annotation.type = possibleMembers.front().type;
|
||||
|
||||
@@ -1995,20 +2007,6 @@ bool TypeChecker::visit(MemberAccess const& _memberAccess)
|
||||
|
||||
if (exprType->category() == Type::Category::Contract)
|
||||
{
|
||||
// Warn about using address members on contracts
|
||||
bool v050 = m_scope->sourceUnit().annotation().experimentalFeatures.count(ExperimentalFeature::V050);
|
||||
for (auto const& addressMember: IntegerType(160, IntegerType::Modifier::Address).nativeMembers(nullptr))
|
||||
if (addressMember.name == memberName && *annotation.type == *addressMember.type)
|
||||
{
|
||||
solAssert(!v050, "Address member still present on contract in v0.5.0.");
|
||||
m_errorReporter.warning(
|
||||
_memberAccess.location(),
|
||||
"Using contract member \"" + memberName +"\" inherited from the address type is deprecated." +
|
||||
" Convert the contract to \"address\" type to access the member,"
|
||||
" for example use \"address(contract)." + memberName + "\" instead."
|
||||
);
|
||||
}
|
||||
|
||||
// Warn about using send or transfer with a non-payable fallback function.
|
||||
if (auto callType = dynamic_cast<FunctionType const*>(type(_memberAccess).get()))
|
||||
{
|
||||
|
||||
@@ -1873,47 +1873,9 @@ MemberList::MemberMap ContractType::nativeMembers(ContractDefinition const* _con
|
||||
&it.second->declaration()
|
||||
));
|
||||
}
|
||||
// In 0.5.0 address members are not populated into the contract.
|
||||
if (!_contract->sourceUnit().annotation().experimentalFeatures.count(ExperimentalFeature::V050))
|
||||
addNonConflictingAddressMembers(members);
|
||||
return members;
|
||||
}
|
||||
|
||||
void ContractType::addNonConflictingAddressMembers(MemberList::MemberMap& _members)
|
||||
{
|
||||
MemberList::MemberMap addressMembers = IntegerType(160, IntegerType::Modifier::Address).nativeMembers(nullptr);
|
||||
for (auto const& addressMember: addressMembers)
|
||||
{
|
||||
bool clash = false;
|
||||
for (auto const& member: _members)
|
||||
{
|
||||
if (
|
||||
member.name == addressMember.name &&
|
||||
(
|
||||
// Members with different types are not allowed
|
||||
member.type->category() != addressMember.type->category() ||
|
||||
// Members must overload functions without clash
|
||||
(
|
||||
member.type->category() == Type::Category::Function &&
|
||||
dynamic_cast<FunctionType const&>(*member.type).hasEqualArgumentTypes(dynamic_cast<FunctionType const&>(*addressMember.type))
|
||||
)
|
||||
)
|
||||
)
|
||||
{
|
||||
clash = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!clash)
|
||||
_members.push_back(MemberList::Member(
|
||||
addressMember.name,
|
||||
addressMember.type,
|
||||
addressMember.declaration
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
shared_ptr<FunctionType const> const& ContractType::newExpressionType() const
|
||||
{
|
||||
if (!m_constructorType)
|
||||
|
||||
@@ -740,8 +740,6 @@ public:
|
||||
std::vector<std::tuple<VariableDeclaration const*, u256, unsigned>> stateVariables() const;
|
||||
|
||||
private:
|
||||
static void addNonConflictingAddressMembers(MemberList::MemberMap& _members);
|
||||
|
||||
ContractDefinition const& m_contract;
|
||||
/// If true, it is the "super" type of the current contract, i.e. it contains only inherited
|
||||
/// members.
|
||||
|
||||
@@ -1214,63 +1214,52 @@ bool ExpressionCompiler::visit(MemberAccess const& _memberAccess)
|
||||
switch (_memberAccess.expression().annotation().type->category())
|
||||
{
|
||||
case Type::Category::Contract:
|
||||
case Type::Category::Integer:
|
||||
{
|
||||
bool alsoSearchInteger = false;
|
||||
if (_memberAccess.expression().annotation().type->category() == Type::Category::Contract)
|
||||
ContractType const& type = dynamic_cast<ContractType const&>(*_memberAccess.expression().annotation().type);
|
||||
if (type.isSuper())
|
||||
{
|
||||
ContractType const& type = dynamic_cast<ContractType const&>(*_memberAccess.expression().annotation().type);
|
||||
if (type.isSuper())
|
||||
{
|
||||
solAssert(!!_memberAccess.annotation().referencedDeclaration, "Referenced declaration not resolved.");
|
||||
utils().pushCombinedFunctionEntryLabel(m_context.superFunction(
|
||||
dynamic_cast<FunctionDefinition const&>(*_memberAccess.annotation().referencedDeclaration),
|
||||
type.contractDefinition()
|
||||
));
|
||||
}
|
||||
solAssert(!!_memberAccess.annotation().referencedDeclaration, "Referenced declaration not resolved.");
|
||||
utils().pushCombinedFunctionEntryLabel(m_context.superFunction(
|
||||
dynamic_cast<FunctionDefinition const&>(*_memberAccess.annotation().referencedDeclaration),
|
||||
type.contractDefinition()
|
||||
));
|
||||
}
|
||||
// ordinary contract type
|
||||
else if (Declaration const* declaration = _memberAccess.annotation().referencedDeclaration)
|
||||
{
|
||||
u256 identifier;
|
||||
if (auto const* variable = dynamic_cast<VariableDeclaration const*>(declaration))
|
||||
identifier = FunctionType(*variable).externalIdentifier();
|
||||
else if (auto const* function = dynamic_cast<FunctionDefinition const*>(declaration))
|
||||
identifier = FunctionType(*function).externalIdentifier();
|
||||
else
|
||||
{
|
||||
// ordinary contract type
|
||||
if (Declaration const* declaration = _memberAccess.annotation().referencedDeclaration)
|
||||
{
|
||||
u256 identifier;
|
||||
if (auto const* variable = dynamic_cast<VariableDeclaration const*>(declaration))
|
||||
identifier = FunctionType(*variable).externalIdentifier();
|
||||
else if (auto const* function = dynamic_cast<FunctionDefinition const*>(declaration))
|
||||
identifier = FunctionType(*function).externalIdentifier();
|
||||
else
|
||||
solAssert(false, "Contract member is neither variable nor function.");
|
||||
utils().convertType(type, IntegerType(160, IntegerType::Modifier::Address), true);
|
||||
m_context << identifier;
|
||||
}
|
||||
else
|
||||
// not found in contract, search in members inherited from address
|
||||
alsoSearchInteger = true;
|
||||
}
|
||||
solAssert(false, "Contract member is neither variable nor function.");
|
||||
utils().convertType(type, IntegerType(160, IntegerType::Modifier::Address), true);
|
||||
m_context << identifier;
|
||||
}
|
||||
else
|
||||
alsoSearchInteger = true;
|
||||
|
||||
if (alsoSearchInteger)
|
||||
solAssert(false, "Invalid member access in contract");
|
||||
break;
|
||||
}
|
||||
case Type::Category::Integer:
|
||||
{
|
||||
if (member == "balance")
|
||||
{
|
||||
if (member == "balance")
|
||||
{
|
||||
utils().convertType(
|
||||
*_memberAccess.expression().annotation().type,
|
||||
IntegerType(160, IntegerType::Modifier::Address),
|
||||
true
|
||||
);
|
||||
m_context << Instruction::BALANCE;
|
||||
}
|
||||
else if ((set<string>{"send", "transfer", "call", "callcode", "delegatecall"}).count(member))
|
||||
utils().convertType(
|
||||
*_memberAccess.expression().annotation().type,
|
||||
IntegerType(160, IntegerType::Modifier::Address),
|
||||
true
|
||||
);
|
||||
else
|
||||
solAssert(false, "Invalid member access to integer");
|
||||
utils().convertType(
|
||||
*_memberAccess.expression().annotation().type,
|
||||
IntegerType(160, IntegerType::Modifier::Address),
|
||||
true
|
||||
);
|
||||
m_context << Instruction::BALANCE;
|
||||
}
|
||||
else if ((set<string>{"send", "transfer", "call", "callcode", "delegatecall"}).count(member))
|
||||
utils().convertType(
|
||||
*_memberAccess.expression().annotation().type,
|
||||
IntegerType(160, IntegerType::Modifier::Address),
|
||||
true
|
||||
);
|
||||
else
|
||||
solAssert(false, "Invalid member access to integer");
|
||||
break;
|
||||
}
|
||||
case Type::Category::Function:
|
||||
|
||||
Reference in New Issue
Block a user