gbranden pushed a commit to branch master
in repository groff.

commit 306047e880015b746c2f8dd4d736c3530a2d6738
Author: G. Branden Robinson <[email protected]>
AuthorDate: Sun May 31 21:06:31 2026 -0500

    [grohtml]: Refactor. (1/2)
    
    Be more fastidious with heap-allocated memory.
    
    * src/devices/grohtml/html-table.cpp: Use ISO C++98 exceptions to handle
      heap storage allocation failures.  Preprocessor-include C++ "<new>"
      header file and project-local "errarg.h" and "error.h" header files.
    
      (tabs::init): Slightly refactor.  Introduce new local variable
      `newtab` (a pointer to `tab_position`, lifting 2 identical attempted
      memory allocations out of both branches of an if/else.
    
      (tabs::init):
      (html_table::html_table):
      (html_table::insert_column):
      (html_indent::html_indent): Catch `std::bad_alloc` exception and
      `fatal()` out with an attempt to describe what we were doing.
    
    Continues the long process of fixing Savannah #68192.
---
 ChangeLog                          | 19 +++++++++++++++++++
 src/devices/grohtml/html-table.cpp | 38 ++++++++++++++++++++++++++++++++------
 2 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e9d5ca8c9..245180ecb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,22 @@
+2026-05-31  G. Branden Robinson <[email protected]>
+
+       [grohtml]: Be more fastidious with heap-allocated memory.
+
+       * src/devices/grohtml/html-table.cpp: Use ISO C++98 exceptions
+       to handle heap storage allocation failures.  Preprocessor-
+       include C++ "<new>" header file and project-local "errarg.h" and
+       "error.h" header files.
+       (tabs::init): Slightly refactor.  Introduce new local variable
+       `newtab` (a pointer to `tab_position`, lifting 2 identical
+       attempted memory allocations out of both branches of an if/else.
+       (tabs::init):
+       (html_table::html_table):
+       (html_table::insert_column):
+       (html_indent::html_indent): Catch `std::bad_alloc` exception and
+       `fatal()` out with an attempt to describe what we were doing.
+
+       Continues the long process of fixing Savannah #68192.
+
 2026-06-06  G. Branden Robinson <[email protected]>
 
        * src/devices/grohtml/post-html.cpp
diff --git a/src/devices/grohtml/html-table.cpp 
b/src/devices/grohtml/html-table.cpp
index 0817e77cf..ae4a12139 100644
--- a/src/devices/grohtml/html-table.cpp
+++ b/src/devices/grohtml/html-table.cpp
@@ -30,10 +30,14 @@ with this program.  If not, see 
<http://www.gnu.org/licenses/>. */
 
 #include <stdlib.h> // atoi()
 
+#include <new> // std::bad_alloc
+
 // libgroff
 #include "symbol.h" // prerequisite of color.h
 #include "color.h"
 #include "cset.h" // csspace()
+#include "errarg.h" // prerequisite of error.h
+#include "error.h" // fatal()
 #include "stringclass.h"
 
 // grohtml
@@ -159,11 +163,18 @@ void tabs::init (const char *s)
     // move over tab position
     while ((*s != '\0') && !csspace(*s))
       s++;
-    if (last == NULL) {
-      tab = new tab_position;
+    tab_position *newtab = 0 /* nullptr */;
+    try {
+      newtab = new tab_position;
+    }
+    catch (const std::bad_alloc &exc) {
+      fatal("cannot allocate storage for tab_position object");
+    }
+    if (last == 0 /* nullptr */) {
+      tab = newtab;
       last = tab;
     } else {
-      last->next = new tab_position;
+      last->next = newtab;
       last = last->next;
     }
     last->alignment = align;
@@ -252,7 +263,12 @@ html_table::html_table (simple_output *op, int linelen)
   : out(op), columns(0 /* nullptr */), linelength(linelen),
     last_col(0 /* nullptr */), start_space(FALSE)
 {
-  tab_stops = new tabs();
+  try {
+    tab_stops = new tabs();
+  }
+  catch (const std::bad_alloc &exc) {
+    fatal("cannot allocate storage for tab_stops object");
+  }
 }
 
 html_table::~html_table ()
@@ -645,7 +661,12 @@ int html_table::insert_column (int coln, int hstart, int 
hend, char align)
       (l->next->left < hend))
     return FALSE;  // new column bumps into next one
 
-  n = new cols;
+  try {
+    n = new cols;
+  }
+  catch (const std::bad_alloc &exc) {
+    fatal("cannot allocate storage for cols object");
+  }
   if (l == 0 /* nullptr */) {
     n->next = columns;
     columns = n;
@@ -801,7 +822,12 @@ void html_table::dump_table (void)
 
 html_indent::html_indent (simple_output *op, int ind, int pageoffset, int 
linelength)
 {
-  table = new html_table(op, linelength);
+  try {
+    table = new html_table(op, linelength);
+  }
+  catch (const std::bad_alloc &exc) {
+    fatal("cannot allocate storage for html_table object");
+  }
 
   table->add_column(1, ind+pageoffset, linelength, 'L');
   table->add_indent(pageoffset);

_______________________________________________
groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit

Reply via email to