From 30e89b3d9a85d7dba3e9858a19b3495e9c8f759a Mon Sep 17 00:00:00 2001 From: LianaHus Date: Thu, 10 Sep 2015 15:05:10 +0200 Subject: [PATCH 1/3] added test Conflicts: test/libsolidity/SolidityEndToEndTest.cpp --- test/libsolidity/SolidityEndToEndTest.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index aa423330f..8f38adac5 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -5230,6 +5230,17 @@ BOOST_AUTO_TEST_CASE(storage_string_as_mapping_key_without_variable) BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(2))); } +BOOST_AUTO_TEST_CASE(string_as_mapping_key) +{ + char const* sourceCode = R"( + contract Test { + function f() { var x = new Test(); } + } + )"; + + compileAndRun(sourceCode, 0, "Test"); +} + BOOST_AUTO_TEST_SUITE_END() } From 47e42430f27c579e09157f3373238e3bbe8ab93e Mon Sep 17 00:00:00 2001 From: LianaHus Date: Thu, 10 Sep 2015 17:17:13 +0200 Subject: [PATCH 2/3] added type check if the type of the var decl is one of base contract type --- libsolidity/AST.cpp | 9 ++++++++- libsolidity/AST.h | 6 ++++-- test/libsolidity/SolidityEndToEndTest.cpp | 11 ----------- test/libsolidity/SolidityNameAndTypeResolution.cpp | 12 ++++++++++++ 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/libsolidity/AST.cpp b/libsolidity/AST.cpp index 25d2ccd84..1b22c44f8 100644 --- a/libsolidity/AST.cpp +++ b/libsolidity/AST.cpp @@ -1001,10 +1001,17 @@ void NewExpression::checkTypeRequirements(TypePointers const*) { m_contractName->checkTypeRequirements(nullptr); m_contract = dynamic_cast(&m_contractName->referencedDeclaration()); + if (!m_contract) BOOST_THROW_EXCEPTION(createTypeError("Identifier is not a contract.")); if (!m_contract->isFullyImplemented()) BOOST_THROW_EXCEPTION(createTypeError("Trying to create an instance of an abstract contract.")); + + auto scopeContract = m_contractName->contractScope(); + auto bases = m_contract->linearizedBaseContracts(); + if (find(bases.begin(), bases.end(), scopeContract) != bases.end()) + BOOST_THROW_EXCEPTION(createTypeError("Circular reference for contract creation: cannot create instance of derived or same contract.")); + shared_ptr contractType = make_shared(*m_contract); TypePointers const& parameterTypes = contractType->constructorType()->parameterTypes(); m_type = make_shared( @@ -1137,7 +1144,7 @@ void Identifier::checkTypeRequirements(TypePointers const* _argumentTypes) } solAssert(!!m_referencedDeclaration, "Referenced declaration is null after overload resolution."); m_isLValue = m_referencedDeclaration->isLValue(); - m_type = m_referencedDeclaration->type(m_currentContract); + m_type = m_referencedDeclaration->type(m_contractScope); if (!m_type) BOOST_THROW_EXCEPTION(createTypeError("Declaration referenced before type could be determined.")); } diff --git a/libsolidity/AST.h b/libsolidity/AST.h index 6068e7561..1288b5d3b 100644 --- a/libsolidity/AST.h +++ b/libsolidity/AST.h @@ -1257,7 +1257,7 @@ public: ) { m_referencedDeclaration = &_referencedDeclaration; - m_currentContract = _currentContract; + m_contractScope = _currentContract; } Declaration const& referencedDeclaration() const; @@ -1273,6 +1273,8 @@ public: /// argument types in a call context. void overloadResolution(TypePointers const& _argumentTypes); + ContractDefinition const* contractScope() { return m_contractScope; } + private: ASTPointer m_name; @@ -1280,7 +1282,7 @@ private: Declaration const* m_referencedDeclaration = nullptr; /// Stores a reference to the current contract. This is needed because types of base contracts /// change depending on the context. - ContractDefinition const* m_currentContract = nullptr; + ContractDefinition const* m_contractScope = nullptr; /// A vector of overloaded declarations, right now only FunctionDefinition has overloaded declarations. std::vector m_overloadedDeclarations; }; diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 8f38adac5..aa423330f 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -5230,17 +5230,6 @@ BOOST_AUTO_TEST_CASE(storage_string_as_mapping_key_without_variable) BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(2))); } -BOOST_AUTO_TEST_CASE(string_as_mapping_key) -{ - char const* sourceCode = R"( - contract Test { - function f() { var x = new Test(); } - } - )"; - - compileAndRun(sourceCode, 0, "Test"); -} - BOOST_AUTO_TEST_SUITE_END() } diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 9f352b364..2b343db3d 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2194,6 +2194,18 @@ BOOST_AUTO_TEST_CASE(string_bytes_conversion) BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text)); } + +BOOST_AUTO_TEST_CASE(creating_contract_within_the_contract) +{ + char const* sourceCode = R"( + contract Test { + function f() { var x = new Test(); } + } + )"; + + BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError); +} + BOOST_AUTO_TEST_SUITE_END() } From d570ab44c893972dfc4541d4970ec9a172e28096 Mon Sep 17 00:00:00 2001 From: LianaHus Date: Fri, 11 Sep 2015 13:39:25 +0200 Subject: [PATCH 3/3] style fixes --- libsolidity/AST.h | 2 +- test/libsolidity/SolidityNameAndTypeResolution.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libsolidity/AST.h b/libsolidity/AST.h index 1288b5d3b..48790e6a2 100644 --- a/libsolidity/AST.h +++ b/libsolidity/AST.h @@ -1273,7 +1273,7 @@ public: /// argument types in a call context. void overloadResolution(TypePointers const& _argumentTypes); - ContractDefinition const* contractScope() { return m_contractScope; } + ContractDefinition const* contractScope() const { return m_contractScope; } private: ASTPointer m_name; diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 2b343db3d..883d78074 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2200,7 +2200,7 @@ BOOST_AUTO_TEST_CASE(creating_contract_within_the_contract) char const* sourceCode = R"( contract Test { function f() { var x = new Test(); } - } + } )"; BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);