gbranden pushed a commit to branch master
in repository groff.

commit 95d2d0a2504e8131a251a9142fd39eca3e453847
Author: G. Branden Robinson <[email protected]>
AuthorDate: Thu Mar 19 08:41:10 2026 -0500

    [grotty]: Avoid leaking memory.
    
    * src/devices/grotty/tty.cpp (tty_printer::end_page): Further simplify
      new dynamic memory management strategy to avoid memory leaks.  Always
      `delete[]` (free) the `lines` array at the end of a page, regardless
      of the value of `nlines`.  Comparing the latter's value to
      `default_lines_per_page` was a naïve technique.  First, reducing the
      page length _below_ the default would also cause a reallocation, so it
      needs to be freed.  Second, if the input document resized the page
      multiple times, with the final value being equal to
      `default_lines_per_page`, we would similarly wrongly skip the
      `delete[]`.  Since `begin_page()` _always_ allocates storage with
      `new` and `add_char()` _always_ reallocates it with `new` (and deletes
      the old one) in the event of a resize, we can (and should) _always_
      delete the storage allocated to `lines` at the end of a page.
---
 ChangeLog                  | 17 +++++++++++++++++
 src/devices/grotty/tty.cpp |  3 +--
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index d467b8ae2..61ef7cde0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2026-03-19  G. Branden Robinson <[email protected]>
+
+       * src/devices/grotty/tty.cpp (tty_printer::end_page): Further
+       simplify new dynamic memory management strategy to avoid memory
+       leaks.  Always `delete[]` (free) the `lines` array at the end of
+       a page, regardless of the value of `nlines`.  Comparing the
+       latter's value to `default_lines_per_page` was a naïve
+       technique.  First, reducing the page length _below_ the default
+       would also cause a reallocation, so it needs to be freed.
+       Second, if the input document resized the page multiple times,
+       with the final value being equal to `default_lines_per_page`, we
+       would similarly wrongly skip the `delete[]`.  Since
+       `begin_page()` _always_ allocates storage with `new` and
+       `add_char()` _always_ reallocates it with `new` (and deletes the
+       old one) in the event of a resize, we can (and should) _always_
+       delete the storage allocated to `lines` at the end of a page.
+
 2026-03-18  G. Branden Robinson <[email protected]>
 
        * src/preproc/eqn/tests/neqn-smoke-test.sh: Configure
diff --git a/src/devices/grotty/tty.cpp b/src/devices/grotty/tty.cpp
index b50163332..c73957e5f 100644
--- a/src/devices/grotty/tty.cpp
+++ b/src/devices/grotty/tty.cpp
@@ -954,8 +954,7 @@ void tty_printer::end_page(int page_length)
     for (; last_line < lines_per_page; last_line++)
       putchar('\n');
   }
-  if (nlines > default_lines_per_page)
-    delete[] lines;
+  delete[] lines;
 }
 
 font *tty_printer::make_font(const char *nm)

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

Reply via email to