From 802eb6032396c4c30892ed060acb99cb597e0b2a Mon Sep 17 00:00:00 2001
From: "Stephen R. van den Berg" <srb@cuci.nl>
Date: Sat, 1 May 2021 14:49:19 +0200
Subject: [PATCH] Thread: Protect against exceptions in backtraces.

---
 lib/modules/Thread.pmod | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/lib/modules/Thread.pmod b/lib/modules/Thread.pmod
index c0fdc25110..d13cf163df 100644
--- a/lib/modules/Thread.pmod
+++ b/lib/modules/Thread.pmod
@@ -239,6 +239,8 @@ optional class Fifo {
 
   protected string _sprintf( int f )
   {
+    if (!this)				// Only if not destructed
+      return UNDEFINED;
     return f=='O' && sprintf( "%O(%d / %d)", this_program,
 			      size(), read_tres );
   }
@@ -407,6 +409,8 @@ optional class Queue {
 
   protected string _sprintf( int f )
   {
+    if (!this)				// Only if not destructed
+      return UNDEFINED;
     return f=='O' && sprintf( "%O(%d)", this_program, size() );
   }
 }
@@ -506,6 +510,8 @@ optional class Farm
 
     protected string _sprintf( int f )
     {
+      if (!this)				// Only if not destructed
+        return UNDEFINED;
       switch( f )
       {
 	case 't':
@@ -620,6 +626,8 @@ optional class Farm
 
     protected string _sprintf( int f )
     {
+      if (!this)				// Only if not destructed
+        return UNDEFINED;
       switch( f )
       {
 	case 't':
@@ -864,6 +872,8 @@ optional class Farm
 
   protected string _sprintf( int f )
   {
+    if (!this)				// Only if not destructed
+      return UNDEFINED;
     return f=='O' && sprintf( "%O(/* %s */)", this_program, debug_status() );
   }
 
@@ -939,6 +949,8 @@ optional class ResourceCount {
 
   /*semi*/private string _sprintf(int type) {
     string res = UNDEFINED;
+    if (!this)				// Only if not destructed
+      return UNDEFINED;
     switch(type) {
       case 'O':
         res = sprintf("Count: %d", _count);
@@ -1139,6 +1151,8 @@ optional class Fifo
 
   protected string _sprintf( int f )
   {
+    if (!this)				// Only if not destructed
+      return UNDEFINED;
     return f=='O' && sprintf( "%O(%d / %d)", this_program,
 			      size(), read_tres );
   }
@@ -1217,6 +1231,8 @@ optional class Queue
 
   protected string _sprintf( int f )
   {
+    if (!this)				// Only if not destructed
+      return UNDEFINED;
     return f=='O' && sprintf( "%O(%d)", this_program, size() );
   }
 }
@@ -1263,6 +1279,8 @@ class ResourceCount {
   protected string _sprintf(int type)
   {
     string res = UNDEFINED;
+    if (!this)				// Only if not destructed
+      return UNDEFINED;
     switch(type) {
       case 'O':
         res = sprintf("Count: %d", _count);
-- 
GitLab