From d25a61e9e65497c55c1a242c8e709c1a7b908f20 Mon Sep 17 00:00:00 2001
From: Akash Mozumdar <akashmozumdar@gmail.com>
Date: Sun, 9 Jun 2019 07:33:26 -0400
Subject: [PATCH] catch garbage pipe data

---
 GUI/host/host.cpp          | 3 ++-
 GUI/mainwindow.cpp         | 2 +-
 extensions/regexfilter.cpp | 2 +-
 include/common.h           | 2 +-
 texthook/hookfinder.cc     | 2 +-
 5 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/GUI/host/host.cpp b/GUI/host/host.cpp
index a65517b..275219a 100644
--- a/GUI/host/host.cpp
+++ b/GUI/host/host.cpp
@@ -134,11 +134,12 @@ namespace
 				{
 					auto tp = *(ThreadParam*)buffer;
 					auto textThreadsByParams = ::textThreadsByParams.Acquire();
-					if (textThreadsByParams->count(tp) == 0)
+					if (textThreadsByParams->count(tp) == 0) try
 					{
 						TextThread& created = textThreadsByParams->try_emplace(tp, tp, Host::GetHookParam(tp)).first->second;
 						OnCreate(created);
 					}
+					catch (std::out_of_range) { continue; } // probably garbage data in pipe, try again
 					textThreadsByParams->find(tp)->second.Push(buffer + sizeof(tp), bytesRead - sizeof(tp));
 				}
 				break;
diff --git a/GUI/mainwindow.cpp b/GUI/mainwindow.cpp
index 42b35ee..d1c372b 100644
--- a/GUI/mainwindow.cpp
+++ b/GUI/mainwindow.cpp
@@ -375,7 +375,7 @@ void MainWindow::FindHooks()
 				QByteArray pattern = QByteArray::fromHex(patternInput->text().replace("??", "11").toUtf8());
 				if (pattern.size() < 3) return;
 				std::wregex filter(L".");
-				if (!filterInput->text().isEmpty()) try { filter = std::wregex(S(filterInput->text())); } catch (std::regex_error&) {};
+				if (!filterInput->text().isEmpty()) try { filter = std::wregex(S(filterInput->text())); } catch (std::regex_error) {};
 				memcpy(sp.pattern, pattern.data(), sp.length = min(pattern.size(), 25));
 				auto hooks = std::make_shared<QString>();
 				Host::FindHooks(processId, sp, [hooks, filter](HookParam hp, DWORD processId, const std::wstring& text)
diff --git a/extensions/regexfilter.cpp b/extensions/regexfilter.cpp
index 5890020..4fb0825 100644
--- a/extensions/regexfilter.cpp
+++ b/extensions/regexfilter.cpp
@@ -18,7 +18,7 @@ struct : QMainWindow
 		{
 			std::lock_guard l(m);
 			try { regex = newRegex.toStdWString(); }
-			catch (...) { return ui.output->setText(INVALID_REGEX); }
+			catch (std::regex_error) { return ui.output->setText(INVALID_REGEX); }
 			ui.output->setText(QString(CURRENT_FILTER).arg(newRegex));
 		});
 		setWindowTitle(REGEX_FILTER);
diff --git a/include/common.h b/include/common.h
index 4a52cc2..a183d40 100644
--- a/include/common.h
+++ b/include/common.h
@@ -74,7 +74,7 @@ private:
 	std::unique_ptr<void, HandleCleaner> h;
 };
 
-inline struct
+static struct
 {
 	BYTE DUMMY[100];
 	template <typename T> 
diff --git a/texthook/hookfinder.cc b/texthook/hookfinder.cc
index 53ce36f..c6814d2 100644
--- a/texthook/hookfinder.cc
+++ b/texthook/hookfinder.cc
@@ -160,7 +160,7 @@ void SearchForHooks(SearchParam sp)
 		std::scoped_lock lock(m);
 
 		try { records = std::make_unique<HookRecord[]>(recordsAvailable = CACHE_SIZE); }
-		catch (std::bad_alloc&) { return ConsoleOutput("Textractor: SearchForHooks ERROR (out of memory)"); }
+		catch (std::bad_alloc) { return ConsoleOutput("Textractor: SearchForHooks ERROR (out of memory)"); }
 
 		current = sp;