From 2b951e3f61b8b4e091031173705922632bba3b5e Mon Sep 17 00:00:00 2001
From: mpc <mpc>
Date: Thu, 1 Jul 2004 09:17:17 +0000
Subject: [PATCH] Change throws to asserts.  If any of this stuff happens it
 means a code logic error or a retarded computer, so throwing it is just a
 waste of time.

---
 apps/enclave/libsockthread/src/mutex.cpp  | 35 ++++++-----------
 apps/enclave/libsockthread/src/mutex.hpp  | 30 ++++++---------
 apps/enclave/libsockthread/src/socket.cpp |  7 ++--
 apps/enclave/libsockthread/src/socket.hpp | 20 +++++-----
 apps/enclave/libsockthread/src/thread.cpp | 41 ++++++++------------
 apps/enclave/libsockthread/src/thread.hpp | 46 ++++++++++-------------
 6 files changed, 72 insertions(+), 107 deletions(-)

diff --git a/apps/enclave/libsockthread/src/mutex.cpp b/apps/enclave/libsockthread/src/mutex.cpp
index 734a26e65e..af50a63432 100644
--- a/apps/enclave/libsockthread/src/mutex.cpp
+++ b/apps/enclave/libsockthread/src/mutex.cpp
@@ -40,14 +40,10 @@ Mutex::Mutex(void)
 {
 #ifdef WINTHREAD
 	mutex = CreateMutex(NULL, FALSE, NULL);
-	if (mutex == NULL) {
-		TCHAR str[80];
-		throw Mutex_error(win_strerror(str, sizeof str));
-	}
+	assert(mutex != NULL);
 #else
 	int rc = pthread_mutex_init(&mutex, NULL);
-	if (!rc)
-		throw Mutex_error(strerror(rc));
+	assert(!rc);
 #endif
 }
 
@@ -57,14 +53,11 @@ Mutex::Mutex(void)
 Mutex::~Mutex(void)
 {
 #ifdef WINTHREAD
-	if (!CloseHandle(mutex)) {
-		TCHAR str[80];
-		throw Mutex_error(win_strerror(str, sizeof str));  // TODO: log instead
-	}
+	BOOL rc = CloseHandle(mutex);
+	assert(!rc);
 #else
 	int rc = pthread_mutex_destroy(&mutex);
-	if (!rc)
-		throw Mutex_error(strerror(rc));  // TODO: log instead
+	assert(!rc);
 #endif
 }
 
@@ -74,14 +67,11 @@ Mutex::~Mutex(void)
 void Mutex::lock(void)
 {
 #ifdef WINTHREAD
-	if (WaitForSingleObject(mutex, INFINITE) == WAIT_FAILED) {
-		TCHAR str[80];
-		throw Mutex_error(win_strerror(str, sizeof str));
-	}
+	DWORD rc = WaitForSingleObject(mutex, INFINITE);
+	assert(rc != WAIT_FAILED);
 #else
 	int rc = pthread_mutex_lock(&mutex);
-	if (!rc)
-		throw Mutex_error(strerror(rc));
+	assert(!rc);
 #endif
 }
 
@@ -91,13 +81,10 @@ void Mutex::lock(void)
 void Mutex::unlock(void)
 {
 #ifdef WINTHREAD
-	if (!ReleaseMutex(mutex)) {
-		TCHAR str[80];
-		throw Mutex_error(win_strerror(str, sizeof str));
-	}
+	BOOL rc = ReleaseMutex(mutex);
+	assert(!rc);
 #else
 	int rc = pthread_mutex_unlock(&mutex);
-	if (!rc)
-		throw Mutex_error(strerror(rc));
+	assert(!rc);
 #endif
 }
diff --git a/apps/enclave/libsockthread/src/mutex.hpp b/apps/enclave/libsockthread/src/mutex.hpp
index 77ce065671..f28a99055d 100644
--- a/apps/enclave/libsockthread/src/mutex.hpp
+++ b/apps/enclave/libsockthread/src/mutex.hpp
@@ -34,26 +34,20 @@
 #define MUTEX_HPP
 
 namespace Libsockthread {
-
 	class Mutex {
-	public:
-		Mutex(void);  // throws Mutex_error
-		~Mutex(void);
-
-		void lock(void);  // throws Mutex_error
-		void unlock(void);  // throws Mutex_error
-	private:
-	#ifdef WINTHREAD
-		HANDLE mutex;
-	#else
-		pthread_mutex_t mutex;
-	#endif
+		public:
+			Mutex(void);
+			~Mutex(void);
+
+			void lock(void);
+			void unlock(void);
+		private:
+#ifdef WINTHREAD
+			HANDLE mutex;
+#else
+			pthread_mutex_t mutex;
+#endif
 	};
-
-	class Mutex_error : public runtime_error {
-		Mutex_error(const string& s) : runtime_error(s) { }
-	};
-
 }
 
 #endif  // MUTEX_HPP
diff --git a/apps/enclave/libsockthread/src/socket.cpp b/apps/enclave/libsockthread/src/socket.cpp
index a6e18afd2f..742f42a2df 100644
--- a/apps/enclave/libsockthread/src/socket.cpp
+++ b/apps/enclave/libsockthread/src/socket.cpp
@@ -40,7 +40,7 @@ Socket::Socket(int type)
 {
 #ifdef WINSOCK
 	winsock_startup();
-
+#endif
 }
 
 #ifdef WINSOCK
