From 7d79fa1d91f41c7cb3eba21e5beea80190119cd6 Mon Sep 17 00:00:00 2001 From: xensik Date: Fri, 29 Jul 2022 11:43:07 +0200 Subject: [PATCH] fix decompiler empty switch cases --- src/h1/xsk/decompiler.cpp | 9 +++++---- src/h2/xsk/decompiler.cpp | 9 +++++---- src/iw5/xsk/decompiler.cpp | 9 +++++---- src/iw6/xsk/decompiler.cpp | 9 +++++---- src/iw7/xsk/decompiler.cpp | 9 +++++---- src/iw8/xsk/decompiler.cpp | 9 +++++---- src/s1/xsk/decompiler.cpp | 9 +++++---- src/s2/xsk/decompiler.cpp | 9 +++++---- src/s4/xsk/decompiler.cpp | 9 +++++---- src/t6/xsk/decompiler.cpp | 9 ++++++--- 10 files changed, 51 insertions(+), 39 deletions(-) diff --git a/src/h1/xsk/decompiler.cpp b/src/h1/xsk/decompiler.cpp index d63e84f0..adca184a 100644 --- a/src/h1/xsk/decompiler.cpp +++ b/src/h1/xsk/decompiler.cpp @@ -2504,12 +2504,13 @@ void decompiler::decompile_switch(const ast::stmt_list::ptr& stmt, std::size_t s end = find_location_index(stmt, end_loc); // update end - // fix empty cases at end - if (stmt->list.at(end) == ast::kind::asm_endswitch) - end--; + while (stmt->list.size() > (end + 1) && stmt->list.at(end) != ast::kind::asm_endswitch) + end++; - // TODO: fix more than 1 empty case at end + if (stmt->list.at(end) != ast::kind::asm_endswitch) + decomp_error("bad empty cases in switch block!"); + end--; stmt->list.erase(stmt->list.begin() + start); // remove 'switch' stmt->list.erase(stmt->list.begin() + end); // remove 'endswitch' diff --git a/src/h2/xsk/decompiler.cpp b/src/h2/xsk/decompiler.cpp index 17ca2ccd..7abd36b5 100644 --- a/src/h2/xsk/decompiler.cpp +++ b/src/h2/xsk/decompiler.cpp @@ -2504,12 +2504,13 @@ void decompiler::decompile_switch(const ast::stmt_list::ptr& stmt, std::size_t s end = find_location_index(stmt, end_loc); // update end - // fix empty cases at end - if (stmt->list.at(end) == ast::kind::asm_endswitch) - end--; + while (stmt->list.size() > (end + 1) && stmt->list.at(end) != ast::kind::asm_endswitch) + end++; - // TODO: fix more than 1 empty case at end + if (stmt->list.at(end) != ast::kind::asm_endswitch) + decomp_error("bad empty cases in switch block!"); + end--; stmt->list.erase(stmt->list.begin() + start); // remove 'switch' stmt->list.erase(stmt->list.begin() + end); // remove 'endswitch' diff --git a/src/iw5/xsk/decompiler.cpp b/src/iw5/xsk/decompiler.cpp index 762b97d6..bbe8de0c 100644 --- a/src/iw5/xsk/decompiler.cpp +++ b/src/iw5/xsk/decompiler.cpp @@ -2498,12 +2498,13 @@ void decompiler::decompile_switch(const ast::stmt_list::ptr& stmt, std::size_t s end = find_location_index(stmt, end_loc); // update end - // fix empty cases at end - if (stmt->list.at(end) == ast::kind::asm_endswitch) - end--; + while (stmt->list.size() > (end + 1) && stmt->list.at(end) != ast::kind::asm_endswitch) + end++; - // TODO: fix more than 1 empty case at end + if (stmt->list.at(end) != ast::kind::asm_endswitch) + decomp_error("bad empty cases in switch block!"); + end--; stmt->list.erase(stmt->list.begin() + start); // remove 'switch' stmt->list.erase(stmt->list.begin() + end); // remove 'endswitch' diff --git a/src/iw6/xsk/decompiler.cpp b/src/iw6/xsk/decompiler.cpp index 92893bf4..f41e93d5 100644 --- a/src/iw6/xsk/decompiler.cpp +++ b/src/iw6/xsk/decompiler.cpp @@ -2498,12 +2498,13 @@ void decompiler::decompile_switch(const ast::stmt_list::ptr& stmt, std::size_t s end = find_location_index(stmt, end_loc); // update end - // fix empty cases at end - if (stmt->list.at(end) == ast::kind::asm_endswitch) - end--; + while (stmt->list.size() > (end + 1) && stmt->list.at(end) != ast::kind::asm_endswitch) + end++; - // TODO: fix more than 1 empty case at end + if (stmt->list.at(end) != ast::kind::asm_endswitch) + decomp_error("bad empty cases in switch block!"); + end--; stmt->list.erase(stmt->list.begin() + start); // remove 'switch' stmt->list.erase(stmt->list.begin() + end); // remove 'endswitch' diff --git a/src/iw7/xsk/decompiler.cpp b/src/iw7/xsk/decompiler.cpp index aa195804..37f1b6b6 100644 --- a/src/iw7/xsk/decompiler.cpp +++ b/src/iw7/xsk/decompiler.cpp @@ -2498,12 +2498,13 @@ void decompiler::decompile_switch(const ast::stmt_list::ptr& stmt, std::size_t s end = find_location_index(stmt, end_loc); // update end - // fix empty cases at end - if (stmt->list.at(end) == ast::kind::asm_endswitch) - end--; + while (stmt->list.size() > (end + 1) && stmt->list.at(end) != ast::kind::asm_endswitch) + end++; - // TODO: fix more than 1 empty case at end + if (stmt->list.at(end) != ast::kind::asm_endswitch) + decomp_error("bad empty cases in switch block!"); + end--; stmt->list.erase(stmt->list.begin() + start); // remove 'switch' stmt->list.erase(stmt->list.begin() + end); // remove 'endswitch' diff --git a/src/iw8/xsk/decompiler.cpp b/src/iw8/xsk/decompiler.cpp index 8a254f37..2c0d545f 100644 --- a/src/iw8/xsk/decompiler.cpp +++ b/src/iw8/xsk/decompiler.cpp @@ -2535,12 +2535,13 @@ void decompiler::decompile_switch(const ast::stmt_list::ptr& stmt, std::size_t s end = find_location_index(stmt, end_loc); // update end - // fix empty cases at end - if (stmt->list.at(end) == ast::kind::asm_endswitch) - end--; + while (stmt->list.size() > (end + 1) && stmt->list.at(end) != ast::kind::asm_endswitch) + end++; - // TODO: fix more than 1 empty case at end + if (stmt->list.at(end) != ast::kind::asm_endswitch) + decomp_error("bad empty cases in switch block!"); + end--; stmt->list.erase(stmt->list.begin() + start); // remove 'switch' stmt->list.erase(stmt->list.begin() + end); // remove 'endswitch' diff --git a/src/s1/xsk/decompiler.cpp b/src/s1/xsk/decompiler.cpp index 221581c6..5f491ff9 100644 --- a/src/s1/xsk/decompiler.cpp +++ b/src/s1/xsk/decompiler.cpp @@ -2504,12 +2504,13 @@ void decompiler::decompile_switch(const ast::stmt_list::ptr& stmt, std::size_t s end = find_location_index(stmt, end_loc); // update end - // fix empty cases at end - if (stmt->list.at(end) == ast::kind::asm_endswitch) - end--; + while (stmt->list.size() > (end + 1) && stmt->list.at(end) != ast::kind::asm_endswitch) + end++; - // TODO: fix more than 1 empty case at end + if (stmt->list.at(end) != ast::kind::asm_endswitch) + decomp_error("bad empty cases in switch block!"); + end--; stmt->list.erase(stmt->list.begin() + start); // remove 'switch' stmt->list.erase(stmt->list.begin() + end); // remove 'endswitch' diff --git a/src/s2/xsk/decompiler.cpp b/src/s2/xsk/decompiler.cpp index 426cf841..d7e45cc4 100644 --- a/src/s2/xsk/decompiler.cpp +++ b/src/s2/xsk/decompiler.cpp @@ -2511,12 +2511,13 @@ void decompiler::decompile_switch(const ast::stmt_list::ptr& stmt, std::size_t s end = find_location_index(stmt, end_loc); // update end - // fix empty cases at end - if (stmt->list.at(end) == ast::kind::asm_endswitch) - end--; + while (stmt->list.size() > (end + 1) && stmt->list.at(end) != ast::kind::asm_endswitch) + end++; - // TODO: fix more than 1 empty case at end + if (stmt->list.at(end) != ast::kind::asm_endswitch) + decomp_error("bad empty cases in switch block!"); + end--; stmt->list.erase(stmt->list.begin() + start); // remove 'switch' stmt->list.erase(stmt->list.begin() + end); // remove 'endswitch' diff --git a/src/s4/xsk/decompiler.cpp b/src/s4/xsk/decompiler.cpp index bcb9f59f..5413ad19 100644 --- a/src/s4/xsk/decompiler.cpp +++ b/src/s4/xsk/decompiler.cpp @@ -2535,12 +2535,13 @@ void decompiler::decompile_switch(const ast::stmt_list::ptr& stmt, std::size_t s end = find_location_index(stmt, end_loc); // update end - // fix empty cases at end - if (stmt->list.at(end) == ast::kind::asm_endswitch) - end--; + while (stmt->list.size() > (end + 1) && stmt->list.at(end) != ast::kind::asm_endswitch) + end++; - // TODO: fix more than 1 empty case at end + if (stmt->list.at(end) != ast::kind::asm_endswitch) + decomp_error("bad empty cases in switch block!"); + end--; stmt->list.erase(stmt->list.begin() + start); // remove 'switch' stmt->list.erase(stmt->list.begin() + end); // remove 'endswitch' diff --git a/src/t6/xsk/decompiler.cpp b/src/t6/xsk/decompiler.cpp index 9039b713..7401b6a4 100644 --- a/src/t6/xsk/decompiler.cpp +++ b/src/t6/xsk/decompiler.cpp @@ -1915,10 +1915,13 @@ void decompiler::decompile_switch(const ast::stmt_list::ptr& stmt, std::size_t s end = find_location_index(stmt, end_loc); // update end - // fix empty cases at end - if (stmt->list.at(end) == ast::kind::asm_endswitch) - end--; + while (stmt->list.size() > (end + 1) && stmt->list.at(end) != ast::kind::asm_endswitch) + end++; + if (stmt->list.at(end) != ast::kind::asm_endswitch) + decomp_error("bad empty cases in switch block!"); + + end--; stmt->list.erase(stmt->list.begin() + start); // remove 'switch' stmt->list.erase(stmt->list.begin() + end); // remove 'endswitch'