gbranden pushed a commit to branch master
in repository groff.

commit dd08f60fd16ff206e93cffe111858682dc8052f8
Author: G. Branden Robinson <[email protected]>
AuthorDate: Mon May 25 04:05:51 2026 -0500

    [tfmtodit]: Be more fastidious with heap memory.
    
    * src/utils/tfmtodit/tfmtodit.cpp: Use ISO C++98 exceptions to handle
      heap storage allocation failures.  Preprocessor-include C++ "<new>"
      header file.
    
      (tfm::load): 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                       | 12 ++++++++++++
 src/utils/tfmtodit/tfmtodit.cpp | 42 ++++++++++++++++++++++++++++++++---------
 2 files changed, 45 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index bba9c7fa9..72ce77811 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2026-05-25  G. Branden Robinson <[email protected]>
+
+       [tfmtodit]: Be more fastidious with heap-allocated memory.
+
+       * src/utils/tfmtodit/tfmtodit.cpp: Use ISO C++98 exceptions to
+       handle heap storage allocation failures.  Preprocessor-include
+       C++ "<new>" header file.
+       (tfm::load): 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-05-25  G. Branden Robinson <[email protected]>
 
        [tfmtodit]: Be more fastidious with heap-allocated memory.
diff --git a/src/utils/tfmtodit/tfmtodit.cpp b/src/utils/tfmtodit/tfmtodit.cpp
index 6e1ff747a..0e3a5cf67 100644
--- a/src/utils/tfmtodit/tfmtodit.cpp
+++ b/src/utils/tfmtodit/tfmtodit.cpp
@@ -61,6 +61,8 @@ both be zero. */
 
 #include <getopt.h> // getopt_long()
 
+#include <new> // std::bad_alloc
+
 // needed for DIR_SEPS, FOPEN_RB
 #include "posix.h"
 #include "nonposix.h"
@@ -303,7 +305,14 @@ bool tfm::load(const char *file)
   }
   int lf = (c1 << 8) + c2;
   int toread = (lf * 4) - 2;
-  unsigned char *buf = new unsigned char[toread];
+  unsigned char *buf = 0 /* nullptr */;
+  try {
+    buf = new unsigned char[toread];
+  }
+  catch (const std::bad_alloc &e) {
+    fatal("cannot allocate %1 bytes for storage of contents of TFM file"
+         " '%2'", toread, file);
+  }
   if (fread(buf, 1, toread, fp) != (size_t)toread) {
     if (feof(fp))
       error("unexpected end of file on '%1'", file);
@@ -342,14 +351,29 @@ bool tfm::load(const char *file)
     delete[] buf;
     return false;
   }
-  char_info = new char_info_word[ec - bc + 1];
-  width = new int[nw];
-  height = new int[nh];
-  depth = new int[nd];
-  italic = new int[ni];
-  lig_kern = new lig_kern_command[nl];
-  kern = new int[nk];
-  param = new int[np];
+  // These are pretty meager allocations, so try/catch them all at once.
+  size_t amount = ((ec - bc + 1) * sizeof(char_info_word)
+                 + (nw * sizeof(int))
+                 + (nh * sizeof(int))
+                 + (nd * sizeof(int))
+                 + (ni * sizeof(int))
+                 + (nl * sizeof(lig_kern_command))
+                 + (nk * sizeof(int))
+                 + (np * sizeof(int)));
+  try {
+    char_info = new char_info_word[ec - bc + 1];
+    width = new int[nw];
+    height = new int[nh];
+    depth = new int[nd];
+    italic = new int[ni];
+    lig_kern = new lig_kern_command[nl];
+    kern = new int[nk];
+    param = new int[np];
+  }
+  catch (const std::bad_alloc &e) {
+    fatal("cannot allocate %1 bytes for storage of font metrics in file"
+         " '%2'", amount, file);
+  }
   cs = read4(ptr);
   ds = read4(ptr);
   ptr += (lh - 2) * 4;

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

Reply via email to