From b25f0c52ac01857a82dda20ec2b646c7ae90cd7d Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Sat, 12 Aug 2017 00:45:37 +0100 Subject: Reject the creation of interface with the new statement --- Changelog.md | 1 + libsolidity/analysis/TypeChecker.cpp | 6 +++++ libsolidity/ast/Types.cpp | 3 +++ test/libsolidity/SolidityNameAndTypeResolution.cpp | 26 ++++++++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/Changelog.md b/Changelog.md index 3574455b..39cbd44b 100644 --- a/Changelog.md +++ b/Changelog.md @@ -15,6 +15,7 @@ Bugfixes: * Parser: Limit maximum recursion depth. * Type Checker: Crash fix related to ``using``. * Type Checker: Disallow constructors in libraries. + * Type Checker: Reject the creation of interface contracts using the ``new`` statement. ### 0.4.15 (2017-08-08) diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 32d078fd..f70b75d3 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -429,6 +429,10 @@ void TypeChecker::endVisit(InheritanceSpecifier const& _inheritance) if (base->isLibrary()) m_errorReporter.typeError(_inheritance.location(), "Libraries cannot be inherited from."); + // Interface can have no constructors - no need to validate + if (base->contractKind() == ContractDefinition::ContractKind::Interface) + return; + auto const& arguments = _inheritance.arguments(); TypePointers parameterTypes = ContractType(*base).newExpressionType()->parameterTypes(); if (!arguments.empty() && parameterTypes.size() != arguments.size()) @@ -1554,6 +1558,8 @@ void TypeChecker::endVisit(NewExpression const& _newExpression) if (!contract) m_errorReporter.fatalTypeError(_newExpression.location(), "Identifier is not a contract."); + if (contract->contractKind() == ContractDefinition::ContractKind::Interface) + m_errorReporter.fatalTypeError(_newExpression.location(), "Cannot instantiate an interface."); if (!contract->annotation().unimplementedFunctions.empty()) m_errorReporter.typeError( _newExpression.location(), diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index e6664895..adf41229 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -2140,6 +2140,8 @@ FunctionTypePointer FunctionType::newExpressionType(ContractDefinition const& _c strings parameterNames; StateMutability stateMutability = StateMutability::NonPayable; + solAssert(_contract.contractKind() != ContractDefinition::ContractKind::Interface, ""); + if (constructor) { for (ASTPointer const& var: constructor->parameters()) @@ -2150,6 +2152,7 @@ FunctionTypePointer FunctionType::newExpressionType(ContractDefinition const& _c if (constructor->isPayable()) stateMutability = StateMutability::Payable; } + return make_shared( parameters, TypePointers{make_shared(_contract)}, diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index e349bf83..a4fc9c98 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -6709,6 +6709,32 @@ BOOST_AUTO_TEST_CASE(experimental_pragma) // CHECK_ERROR_ALLOW_MULTI(text, SyntaxError, "Duplicate experimental feature name."); } +BOOST_AUTO_TEST_CASE(reject_interface_creation) +{ + char const* text = R"( + interface I {} + contract C { + function f() { + new I(); + } + } + )"; + CHECK_ERROR(text, TypeError, "Cannot instantiate an interface."); +} + +BOOST_AUTO_TEST_CASE(accept_library_creation) +{ + char const* text = R"( + library L {} + contract C { + function f() { + new L(); + } + } + )"; + CHECK_SUCCESS(text); +} + BOOST_AUTO_TEST_SUITE_END() } -- cgit From 7b0046a9aafffec4e42be7e30c283e07ca1841b9 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 21 Aug 2017 16:43:15 +0200 Subject: Check inheritance specifier arguments for interfaces. --- libsolidity/analysis/TypeChecker.cpp | 10 +++++----- test/libsolidity/SolidityNameAndTypeResolution.cpp | 9 +++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index f70b75d3..99f3c64c 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -429,12 +429,12 @@ void TypeChecker::endVisit(InheritanceSpecifier const& _inheritance) if (base->isLibrary()) m_errorReporter.typeError(_inheritance.location(), "Libraries cannot be inherited from."); - // Interface can have no constructors - no need to validate - if (base->contractKind() == ContractDefinition::ContractKind::Interface) - return; - auto const& arguments = _inheritance.arguments(); - TypePointers parameterTypes = ContractType(*base).newExpressionType()->parameterTypes(); + TypePointers parameterTypes; + if (base->contractKind() != ContractDefinition::ContractKind::Interface) + // Interfaces do not have constructors, so there are zero parameters. + parameterTypes = ContractType(*base).newExpressionType()->parameterTypes(); + if (!arguments.empty() && parameterTypes.size() != arguments.size()) { m_errorReporter.typeError( diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index a4fc9c98..f5f607ca 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -6735,6 +6735,15 @@ BOOST_AUTO_TEST_CASE(accept_library_creation) CHECK_SUCCESS(text); } +BOOST_AUTO_TEST_CASE(reject_interface_constructors) +{ + char const* text = R"( + interface I {} + contract C is I(2) {} + )"; + CHECK_ERROR(text, TypeError, "Wrong argument count for constructor call: 1 arguments given but expected 0."); +} + BOOST_AUTO_TEST_SUITE_END() } -- cgit