Replace direct abort() calls with fatal() in String methods

Thu, 23 May 2013 17:37:25 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Thu, 23 May 2013 17:37:25 +0300
changeset 247
1a2ca515f683
parent 246
56226ddd4ad3
child 248
4431371f3ffe

Replace direct abort() calls with fatal() in String methods

src/main.cpp file | annotate | diff | comparison | revisions
src/string.cpp file | annotate | diff | comparison | revisions
--- a/src/main.cpp	Wed May 22 23:33:49 2013 +0300
+++ b/src/main.cpp	Thu May 23 17:37:25 2013 +0300
@@ -134,7 +134,7 @@
 }
 
 void assertionFailure (const char* file, const ulong line, const char* funcname, const char* expr) {
-	str errmsg = fmt ("File %s:%lu:\nFunction %s:\n\nAssertion `%s' failed",
+	str errmsg = fmt ("File %s\nLine: %lu:\nFunction %s:\n\nAssertion `%s' failed",
 		file, line, funcname, expr);
 	
 #if BUILD_ID == BUILD_INTERNAL
@@ -155,7 +155,7 @@
 }
 
 void fatalError (const char* file, const ulong line, const char* funcname, str msg) {
-	str errmsg = fmt ("fatal() called:\nFile: %s\nLine: %lu\nFunction: %s\n\n%s",
+	str errmsg = fmt ("Aborting over a call to fatal():\nFile: %s\nLine: %lu\nFunction: %s\n\n%s",
 		file, line, funcname, msg.chars ());
 	
 	printf ("%s\n", errmsg.chars ());
--- a/src/string.cpp	Wed May 22 23:33:49 2013 +0300
+++ b/src/string.cpp	Thu May 23 17:37:25 2013 +0300
@@ -40,8 +40,11 @@
 		try {
 			buf = new char[size];
 		} catch (std::bad_alloc&) {
-			fprintf (stderr, "%s: allocation error on run #%u: tried to allocate %lu bytes\n", __func__, run + 1, size);
-			abort ();
+			// fmt uses dynafmt, so using fmt here is dangerous and could lead
+			// into infinite recursion. Thus, use a C string this one time.
+			char err[256];
+			sprintf (err, "caught std::bad_alloc on run #%u while trying to allocate %lu bytes", run + 1, size);
+			fatal (err);
 		}
 		
 		if (!vsnprintf (buf, size - 1, fmtstr, va)) {
@@ -165,9 +168,8 @@
 	try {
 		sub = m_string.substr (a, b - a);
 	} catch (const std::out_of_range& e) {
-		printf ("%s: %s: caught std::out_of_range, coords were: (%ld, %ld), string: `%s', length: %lu\n",
-			__func__, e.what (), a, b, chars (), (ulong) len ());
-		abort ();
+		fatal (fmt ("caught std::out_of_range, coords were: (%ld, %ld), string: `%s', length: %lu",
+			a, b, chars (), (ulong) len ()));
 	}
 	
 	return sub;

mercurial