gbranden pushed a commit to branch master
in repository groff.

commit 3d3ee76860e050750d29a25eb2f648b04bf13894
Author: G. Branden Robinson <g.branden.robin...@gmail.com>
AuthorDate: Thu Jul 10 21:10:38 2025 -0500

    [troff]: Catch `std::bad_alloc` exceptions (2/3).
    
    ...from `new` operator.  Throw a fatal error indicating how much memory
    we couldn't allocate.
    
    * src/roff/troff/input.cpp (do_get_long_name): Do it.
    
    Exhibit:
    
    $ { printf '.defcolor fuchsia'; \
        dd if=/dev/zero of=/dev/stdout bs=1M count=1024 | tr '\0' 'a'; \
        printf ' rgb #fe00fe\n'; } | ./build/test-groff
    1024+0 records in
    1024+0 records out
    1073741824 bytes (1.1 GB, 1.0 GiB) copied, 9.87108 s, 109 MB/s
    troff:<standard input>:1: fatal error: cannot allocate 1073741824 bytes to 
read input line
---
 ChangeLog                |  3 ++-
 src/roff/troff/input.cpp | 24 +++++++++++++++++++-----
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 8f3261114..d1f5698c6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -4,7 +4,8 @@
        Throw a fatal error indicating how much memory we couldn't
        allocate.
 
-       * src/roff/troff/input.cpp (read_long_escape_parameters): Do it.
+       * src/roff/troff/input.cpp (read_long_escape_parameters)
+       (do_get_long_name): Do it.
 
 2025-07-10  G. Branden Robinson <g.branden.robin...@gmail.com>
 
diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index 5245ef72e..c1d905a67 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -2833,17 +2833,31 @@ static symbol do_get_long_name(bool required, char 
end_char)
   while (tok.is_space())
     tok.next();
   int buf_size = default_buffer_size;
-  char *buf = new char[buf_size];
+  char *buf = 0 /* nullptr */;
+  try {
+    // C++03: new char[buf_size]();
+    buf = new char[buf_size];
+  }
+  catch (const std::bad_alloc &e) {
+    fatal("cannot allocate %1 bytes to read input line", buf_size);
+  }
+  (void) memset(buf, 0, (buf_size * sizeof(char)));
   int i = 0;
   for (;;) {
     // If `end_char` != `\0` we normally have to append a null byte.
     if ((i + 2) > buf_size) {
       char *old_buf = buf;
-      // C++03: new char[buf_size * 2]();
-      buf = new char[buf_size * 2];
-      (void) memset(buf, 0, (buf_size * 2 * sizeof(char)));
+      int new_buf_size = buf_size * 2;
+      // C++03: new char[new_buf_size]();
+      try {
+       buf = new char[new_buf_size];
+      }
+      catch (const std::bad_alloc &e) {
+       fatal("cannot allocate %1 bytes to read input line", buf_size);
+      }
+      (void) memset(buf, 0, (new_buf_size * sizeof(char)));
       memcpy(buf, old_buf, (buf_size * sizeof(char)));
-      buf_size *= 2;
+      buf_size = new_buf_size;
       delete[] old_buf;
     }
     if ((buf[i] = tok.ch()) == '\0' || (buf[i] == end_char))

_______________________________________________
groff-commit mailing list
groff-commit@gnu.org
https://lists.gnu.org/mailman/listinfo/groff-commit

Reply via email to