gbranden pushed a commit to branch master
in repository groff.

commit 6f9e7b255df14431fbda6b13bfd4c5cf4d05619d
Author: G. Branden Robinson <[email protected]>
AuthorDate: Sun Nov 15 18:54:57 2026 -0600

    [pic]: Fix memory leak.
    
    * src/preproc/pic/pic.h: Declare new function `clean_up_parser()`.
    
    * src/preproc/pic/pic.ypp: Preprocessor-include C++ <vector> header
      file.  Declare global `place_list`, an STL vector of pointers to
      `place`.
    
      (define_label, define_variable): Store heap-allocated `place` pointer
      to `place_list`.
    
      (clean_up_parser): New function walks the `place_list` and
      array-deletes the `place` pointers within.
    
    * src/preproc/pic/main.cpp (main): Call `clean_up_parser()` when exiting
      normally.
    
    Begins the long process of fixing Savannah #68922.
    
    Given rudimentary input like:
    
    .PS
    circle 0.1;
    .PE
    
    ...fixes:
    
    ==11850==ERROR: LeakSanitizer: detected memory leaks
    
    Direct leak of 504 byte(s) in 21 object(s) allocated from:
        #0 0x7fc9be4b9628 in operator new[](unsigned long) 
../../../../src/libsanitizer/asan/asan_new_delete.cpp:98
        #1 0x55e96fc9e3c1 in define_variable(char const*, double) 
../src/preproc/pic/pic.ypp:1840
    
    SUMMARY: AddressSanitizer: 504 byte(s) leaked in 21 allocation(s).
---
 ChangeLog                | 18 ++++++++++++++++++
 src/preproc/pic/main.cpp |  1 +
 src/preproc/pic/pic.h    |  1 +
 src/preproc/pic/pic.ypp  | 14 ++++++++++++++
 4 files changed, 34 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index b3041bfee..2a5fd7a87 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2026-11-15  G. Branden Robinson <[email protected]>
+
+       [pic]: Fix memory leak.
+
+       * src/preproc/pic/pic.h: Declare new function
+       `clean_up_parser()`.
+       * src/preproc/pic/pic.ypp: Preprocessor-include C++ <vector>
+       header file.  Declare global `place_list`, an STL vector of
+       pointers to `place`.
+       (define_label, define_variable): Store heap-allocated `place`
+       pointer to `place_list`.
+       (clean_up_parser): New function walks the `place_list` and
+       array-deletes the `place` pointers within.
+       * src/preproc/pic/main.cpp (main): Call `clean_up_parser()` when
+       exiting normally.
+
+       Begins the long process of fixing Savannah #68922.
+
 2026-11-15  G. Branden Robinson <[email protected]>
 
        * src/preproc/pic/pic.ypp (reset, reset_all): Fix code style
diff --git a/src/preproc/pic/main.cpp b/src/preproc/pic/main.cpp
index 25951645d..171ccf55e 100644
--- a/src/preproc/pic/main.cpp
+++ b/src/preproc/pic/main.cpp
@@ -696,6 +696,7 @@ int main(int argc, char **argv)
   }
 #endif
   delete out;
+  clean_up_parser();
   if (ferror(stdout))
     fatal("error status on standard output stream");
   if (fflush(stdout) < 0)
diff --git a/src/preproc/pic/pic.h b/src/preproc/pic/pic.h
index 919995241..aa77e49db 100644
--- a/src/preproc/pic/pic.h
+++ b/src/preproc/pic/pic.h
@@ -83,6 +83,7 @@ void do_copy(const char *file);
 // pic.ypp
 void parse_init();
 void parse_cleanup();
+void clean_up_parser();
 
 void lex_error(const char *message,
               const errarg &arg1 = empty_errarg,
diff --git a/src/preproc/pic/pic.ypp b/src/preproc/pic/pic.ypp
index 3a6b51038..7a65b1c07 100644
--- a/src/preproc/pic/pic.ypp
+++ b/src/preproc/pic/pic.ypp
@@ -51,6 +51,8 @@ extern "C" {
 }
 #endif /* NEED_DECLARATION_SRAND */
 
+#include <vector>
+
 #include "lib.h" // strsave()
 
 #include "pic.h"
@@ -1818,9 +1820,12 @@ place *lookup_label(const char *label)
   }
 }
 
+std::vector<place *> place_list;
+
 void define_label(const char *label, const place *pl)
 {
   place *p = new place[1];
+  place_list.push_back(p);
   *p = *pl;
   current_table->define(label, p);
 }
@@ -1838,6 +1843,7 @@ int lookup_variable(const char *name, double *val)
 void define_variable(const char *name, double val)
 {
   place *p = new place[1];
+  place_list.push_back(p);
   p->obj = 0;
   p->x = val;
   p->y = 0.0;
@@ -2029,6 +2035,14 @@ char *do_sprintf(const char *fmt, const double *v, int 
nv)
   return strsave(result.contents());
 }
 
+void clean_up_parser()
+{
+  while (!place_list.empty()) {
+    delete[] place_list.back();
+    place_list.pop_back();
+  }
+}
+
 // Local Variables:
 // fill-column: 72
 // mode: C++

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

Reply via email to