mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Check that no internals are used in any external function type.
This commit is contained in:
parent
f3d0433ec3
commit
22b4d1b29a
@ -97,6 +97,13 @@ void ReferencesResolver::endVisit(FunctionTypeName const& _typeName)
|
|||||||
|
|
||||||
if (_typeName.isPayable() && _typeName.visibility() != VariableDeclaration::Visibility::External)
|
if (_typeName.isPayable() && _typeName.visibility() != VariableDeclaration::Visibility::External)
|
||||||
fatalTypeError(_typeName.location(), "Only external function types can be payable.");
|
fatalTypeError(_typeName.location(), "Only external function types can be payable.");
|
||||||
|
if (_typeName.visibility() == VariableDeclaration::Visibility::External)
|
||||||
|
for (auto const& t: _typeName.parameterTypes() + _typeName.returnParameterTypes())
|
||||||
|
{
|
||||||
|
solAssert(t->annotation().type, "Type not set for parameter.");
|
||||||
|
if (!t->annotation().type->canBeUsedExternally(false))
|
||||||
|
fatalTypeError(t->location(), "Internal type cannot be used for external function type.");
|
||||||
|
}
|
||||||
|
|
||||||
_typeName.annotation().type = make_shared<FunctionType>(_typeName);
|
_typeName.annotation().type = make_shared<FunctionType>(_typeName);
|
||||||
}
|
}
|
||||||
|
@ -574,6 +574,14 @@ bool TypeChecker::visit(EventDefinition const& _eventDef)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TypeChecker::endVisit(FunctionTypeName const& _funType)
|
||||||
|
{
|
||||||
|
FunctionType const& fun = dynamic_cast<FunctionType const&>(*_funType.annotation().type);
|
||||||
|
if (fun.location() == FunctionType::Location::External)
|
||||||
|
if (!fun.canBeUsedExternally(false))
|
||||||
|
typeError(_funType.location(), "External function type uses internal types.");
|
||||||
|
}
|
||||||
|
|
||||||
bool TypeChecker::visit(InlineAssembly const& _inlineAssembly)
|
bool TypeChecker::visit(InlineAssembly const& _inlineAssembly)
|
||||||
{
|
{
|
||||||
// Inline assembly does not have its own type-checking phase, so we just run the
|
// Inline assembly does not have its own type-checking phase, so we just run the
|
||||||
|
@ -87,6 +87,7 @@ private:
|
|||||||
/// case this is a base constructor call.
|
/// case this is a base constructor call.
|
||||||
void visitManually(ModifierInvocation const& _modifier, std::vector<ContractDefinition const*> const& _bases);
|
void visitManually(ModifierInvocation const& _modifier, std::vector<ContractDefinition const*> const& _bases);
|
||||||
virtual bool visit(EventDefinition const& _eventDef) override;
|
virtual bool visit(EventDefinition const& _eventDef) override;
|
||||||
|
virtual void endVisit(FunctionTypeName const& _funType) override;
|
||||||
virtual bool visit(InlineAssembly const& _inlineAssembly) override;
|
virtual bool visit(InlineAssembly const& _inlineAssembly) override;
|
||||||
virtual bool visit(IfStatement const& _ifStatement) override;
|
virtual bool visit(IfStatement const& _ifStatement) override;
|
||||||
virtual bool visit(WhileStatement const& _whileStatement) override;
|
virtual bool visit(WhileStatement const& _whileStatement) override;
|
||||||
|
@ -1265,6 +1265,7 @@ TypePointer ArrayType::decodingType() const
|
|||||||
|
|
||||||
TypePointer ArrayType::interfaceType(bool _inLibrary) const
|
TypePointer ArrayType::interfaceType(bool _inLibrary) const
|
||||||
{
|
{
|
||||||
|
// Note: This has to fulfill canBeUsedExternally(_inLibrary) == !!interfaceType(_inLibrary)
|
||||||
if (_inLibrary && location() == DataLocation::Storage)
|
if (_inLibrary && location() == DataLocation::Storage)
|
||||||
return shared_from_this();
|
return shared_from_this();
|
||||||
|
|
||||||
@ -1282,6 +1283,21 @@ TypePointer ArrayType::interfaceType(bool _inLibrary) const
|
|||||||
return make_shared<ArrayType>(DataLocation::Memory, baseExt, m_length);
|
return make_shared<ArrayType>(DataLocation::Memory, baseExt, m_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ArrayType::canBeUsedExternally(bool _inLibrary) const
|
||||||
|
{
|
||||||
|
// Note: This has to fulfill canBeUsedExternally(_inLibrary) == !!interfaceType(_inLibrary)
|
||||||
|
if (_inLibrary && location() == DataLocation::Storage)
|
||||||
|
return true;
|
||||||
|
else if (m_arrayKind != ArrayKind::Ordinary)
|
||||||
|
return true;
|
||||||
|
else if (!m_baseType->canBeUsedExternally(_inLibrary))
|
||||||
|
return false;
|
||||||
|
else if (m_baseType->category() == Category::Array && m_baseType->isDynamicallySized())
|
||||||
|
return false;
|
||||||
|
else
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
u256 ArrayType::memorySize() const
|
u256 ArrayType::memorySize() const
|
||||||
{
|
{
|
||||||
solAssert(!isDynamicallySized(), "");
|
solAssert(!isDynamicallySized(), "");
|
||||||
@ -1815,11 +1831,19 @@ FunctionType::FunctionType(FunctionTypeName const& _typeName):
|
|||||||
for (auto const& t: _typeName.parameterTypes())
|
for (auto const& t: _typeName.parameterTypes())
|
||||||
{
|
{
|
||||||
solAssert(t->annotation().type, "Type not set for parameter.");
|
solAssert(t->annotation().type, "Type not set for parameter.");
|
||||||
|
solAssert(
|
||||||
|
m_location != Location::External || t->annotation().type->canBeUsedExternally(false),
|
||||||
|
"Internal type used as parameter for external function."
|
||||||
|
);
|
||||||
m_parameterTypes.push_back(t->annotation().type);
|
m_parameterTypes.push_back(t->annotation().type);
|
||||||
}
|
}
|
||||||
for (auto const& t: _typeName.returnParameterTypes())
|
for (auto const& t: _typeName.returnParameterTypes())
|
||||||
{
|
{
|
||||||
solAssert(t->annotation().type, "Type not set for return parameter.");
|
solAssert(t->annotation().type, "Type not set for return parameter.");
|
||||||
|
solAssert(
|
||||||
|
m_location != Location::External || t->annotation().type->canBeUsedExternally(false),
|
||||||
|
"Internal type used as return parameter for external function."
|
||||||
|
);
|
||||||
m_returnParameterTypes.push_back(t->annotation().type);
|
m_returnParameterTypes.push_back(t->annotation().type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -254,6 +254,9 @@ public:
|
|||||||
/// @param _inLibrary if set, returns types as used in a library, e.g. struct and contract types
|
/// @param _inLibrary if set, returns types as used in a library, e.g. struct and contract types
|
||||||
/// are returned without modification.
|
/// are returned without modification.
|
||||||
virtual TypePointer interfaceType(bool /*_inLibrary*/) const { return TypePointer(); }
|
virtual TypePointer interfaceType(bool /*_inLibrary*/) const { return TypePointer(); }
|
||||||
|
/// @returns true iff this type can be passed on via calls (to libraries if _inLibrary is true),
|
||||||
|
/// should be have identical to !!interfaceType(_inLibrary) but might do optimizations.
|
||||||
|
virtual bool canBeUsedExternally(bool _inLibrary) const { return !!interfaceType(_inLibrary); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// @returns a member list containing all members added to this type by `using for` directives.
|
/// @returns a member list containing all members added to this type by `using for` directives.
|
||||||
@ -580,6 +583,7 @@ public:
|
|||||||
virtual TypePointer encodingType() const override;
|
virtual TypePointer encodingType() const override;
|
||||||
virtual TypePointer decodingType() const override;
|
virtual TypePointer decodingType() const override;
|
||||||
virtual TypePointer interfaceType(bool _inLibrary) const override;
|
virtual TypePointer interfaceType(bool _inLibrary) const override;
|
||||||
|
virtual bool canBeUsedExternally(bool _inLibrary) const override;
|
||||||
|
|
||||||
/// @returns true if this is a byte array or a string
|
/// @returns true if this is a byte array or a string
|
||||||
bool isByteArray() const { return m_arrayKind != ArrayKind::Ordinary; }
|
bool isByteArray() const { return m_arrayKind != ArrayKind::Ordinary; }
|
||||||
|
Loading…
Reference in New Issue
Block a user