groff-commit
[Top][All Lists]
Advanced

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

[groff] 24/39: src/libs/libgroff/fontfile.cpp: Refactor function.


From: G. Branden Robinson
Subject: [groff] 24/39: src/libs/libgroff/fontfile.cpp: Refactor function.
Date: Tue, 31 May 2022 20:32:29 -0400 (EDT)

gbranden pushed a commit to branch master
in repository groff.

commit 02f0e2a0ac699311731aad87620102390f60a629
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Mon May 30 13:28:33 2022 -0500

    src/libs/libgroff/fontfile.cpp: Refactor function.
    
    * src/libs/libgroff/fontfile.cpp (font::open_file): Refactor.  Move more
      logic, including memory allocation, inside conditional that accepts
      only file names without '/' characters, skipping unnecessary work in
      the alternative.  Annotate use of zero literals as null pointers to
      ease any future migration to ISO C++11.  Add 'const' qualifier to
      variable that doesn't require modification (and which is used in the
      LHS of an equality comparison, so that clumsy operator misuse will
      provoke a compiler warning).
    
    Fixes <https://savannah.gnu.org/bugs/?62532>.  Thanks to Bjarni Ingi
    Gislason for the report.
---
 ChangeLog                      | 15 +++++++++++++++
 src/libs/libgroff/fontfile.cpp | 13 +++++++------
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 33c39734..a6c5a2fd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2022-05-30  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       * src/libs/libgroff/fontfile.cpp (font::open_file): Refactor.
+       Move more logic, including memory allocation, inside conditional
+       that accepts only file names without '/' characters, skipping
+       unnecessary work in the alternative.  Annotate use of zero
+       literals as null pointers to ease any future migration to ISO
+       C++11.  Add 'const' qualifier to variable that doesn't require
+       modification (and which is used in the LHS of an equality
+       comparison, so that clumsy operator misuse will provoke a
+       compiler warning).
+
+       Fixes <https://savannah.gnu.org/bugs/?62532>.  Thanks to Bjarni
+       Ingi Gislason for the report.
+
 2022-05-30  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        * configure.ac: In configuration report, say that we're
diff --git a/src/libs/libgroff/fontfile.cpp b/src/libs/libgroff/fontfile.cpp
index a5b03b6a..f468acca 100644
--- a/src/libs/libgroff/fontfile.cpp
+++ b/src/libs/libgroff/fontfile.cpp
@@ -60,17 +60,18 @@ void font::command_line_font_dir(const char *dir)
 
 FILE *font::open_file(const char *nm, char **pathp)
 {
-  FILE *fp = 0;
-  int expected_size = strlen(nm) + strlen(device) + 5; // 'dev' '/' '\0'
-  char *filename = new char[expected_size];
+  FILE *fp = 0 /* nullptr */;
   // Do not traverse user-specified directories; Savannah #61424.
-  if (0 == strchr(nm, '/')) {
-    int actual_size = sprintf(filename, "dev%s/%s", device, nm);
+  if (0 /* nullptr */ == strchr(nm, '/')) {
+    // Allocate enough for nm + device + 'dev' '/' '\0'.
+    int expected_size = strlen(nm) + strlen(device) + 5;
+    char *filename = new char[expected_size];
+    const int actual_size = sprintf(filename, "dev%s/%s", device, nm);
     expected_size--; // sprintf() doesn't count the null terminator.
     if (actual_size == expected_size)
       fp = font_path.open_file(filename, pathp);
+    delete[] filename;
   }
-  delete[] filename;
   return fp;
 }
 



reply via email to

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