groff-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[groff] 03/06: [pic]: Fix Savannah #64628 (incorrect `lf` usage).


From: G. Branden Robinson
Subject: [groff] 03/06: [pic]: Fix Savannah #64628 (incorrect `lf` usage).
Date: Mon, 25 Sep 2023 10:20:08 -0400 (EDT)

gbranden pushed a commit to branch master
in repository groff.

commit 76af4781348027740537e4f3e620af55f4e40efa
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Mon Sep 25 05:42:19 2023 -0500

    [pic]: Fix Savannah #64628 (incorrect `lf` usage).
    
    * src/preproc/pic/troff.cpp (strsame): Add utility function for
      comparing two C strings for identical content, handling null pointer
      arguments as `strcmp()` does not.
    
      (troff_output::set_location): Refactor; write the two-argument form of
      the `lf` request if the current and last seen file names _don't_
      match.  Problem introduced by me in commit 705be31107, 29 July.  Also
      fix heap memory leak when repeatedly updating
      `troff_output::last_filename` member variable.
    
    Fixes <https://savannah.gnu.org/bugs/?64628>.  Thanks to Bjarni Ingi
    Gislason for the report.
---
 ChangeLog                 | 14 ++++++++++++++
 src/preproc/pic/troff.cpp | 29 ++++++++++++++++++++++-------
 2 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index dd0e92007..822d1c184 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2023-09-25  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       * src/preproc/pic/troff.cpp (strsame): Add utility function for
+       comparing two C strings for identical content, handling null
+       pointer arguments as `strcmp()` does not.
+       (troff_output::set_location): Refactor; write the two-argument
+       form of the `lf` request if the current and last seen file names
+       _don't_ match.  Problem introduced by me in commit 705be31107,
+       29 July.  Also fix heap memory leak when repeatedly updating
+       `troff_output::last_filename` member variable.
+
+       Fixes <https://savannah.gnu.org/bugs/?64628>.  Thanks to Bjarni
+       Ingi Gislason for the report.
+
 2023-09-25  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        [pic]: Save and restore stroke and fill colors when entering and
diff --git a/src/preproc/pic/troff.cpp b/src/preproc/pic/troff.cpp
index eb1f9e48b..7a1348ce9 100644
--- a/src/preproc/pic/troff.cpp
+++ b/src/preproc/pic/troff.cpp
@@ -568,17 +568,32 @@ void troff_output::dot(const position &cent, const 
line_type &lt)
   }
 }
 
+// We might consider putting this in libgroff.  We treat null pointers
+// like NaNs: they are incommensurable even with themselves.
+bool strsame(const char *s, const char *t)
+{
+  if ((s == 0 /* nullptr */) || (t == 0 /* nullptr */))
+    return false;
+  return (strcmp(s, t) == 0);
+}
+
 void troff_output::set_location(const char *s, int n)
 {
   assert(s != 0 /* nullptr */);
-  if ((s != 0 /* nullptr */) && (last_filename != 0 /* nullptr */)
-      && strcmp(s, last_filename) == 0) {
-    printf(".lf %d %s\n", n, s);
-    char *lfn = strdup(s);
-    if (0 /* nullptr */ == lfn)
-      fatal("memory allocation failure while copying file name");
-    last_filename = lfn;
+  bool update_file_name = false;
+  if (s != 0 /* nullptr */) {
+    if (!strsame(s, last_filename)) {
+      char *lfn = strdup(s);
+      if (0 /* nullptr */ == lfn)
+       fatal("memory allocation failure while copying file name");
+      if (last_filename != 0 /* nullptr */)
+       free(const_cast<char *>(last_filename));
+      last_filename = lfn;
+      update_file_name = true;
+    }
   }
+  if (update_file_name)
+    printf(".lf %d %s\n", n, s);
   else
     printf(".lf %d\n", n);
 }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]