@@ -49,9 +49,8 @@ Socket::Socket(int type)
  */
 void Socket::winsock_cleanup(void)
 {
-	if (WSACleanup() == SOCKET_ERROR)
-		throw Socket_error("WSACleanup() failed (" +
-			winsock_strerror(WSAGetLastError()) + ")");  // TODO: log instead
+	int rc = WSACleanup();
+	assert(rc != SOCKET_ERROR);
 }
 
 /*
diff --git a/apps/enclave/libsockthread/src/socket.hpp b/apps/enclave/libsockthread/src/socket.hpp
index 4bb0812311..dae1a0fa8a 100644
--- a/apps/enclave/libsockthread/src/socket.hpp
+++ b/apps/enclave/libsockthread/src/socket.hpp
@@ -32,24 +32,24 @@
 #define SOCKET_HPP
 
 namespace Libsockthread {
-
 	class Socket {
-	public:
-		Socket(int type);  // throws Socket error
+		public:
+			Socket(int type);  // throws Socket error
 
-		void func(void);
-	private:
+			void func(void);
+		private:
 #ifdef WINSOCK
-		void winsock_cleanup(void);
-		void winsock_startup(void);  // throws Socket_error
-		const char* Socket::winsock_strerror(int code);
+			void winsock_cleanup(void);
+			void winsock_startup(void);  // throws Socket_error
+			const char* winsock_strerror(int code);
 #endif
 	};
 
 	class Socket_error : public runtime_error {
-		Socket_error(const string& s) : runtime_error(s) { }
+		public:
+			Socket_error(const string& s)
+				: runtime_error(s) { }
 	};
-
 }
 
 #endif  // MUTEX_HPP
diff --git a/apps/enclave/libsockthread/src/thread.cpp b/apps/enclave/libsockthread/src/thread.cpp
index 0e7ad9a68a..a97774c051 100644
--- a/apps/enclave/libsockthread/src/thread.cpp
+++ b/apps/enclave/libsockthread/src/thread.cpp
@@ -62,29 +62,26 @@ bool Thread::is_running(void)
 /*
  * Stops the thread
  * Generally NOT a good idea
- *
- * Returns: true if the thread was killed, or false if it was already dead
  */
-bool Thread::kill(void)
+void Thread::kill(void)
 {
 	running_m.lock();
+#ifndef NDEBUG
+	// make sure it as actually running first
 	if (!running) {
 		running_m.unlock();
-		return false;
+		assert(false);
 	}
+#endif
 #ifdef WINTHREAD
-	if (!TerminateThread(handle, 0)) {
-		TCHAR str[80];
-		throw Thread_error(win_strerror(str, sizeof str));  // TODO: log instead
-	}
+	BOOL rc = TerminateThread(handle, 0);
+	assert(!rc);
 #else
 	int rc = pthread_cancel(id);
-	if (!rc)
-		throw Thread_error(strerror(rc));  // TODO: log instead
+	assert(!rc);
 #endif
 	running = false;
 	running_m.unlock();
-	return true;
 }
 
 /*
@@ -92,25 +89,19 @@ bool Thread::kill(void)
  */
 void Thread::start(void)
 {
-	#ifndef NDEBUG
-		// check whether the thread is already running
-		running_m.lock();
-		assert(!running);
-		running_m.unlock();
-	#endif
+#ifndef NDEBUG
+	// check whether the thread is already running
+	running_m.lock();
+	assert(!running);
+	running_m.unlock();
+#endif
 	continue_m.lock();
 #ifdef WINTHREAD
 	handle = CreateThread(NULL, 0, &the_thread, this, 0, &id);
-	if (handle == NULL) {
-		TCHAR str[80];
-		throw Thread_error(win_strerror(str, sizeof str));
-	}
+	assert(handle != NULL);
 #else
 	int rc = pthread_create(&id, NULL, &the_thread, this);
-	if (!rc) {
-		continue_m.unlock();
-		throw Thread_error(strerror(rc));
-	}
+	assert(!rc);
 #endif
 	// Wait until `running' is set
 	running_m.lock();
diff --git a/apps/enclave/libsockthread/src/thread.hpp b/apps/enclave/libsockthread/src/thread.hpp
index f41addc1e0..22e06566e5 100644
--- a/apps/enclave/libsockthread/src/thread.hpp
+++ b/apps/enclave/libsockthread/src/thread.hpp
@@ -34,39 +34,33 @@
 #define THREAD_HPP
 
 namespace Libsockthread {
-
 	class Thread {
-	public:
-		Thread(void)  // throws Mutex_error (a mutex is created)
-			: retval(0), running(false) { }
-		virtual ~Thread(void)
-			{ kill(); }
+		public:
+			Thread(void)
+				: retval(0), running(false) { }
+			virtual ~Thread(void)
+				{ kill(); }
 
-		virtual void *execute(void) = 0;
-		void* get_retval(void);
-		bool is_running(void);
-		bool kill(void);  // throws Thread_error
-		void start(void);  // throws Thread_error
+			virtual void *execute(void) = 0;
+			void* get_retval(void);
+			bool is_running(void);
+			void kill(void);
+			void start(void);
 
-	private:
+		private:
 #ifdef WINTHREAD
-		static DWORD WINAPI the_thread(void* param);
-		HANDLE handle;
-		DWORD id;
+			static DWORD WINAPI the_thread(void* param);
+			HANDLE handle;
+			DWORD id;
 #else
-		static void* the_thread(void* param);
-		pthread_t id;
+			static void* the_thread(void* param);
+			pthread_t id;
 #endif
-		void *retval;
-		bool running;
-		Mutex running_m;
-		Mutex continue_m;
-	};
-
-	class Thread_error : public runtime_error {
-		Thread_error(const string& s) : runtime_error(s) { }
+			void *retval;
+			bool running;
+			Mutex running_m;
+			Mutex continue_m;
 	};
-
 }
 
 #endif  // THREAD_HPP
-- 
GitLab