groff-commit
[Top][All Lists]
Advanced

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

[groff] 08/27: src/preproc/html/pre-html.cpp: Refactor (7/11).


From: G. Branden Robinson
Subject: [groff] 08/27: src/preproc/html/pre-html.cpp: Refactor (7/11).
Date: Sat, 2 Jul 2022 00:43:11 -0400 (EDT)

gbranden pushed a commit to branch master
in repository groff.

commit 665953f8026f25c4fc820e68e8a901531e1b2445
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Tue Jun 28 10:48:33 2022 -0500

    src/preproc/html/pre-html.cpp: Refactor (7/11).
    
    * src/preproc/html/pre-html.cpp (makeFileName, scanArguments): Dismiss
      Shlemiel the Painter: save return value of `strlen()` and call
      `strcpy()` multiple times instead of mixing `strcpy()` and `strcat()`;
      the latter approach rescans the string unnecessarily.
---
 ChangeLog                     |  6 +++++-
 src/preproc/html/pre-html.cpp | 23 +++++++++++++----------
 2 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5d86b8f2..d706742c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,4 @@
-2022-06-27  G. Branden Robinson <g.branden.robinson@gmail.com>
+2022-06-28  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        * src/preproc/html/pre-html.cpp: Refactor.  Drop unnecessary
        prototype for static function.
@@ -13,6 +13,10 @@
        (makeFileName, checkImageDir, char_buffer::run_output_filter,
        scanArguments): Call `fatal()` instead of `error()` and then
        `exit(1)`.
+       (makeFileName, scanArguments): Dismiss Shlemiel the Painter:
+       save return value of `strlen()` and call `strcpy()` multiple
+       times instead of mixing `strcpy()` and `strcat()`; the latter
+       approach rescans the string unnecessarily.
        (char_buffer::run_output_filter): Stop passing unnecessary null
        pointer argument to diagnostic message functions.  Stop calling
        `fflush()` after libgroff diagnostic function, which always
diff --git a/src/preproc/html/pre-html.cpp b/src/preproc/html/pre-html.cpp
index e8a3175e..722eee8c 100644
--- a/src/preproc/html/pre-html.cpp
+++ b/src/preproc/html/pre-html.cpp
@@ -572,13 +572,14 @@ static void makeFileName(void)
   if (macroset_template == NULL)
     sys_fatal("make_message");
 
-  image_template =
-    (char *)malloc(strlen("%d") + strlen(macroset_template) + 1);
+  size_t mtlen = strlen(macroset_template);
+  image_template = (char *)malloc(strlen("%d") + mtlen + 1);
   if (image_template == NULL)
     sys_fatal("malloc");
-  strcpy(image_template, macroset_template);
+  char *s = strcpy(image_template, macroset_template);
+  s += mtlen;
   // Keep this format string synced with troff:suppress_node::tprint().
-  strcat(image_template, "%d");
+  strcpy(s, "%d");
 }
 
 /*
@@ -1600,12 +1601,14 @@ static void usage(FILE *stream)
 
 static int scanArguments(int argc, char **argv)
 {
-  const char *command_prefix = getenv("GROFF_COMMAND_PREFIX");
-  if (!command_prefix)
-    command_prefix = PROG_PREFIX;
-  char *troff_name = new char[strlen(command_prefix) + strlen("troff") + 1];
-  strcpy(troff_name, command_prefix);
-  strcat(troff_name, "troff");
+  const char *cmdprefix = getenv("GROFF_COMMAND_PREFIX");
+  if (!cmdprefix)
+    cmdprefix = PROG_PREFIX;
+  size_t pfxlen = strlen(cmdprefix);
+  char *troff_name = new char[pfxlen + strlen("troff") + 1];
+  char *s = strcpy(troff_name, cmdprefix);
+  s += pfxlen;
+  strcpy(s, "troff");
   int c, i;
   static const struct option long_options[] = {
     { "help", no_argument, 0, CHAR_MAX + 1 },



reply via email to

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