gbranden pushed a commit to branch master
in repository groff.

commit a43453c9663ff215128efdaaa60ea4c2ee717d36
Author: G. Branden Robinson <[email protected]>
AuthorDate: Thu Jun 11 11:25:33 2026 -0500

    [grops]: Slightly refactor. (2/2)
    
    * src/devices/grops/psrm.cpp: Use ISO C++98 exceptions to handle heap
      storage allocation failures.  Preprocessor-include C++ "<new>" header
      file.
    
    * src/devices/grops/psrm.cpp:
      (resource_manager::lookup_resource)
      (resource_manager::lookup_font)
      (resource_manager::document_setup):
      Catch `std:bad_alloc` exception and `fatal()` out with an attempt to
      describe what we were doing.
    
    * src/devices/grops/psrm.cpp (resource_manager::document_setup):
      Trivially refactor to lift a repeated expression into a local
      variable, `count`.  retype local variable `i` from `int` to `size_t`
      for signedness-correct comparisons.
    
    Continues the long process of fixing Savannah #68192.
---
 ChangeLog                  | 18 ++++++++++++++----
 src/devices/grops/psrm.cpp | 47 +++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 54 insertions(+), 11 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 017b7afed..b9e4139de 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,14 +2,24 @@
 
        [grops]: Slightly refactor.
 
-       * src/devices/grops/ps.cpp: Use ISO C++98 exceptions to handle
+       * src/devices/grops/ps.cpp:
+       * src/devices/grops/psrm.cpp: Use ISO C++98 exceptions to handle
        heap storage allocation failures.  Preprocessor-include C++
        "<new>" header file.
+       * src/devices/grops/ps.cpp:
        (ps_font::load_ps_font, ps_printer::set_subencoding)
        (ps_printer::get_subfont, ps_printer::define_encoding)
-       (ps_printer::encode_fonts, ps_printer::set_style): Catch
-       `std:bad_alloc` exception and `fatal()` out with an attempt to
-       describe what we were doing.
+       (ps_printer::encode_fonts, ps_printer::set_style):
+       * src/devices/grops/psrm.cpp:
+       (resource_manager::lookup_resource)
+       (resource_manager::lookup_font)
+       (resource_manager::document_setup):
+       Catch `std:bad_alloc` exception and `fatal()` out with an
+       attempt to describe what we were doing.
+
+       * src/devices/grops/psrm.cpp (resource_manager::document_setup):
+       Trivially refactor to lift a repeated expression into a local
+       variable, `count`.
 
        Continues the long process of fixing Savannah #68192.
 
diff --git a/src/devices/grops/psrm.cpp b/src/devices/grops/psrm.cpp
index d199935d6..6cc67ca36 100644
--- a/src/devices/grops/psrm.cpp
+++ b/src/devices/grops/psrm.cpp
@@ -27,6 +27,8 @@ along with this program.  If not, see 
<http://www.gnu.org/licenses/>. */
 #include <stdlib.h> // getenv(), setenv(), strtoul()
 #include <string.h> // strerror(), strtok()
 
+#include <new> // std::bad_alloc
+
 #include "cset.h"
 #include "driver.h"
 #include "lib.h" // strsave()
@@ -186,7 +188,15 @@ resource *resource_manager::lookup_resource(resource_type 
type,
        && r->version == version
        && r->revision == revision)
       return r;
-  r = new resource(type, name, version, revision);
+  r = 0 /* nullptr */;
+  try {
+    r = new resource(type, name, version, revision);
+  }
+  catch (const std::bad_alloc &e) {
+    fatal("cannot allocate %1 bytes for storage of PostScript resource"
+         " '%2', version '%3'", sizeof(resource), name.contents(),
+         version.contents());
+  }
   r->next = resource_list;
   resource_list = r;
   return r;
@@ -203,7 +213,14 @@ resource *resource_manager::lookup_font(const char *name)
        && memcmp(name, r->name.contents(), r->name.length()) == 0)
       return r;
   string s(name);
-  r = new resource(RESOURCE_FONT, s);
+  r = 0 /* nullptr */;
+  try {
+    r = new resource(RESOURCE_FONT, s);
+  }
+  catch (const std::bad_alloc &e) {
+    fatal("cannot allocate %1 bytes for storage of PostScript font"
+         " resource '%2'", sizeof(resource), s.contents());
+  }
   r->next = resource_list;
   resource_list = r;
   return r;
@@ -214,6 +231,7 @@ void resource_manager::need_font(const char *name)
   lookup_font(name)->flags |= resource::FONT_NEEDED;
 }
 
+// XXX: This comment dates to 1991.  Discard the workaround.
 typedef resource *Presource;   // Work around g++ bug.
 
 void resource_manager::document_setup(ps_output &out)
@@ -225,10 +243,25 @@ void resource_manager::document_setup(ps_output &out)
       nranks = r->rank + 1;
   if (nranks > 0) {
     // Sort resource_list in reverse order of rank.
-    Presource *head = new Presource[nranks + 1];
-    Presource **tail = new Presource *[nranks + 1];
-    int i;
-    for (i = 0; i < nranks + 1; i++) {
+    Presource *head = 0 /* nullptr */;
+    size_t count = nranks + 1;
+    try {
+      head = new Presource[count];
+    }
+    catch (const std::bad_alloc &e) {
+      fatal("cannot allocate %1 bytes for storage of %2 pointers",
+           (count * sizeof(Presource)), count);
+    }
+    Presource **tail = 0 /* nullptr */;
+    try {
+      tail = new Presource *[count];
+    }
+    catch (const std::bad_alloc &e) {
+      fatal("cannot allocate %1 bytes for storage of %2 pointers",
+           (count * sizeof(Presource)), count);
+    }
+    size_t i;
+    for (i = 0; i < count; i++) {
       head[i] = 0;
       tail[i] = &head[i];
     }
@@ -238,7 +271,7 @@ void resource_manager::document_setup(ps_output &out)
       tail[i] = &(*tail[i])->next;
     }
     resource_list = 0;
-    for (i = 0; i < nranks + 1; i++)
+    for (i = 0; i < count; i++)
       if (head[i]) {
        *tail[i] = resource_list;
        resource_list = head[i];

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

Reply via email to