groff-commit
[Top][All Lists]
Advanced

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

[groff] 08/19: [libgroff,pic]: Check `strdup()` return value.


From: G. Branden Robinson
Subject: [groff] 08/19: [libgroff,pic]: Check `strdup()` return value.
Date: Mon, 8 Nov 2021 18:56:00 -0500 (EST)

gbranden pushed a commit to branch master
in repository groff.

commit f6671243d873b147947125970895f34ca5201cb5
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Mon Nov 8 08:58:06 2021 +1100

    [libgroff,pic]: Check `strdup()` return value.
    
    * src/libs/libgroff/font.cpp (struct text_file): Add `fatal()` member
      function.
    
      (text_file::fatal): Implement it.
    
    * src/libs/libgroff/font.cpp (font::load_desc):
    * src/preproc/pic/troff.cpp (troff_output::set_location): Die if
      `strdup()` returned a null pointer.
---
 ChangeLog                  | 12 ++++++++++++
 src/libs/libgroff/font.cpp | 17 +++++++++++++++++
 src/preproc/pic/troff.cpp  |  5 ++++-
 3 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 0e87c40..80a1df9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
 2021-11-08  G. Branden Robinson <g.branden.robinson@gmail.com>
 
+       [libgroff,pic]: Check `strdup()` return value.
+
+       * src/libs/libgroff/font.cpp (struct text_file): Add `fatal()`
+       member function.
+       (text_file::fatal): Implement it.
+
+       * src/libs/libgroff/font.cpp (font::load_desc):
+       * src/preproc/pic/troff.cpp (troff_output::set_location): Die if
+       `strdup()` returned a null pointer.
+
+2021-11-08  G. Branden Robinson <g.branden.robinson@gmail.com>
+
        * src/libs/libgroff/error.cpp (do_error_with_file_and_line):
        Revise to eliminate `fprintf()` calls, which might perform
        dynamic memory allocation, rendering this function unsafe to
diff --git a/src/libs/libgroff/font.cpp b/src/libs/libgroff/font.cpp
index 2168afa..14a7562 100644
--- a/src/libs/libgroff/font.cpp
+++ b/src/libs/libgroff/font.cpp
@@ -79,6 +79,10 @@ struct text_file {
             const errarg &arg1 = empty_errarg,
             const errarg &arg2 = empty_errarg,
             const errarg &arg3 = empty_errarg);
+  void fatal(const char *format,
+            const errarg &arg1 = empty_errarg,
+            const errarg &arg2 = empty_errarg,
+            const errarg &arg3 = empty_errarg);
 };
 
 text_file::text_file(FILE *p, char *s) : fp(p), path(s), lineno(0),
@@ -143,6 +147,15 @@ void text_file::error(const char *format,
     error_with_file_and_line(path, lineno, format, arg1, arg2, arg3);
 }
 
+void text_file::fatal(const char *format,
+                     const errarg &arg1,
+                     const errarg &arg2,
+                     const errarg &arg3)
+{
+  if (!silent)
+    fatal_with_file_and_line(path, lineno, format, arg1, arg2, arg3);
+}
+
 int glyph_to_unicode(glyph *g)
 {
   const char *nm = glyph_to_name(g);
@@ -1145,6 +1158,9 @@ bool font::load_desc()
       }
       bool found_paper = false;
       char *savedp = strdup(p);
+      if (0 == savedp)
+       t.fatal("memory allocation failure while processing 'papersize'"
+               " directive");
       while (p) {
        double unscaled_paperwidth, unscaled_paperlength;
        if (scan_papersize(p, &papersize, &unscaled_paperlength,
@@ -1156,6 +1172,7 @@ bool font::load_desc()
        }
        p = strtok(0, WS);
       }
+      assert(savedp != 0);
       if (!found_paper) {
        t.error("unable to determine a paper format from '%1'", savedp);
        free(savedp);
diff --git a/src/preproc/pic/troff.cpp b/src/preproc/pic/troff.cpp
index 3ccd681..256aa29 100644
--- a/src/preproc/pic/troff.cpp
+++ b/src/preproc/pic/troff.cpp
@@ -560,7 +560,10 @@ void troff_output::set_location(const char *s, int n)
     printf(".lf %d\n", n);
   else {
     printf(".lf %d %s\n", n, s);
-    last_filename = strdup(s);
+    char *lfn = strdup(s);
+    if (0 == lfn)
+      fatal("memory allocation failure while copying file name");
+    last_filename = lfn;
   }
 }
 



reply via email to

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