From c644491f9f7efaded6bb0a3b9550d1574ec98894 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sat, 25 Jul 2020 21:06:39 +0300
Subject: [PATCH] Fix for memory corruption in thumbnails

---
 CMakeLists.txt                                  | 2 +-
 applications/gui2/src/screen.cpp                | 1 +
 applications/gui2/src/views/thumbnails.cpp      | 4 ++--
 components/common/cpp/include/ftl/exception.hpp | 2 +-
 components/net/cpp/src/universe.cpp             | 7 +++++++
 5 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 475443500..d2b26c52e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -398,7 +398,7 @@ else()
 	add_definitions(-DUNIX)
 	# -fdiagnostics-color
 	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -fPIC -msse3 -Wall -Werror=unused-result -Werror=return-type")
-	set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_DEBUG -pg")
+	set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -pg")
 	set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -mfpmath=sse")
 	set(OS_LIBS "dl")
 endif()
diff --git a/applications/gui2/src/screen.cpp b/applications/gui2/src/screen.cpp
index fe7747306..6fbd703b8 100644
--- a/applications/gui2/src/screen.cpp
+++ b/applications/gui2/src/screen.cpp
@@ -107,6 +107,7 @@ nanogui::Vector2i Screen::viewSize() {
 
 
 void Screen::showError(const std::string&title, const std::string& msg) {
+	// FIXME: This isn't thread safe?
 	if (msgerror_) { return; }
 	msgerror_ = new nanogui::MessageDialog
 		(screen(), nanogui::MessageDialog::Type::Warning, title, msg);
diff --git a/applications/gui2/src/views/thumbnails.cpp b/applications/gui2/src/views/thumbnails.cpp
index a12e7cfd7..78b1b86f1 100644
--- a/applications/gui2/src/views/thumbnails.cpp
+++ b/applications/gui2/src/views/thumbnails.cpp
@@ -133,8 +133,6 @@ Thumbnails::~Thumbnails() {
 }
 
 bool Thumbnails::mouseButtonEvent(const nanogui::Vector2i &p, int button, bool down, int modifiers) {
-	bool r = View::mouseButtonEvent(p, button, down, modifiers);
-
 	if (button == 1) {
 		if (!down) {
 			context_menu_->setPosition(p - mPos);
@@ -144,6 +142,8 @@ bool Thumbnails::mouseButtonEvent(const nanogui::Vector2i &p, int button, bool d
 	} else {
 		context_menu_->setVisible(false);
 	}
+
+	bool r = View::mouseButtonEvent(p, button, down, modifiers);
 	return r;
 }
 
diff --git a/components/common/cpp/include/ftl/exception.hpp b/components/common/cpp/include/ftl/exception.hpp
index 43e303d9b..e78fe3854 100644
--- a/components/common/cpp/include/ftl/exception.hpp
+++ b/components/common/cpp/include/ftl/exception.hpp
@@ -65,6 +65,6 @@ class exception : public std::exception
 
 }
 
-#define FTL_Error(A) (ftl::exception(ftl::Formatter() << __FILE__ << ":" << __LINE__ << ": " << A))
+#define FTL_Error(A) (ftl::exception(ftl::Formatter() << A << " [" << __FILE__ << ":" << __LINE__ << "]"))
 
 #endif  // _FTL_EXCEPTION_HPP_
diff --git a/components/net/cpp/src/universe.cpp b/components/net/cpp/src/universe.cpp
index 4fdba608f..558619602 100644
--- a/components/net/cpp/src/universe.cpp
+++ b/components/net/cpp/src/universe.cpp
@@ -260,6 +260,13 @@ void Universe::_cleanupPeers() {
 			auto ix = peer_ids_.find(p->id());
 			if (ix != peer_ids_.end()) peer_ids_.erase(ix);
 
+			for (auto i=peer_by_uri_.begin(); i != peer_by_uri_.end(); ++i) {
+				if (i->second == p) {
+					peer_by_uri_.erase(i);
+					break;
+				}
+			}
+
 			i = peers_.erase(i);
 
 			if (p->status() == ftl::net::Peer::kReconnecting) {
-- 
GitLab