[vte] widget: Rework exception handling in non-debug builds



commit 6460463b258d69ae12e5e9db09e9414403e136b5
Author: Christian Persch <chpe src gnome org>
Date:   Sat Aug 14 21:45:46 2021 +0200

    widget: Rework exception handling in non-debug builds
    
    Always make bad_alloc fatal.

 src/cxx-utils.hh |  2 +-
 src/vtegtk.cc    | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)
---
diff --git a/src/cxx-utils.hh b/src/cxx-utils.hh
index af4580f4..3ce6d046 100644
--- a/src/cxx-utils.hh
+++ b/src/cxx-utils.hh
@@ -51,7 +51,7 @@ void log_exception(char const* func = __builtin_FUNCTION(),
                    char const* filename = __builtin_FILE(),
                    int const line = __builtin_LINE()) noexcept;
 #else
-inline void log_exception() noexcept { }
+inline void log_exception() noexcept;
 #endif
 
 template <typename T, typename D, D func>
diff --git a/src/vtegtk.cc b/src/vtegtk.cc
index b35590a9..8d17e666 100644
--- a/src/vtegtk.cc
+++ b/src/vtegtk.cc
@@ -6313,6 +6313,8 @@ exception_append_to_string(std::exception const& e,
 
         try {
                 std::rethrow_if_nested(e);
+        } catch (std::bad_alloc const& en) {
+                g_error("Allocation failure: %s\n", what.c_str());
         } catch (std::exception const& en) {
                 exception_append_to_string(en, what, level + 1);
         } catch (...) {
@@ -6321,6 +6323,7 @@ exception_append_to_string(std::exception const& e,
 }
 
 #ifdef VTE_DEBUG
+
 void log_exception(char const* func,
                    char const* filename,
                    int const line) noexcept
@@ -6348,6 +6351,39 @@ catch (...)
                          "Caught exception while logging an exception in %s [%s:%d]\n",
                          func, filename, line);
 }
+
+#else
+
+static void
+log_exception(std::exception const& e)
+{
+        try {
+                std::rethrow_if_nested(e);
+        } catch (std::bad_alloc const& en) {
+                g_error("Allocation failure: %s\n", e.what());
+        } catch (std::exception const& en) {
+                log_exception(en);
+        } catch (...) {
+        }
+}
+
+void
+log_exception() noexcept
+try
+{
+        try {
+                throw; // rethrow current exception
+        } catch (std::bad_alloc const& e) {
+                g_error("Allocation failure: %s\n", e.what());
+        } catch (std::exception const& e) {
+                log_exception(e);
+        } catch (...) {
+        }
+}
+catch (...)
+{
+}
+
 #endif /* VTE_DEBUG */
 
 namespace glib {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]