From f3a84eab91a6f05cfe58e260f206f9af51811313 Mon Sep 17 00:00:00 2001 From: Rhett Aultman Date: Fri, 18 Nov 2016 07:37:15 -0800 Subject: Error out when contracts collide on name The previous behaviour, courtesy of the [] operator in std::map, would uncritically store a new ContractDefinition in m_contracts even when a ContractDefinition already existed. This "resolved" collissions on contract names by clobbering the original one with the new one, and could lead to scenarios where the clobber would only be discovered when the original ContractDefinition could not be found or referred to, which was an unhelpful InternalCompilerError. This change checks the m_contracts map for a collision first and will not let the ContractDefinition be changed to a new one once it's set, throwing a CompilerError with information about the conflict. --- libsolidity/interface/CompilerStack.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'libsolidity/interface') diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index a31df584..5d4ccd92 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -180,6 +180,15 @@ bool CompilerStack::parse() if (!resolver.updateDeclaration(*m_globalContext->currentThis())) return false; if (!resolver.updateDeclaration(*m_globalContext->currentSuper())) return false; if (!resolver.resolveNamesAndTypes(*contract)) return false; + if (m_contracts.find(contract->name()) != m_contracts.end()) + { + const ContractDefinition* existingContract = m_contracts.find(contract->name())->second.contract; + if (contract != existingContract) + BOOST_THROW_EXCEPTION(CompilerError() << + errinfo_sourceLocation(contract->location()) << + errinfo_comment(contract->name() + " conflicts with contract at " + + *(existingContract->location().sourceName))); + } m_contracts[contract->name()].contract = contract; } @@ -201,6 +210,16 @@ bool CompilerStack::parse() else noErrors = false; + if (m_contracts.find(contract->name()) != m_contracts.end()) + { + const ContractDefinition* existingContract = m_contracts.find(contract->name())->second.contract; + if (contract != existingContract) + BOOST_THROW_EXCEPTION(CompilerError() << + errinfo_sourceLocation(contract->location()) << + errinfo_comment(contract->name() + " conflicts with!!! contract at " + + *(existingContract->location().sourceName))); + } + m_contracts[contract->name()].contract = contract; } -- cgit