From 0dd75ac100d59d81321d8815638c8f252b2fe467 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Sat, 15 Oct 2016 02:13:08 +0100 Subject: Use warning function in TypeChecker --- libsolidity/analysis/TypeChecker.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 332ce2c3..b5955c8f 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -718,11 +718,10 @@ bool TypeChecker::visit(VariableDeclarationStatement const& _statement) { if (ref->dataStoredIn(DataLocation::Storage)) { - auto err = make_shared(Error::Type::Warning); - *err << - errinfo_sourceLocation(varDecl.location()) << - errinfo_comment("Uninitialized storage pointer. Did you mean ' memory " + varDecl.name() + "'?"); - m_errors.push_back(err); + warning( + varDecl.location(), + "Uninitialized storage pointer. Did you mean ' memory " + varDecl.name() + "'?" + ); } } varDecl.accept(*this); -- cgit From e1ff8bebe1e9468ac7ea2d1d221a22381052c653 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Sat, 15 Oct 2016 16:19:59 +0100 Subject: Mention in changelog --- Changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Changelog.md b/Changelog.md index 343d8aa7..39e03014 100644 --- a/Changelog.md +++ b/Changelog.md @@ -4,6 +4,7 @@ Features: * Inline assembly: support both ``suicide`` and ``selfdestruct`` opcodes (note: ``suicide`` is deprecated). + * Inline assembly: issue warning if stack is not balanced after block. * Include ``keccak256()`` as an alias to ``sha3()``. * Support shifting constant numbers. -- cgit From c3330faf210ee06464dbc71a7946e069033299ac Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 20 Oct 2016 01:09:28 +0100 Subject: Issue warnings if stack is not balanced after inline assembly block --- libsolidity/inlineasm/AsmCodeGen.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/libsolidity/inlineasm/AsmCodeGen.cpp b/libsolidity/inlineasm/AsmCodeGen.cpp index 53d19b0a..5d920cb7 100644 --- a/libsolidity/inlineasm/AsmCodeGen.cpp +++ b/libsolidity/inlineasm/AsmCodeGen.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -213,10 +214,31 @@ public: void operator()(assembly::Block const& _block) { size_t numVariables = m_state.variables.size(); + int deposit = m_state.assembly.deposit(); std::for_each(_block.statements.begin(), _block.statements.end(), boost::apply_visitor(*this)); - // pop variables - // we deliberately do not check stack height + deposit = m_state.assembly.deposit() - deposit; + m_state.assembly.setSourceLocation(_block.location); + + // issue warnings for stack height discrepancies + if (deposit < 0) + { + m_state.addError( + Error::Type::Warning, + "Inline assembly block is not balanced. It takes " + toString(-deposit) + " item(s) from the stack.", + _block.location + ); + } + else if (deposit > 0) + { + m_state.addError( + Error::Type::Warning, + "Inline assembly block is not balanced. It leaves " + toString(deposit) + " item(s) on the stack.", + _block.location + ); + } + + // pop variables while (m_state.variables.size() > numVariables) { m_state.assembly.append(solidity::Instruction::POP); -- cgit From 0e33b6346802581cfcc45195344a705ebd683c1f Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 20 Oct 2016 01:09:57 +0100 Subject: Allow warnings for inline assembly block --- libsolidity/codegen/ContractCompiler.cpp | 2 +- test/libsolidity/InlineAssembly.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libsolidity/codegen/ContractCompiler.cpp b/libsolidity/codegen/ContractCompiler.cpp index 18b42fce..ebb84784 100644 --- a/libsolidity/codegen/ContractCompiler.cpp +++ b/libsolidity/codegen/ContractCompiler.cpp @@ -575,7 +575,7 @@ bool ContractCompiler::visit(InlineAssembly const& _inlineAssembly) return true; } ); - solAssert(errors.empty(), "Code generation for inline assembly with errors requested."); + solAssert(Error::containsOnlyWarnings(errors), "Code generation for inline assembly with errors requested."); m_context.setStackOffset(startStackHeight); return false; } diff --git a/test/libsolidity/InlineAssembly.cpp b/test/libsolidity/InlineAssembly.cpp index f8655c0c..6c04367f 100644 --- a/test/libsolidity/InlineAssembly.cpp +++ b/test/libsolidity/InlineAssembly.cpp @@ -51,7 +51,7 @@ bool successParse(std::string const& _source, bool _assemble = false) if (_assemble) { stack.assemble(); - if (!stack.errors().empty()) + if (!stack.errors().empty() && !Error::containsOnlyWarnings(stack.errors())) return false; } } -- cgit From 6cb2770344ccc781911d48a645335733e850e0af Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 20 Oct 2016 01:15:39 +0100 Subject: Add two test cases for unbalanced inline assembly --- test/libsolidity/SolidityNameAndTypeResolution.cpp | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 7eedbefa..83472369 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -4079,6 +4079,34 @@ BOOST_AUTO_TEST_CASE(shift_constant_right_excessive_rvalue) BOOST_CHECK(expectError(text, false) == Error::Type::TypeError); } +BOOST_AUTO_TEST_CASE(inline_assembly_unbalanced_positive_stack) +{ + char const* text = R"( + contract test { + function f() { + assembly { + 1 + } + } + } + )"; + BOOST_CHECK(expectError(text, true) == Error::Type::Warning); +} + +BOOST_AUTO_TEST_CASE(inline_assembly_unbalanced_negative_stack) +{ + char const* text = R"( + contract test { + function f() { + assembly { + pop + } + } + } + )"; + BOOST_CHECK(expectError(text, true) == Error::Type::Warning); +} + BOOST_AUTO_TEST_SUITE_END() } -- cgit