From eb6e4f06741a1ab539de6f797d16d204a7f1e9a9 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 10 Jun 2015 09:58:59 +0200 Subject: [PATCH] Improved exception safety in CSE. Fixes #2135 --- libevmasm/CommonSubexpressionEliminator.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/libevmasm/CommonSubexpressionEliminator.cpp b/libevmasm/CommonSubexpressionEliminator.cpp index fadf2776a..2c4742d61 100644 --- a/libevmasm/CommonSubexpressionEliminator.cpp +++ b/libevmasm/CommonSubexpressionEliminator.cpp @@ -35,6 +35,19 @@ vector CommonSubexpressionEliminator::getOptimizedItems() { optimizeBreakingItem(); + KnownState nextInitialState = m_state; + if (m_breakingItem) + nextInitialState.feedItem(*m_breakingItem); + KnownState nextState = nextInitialState; + + ScopeGuard reset([&]() + { + m_breakingItem = nullptr; + m_storeOperations.clear(); + m_initialState = move(nextInitialState); + m_state = move(nextState); + }); + map initialStackContents; map targetStackContents; int minHeight = m_state.stackHeight() + 1; @@ -52,15 +65,7 @@ vector CommonSubexpressionEliminator::getOptimizedItems() targetStackContents ); if (m_breakingItem) - { items.push_back(*m_breakingItem); - m_state.feedItem(*m_breakingItem); - } - - // cleanup - m_initialState = m_state; - m_breakingItem = nullptr; - m_storeOperations.clear(); return items; }