groff-commit
[Top][All Lists]
Advanced

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

[groff] 05/11: Resolve GCC compiler warnings.


From: G. Branden Robinson
Subject: [groff] 05/11: Resolve GCC compiler warnings.
Date: Mon, 16 Aug 2021 00:01:44 -0400 (EDT)

gbranden pushed a commit to branch master
in repository groff.

commit 261a663cc0e74ed3fd6819d0460deb9824a750b5
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Mon Aug 16 01:37:01 2021 +1000

    Resolve GCC compiler warnings.
    
    Resolve compiler warnings relating to format string security and ISO
    C++98 conformance.
    
    * src/preproc/eqn/delim.cpp (define_extensible_string):
    * src/preproc/pic/pic.ypp (do_sprintf): Use #pragma to silence GCC
      "format-nonliteral" warning and explain why our usage is safe in a
      comment.
    
    * src/preproc/preconv/preconv.cpp (detect_file_encoding): Use `l`
      modifier to `%u` `fprintf()` conversion instead of `z`, and cast
      return values of `size_t` to unsigned long; "ISO C++98 does not
      support the ā€˜zā€™ gnu_printf length modifier" (it doesn't support `ll`
      either).  N.B. this is debugging output only.
    
    * src/roff/groff/groff.cpp (synopsis):
    * src/roff/troff/input.cpp (usage): Repeat variadic argument for `%s`
      conversion in `fprintf()` call because "ISO C++98 does not support %n$
      operand number formats".
    
    * src/roff/troff/env.cpp: Initialize adjustment and hyphenation mode
      enums without commas at the end of the enumeration lists.
---
 ChangeLog                       | 24 ++++++++++++++++++++++++
 src/preproc/eqn/delim.cpp       |  7 ++++++-
 src/preproc/pic/pic.ypp         |  5 +++++
 src/preproc/preconv/preconv.cpp |  5 +++--
 src/roff/groff/groff.cpp        |  6 +++---
 src/roff/troff/env.cpp          |  4 ++--
 src/roff/troff/input.cpp        |  6 +++---
 7 files changed, 46 insertions(+), 11 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index eb47071..6b5c245 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,29 @@
 2021-08-15  G. Branden Robinson <g.branden.robinson@gmail.com>
 
+       Resolve compiler warnings relating to format string security and
+       ISO C++98 conformance.
+
+       * src/preproc/eqn/delim.cpp (define_extensible_string):
+       * src/preproc/pic/pic.ypp (do_sprintf): Use #pragma to silence
+       GCC "format-nonliteral" warning and explain why our usage is
+       safe in a comment.
+
+       * src/preproc/preconv/preconv.cpp (detect_file_encoding): Use
+       `l` modifier to `%u` `fprintf()` conversion instead of `z`, and
+       cast return values of `size_t` to unsigned long; "ISO C++98
+       does not support the ā€˜zā€™ gnu_printf length modifier" (it doesn't
+       support `ll` either).  N.B. this is debugging output only.
+
+       * src/roff/groff/groff.cpp (synopsis):
+       * src/roff/troff/input.cpp (usage): Repeat variadic argument for
+       `%s` conversion in `fprintf()` call because "ISO C++98 does not
+       support %n$ operand number formats".
+
+       * src/roff/troff/env.cpp: Initialize adjustment and hyphenation
+       mode enums without commas at the end of the enumeration lists.
+
+2021-08-15  G. Branden Robinson <g.branden.robinson@gmail.com>
+
        [man]: Clean up the interface between `an-footer` and `BT`, and
        HTML header and footer suppression generally.  Move
        responsibility for checking the `ps4html` register from the
diff --git a/src/preproc/eqn/delim.cpp b/src/preproc/eqn/delim.cpp
index deda4f9..b76e971 100644
--- a/src/preproc/eqn/delim.cpp
+++ b/src/preproc/eqn/delim.cpp
@@ -314,9 +314,14 @@ static void define_extensible_string(char *delim, int uid,
         "\\{",
         current_roman_font, d->small, axis_height,
         current_roman_font, d->small);
-        
+
   char buf[256];
+// The format string in the sprintf below comes from a struct
+// initializer above, and is not subject to external influence.
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
   sprintf(buf, d->chain_format, "\\\\n[" INDEX_REG "]");
+#pragma GCC diagnostic pop
   printf(".nr " INDEX_REG " 0\n"
         ".de " TEMP_MACRO "\n"
         ".ie c%s \\{\\\n"
diff --git a/src/preproc/pic/pic.ypp b/src/preproc/pic/pic.ypp
index 413e3d1..eaec38c 100644
--- a/src/preproc/pic/pic.ypp
+++ b/src/preproc/pic/pic.ypp
@@ -1917,8 +1917,13 @@ char *do_sprintf(const char *form, const double *v, int 
nv)
        }
        one_format += *form++;
        one_format += '\0';
+// We validated the format string above.  Most conversion specifiers are
+// rejected, including `n`.
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
        snprintf(sprintf_buf, sizeof(sprintf_buf),
                 one_format.contents(), v[i++]);
+#pragma GCC diagnostic pop
       }
       one_format.clear();
       result += sprintf_buf;
diff --git a/src/preproc/preconv/preconv.cpp b/src/preproc/preconv/preconv.cpp
index b58c753..b5b5a8c 100644
--- a/src/preproc/preconv/preconv.cpp
+++ b/src/preproc/preconv/preconv.cpp
@@ -1020,7 +1020,7 @@ detect_file_encoding(FILE *fp)
   }
   len = stat_buf.st_size;
   if (debug_flag)
-    fprintf(stderr, "  len: %zu\n", len);  
+    fprintf(stderr, "  len: %lu\n", (unsigned long)len);
   if (len == 0)
     goto end;
   data = (char *)calloc(len, 1);
@@ -1041,7 +1041,8 @@ detect_file_encoding(FILE *fp)
     goto end;
   }
   if (debug_flag)
-    fprintf(stderr, "  uchardet read: %zu bytes\n", read_bytes);
+    fprintf(stderr, "  uchardet read: %lu bytes\n",
+           (unsigned long)read_bytes);
   uchardet_data_end(ud);
   charset = uchardet_get_charset(ud);
   if (debug_flag) {
diff --git a/src/roff/groff/groff.cpp b/src/roff/groff/groff.cpp
index d161f6d..73d30c5 100644
--- a/src/roff/groff/groff.cpp
+++ b/src/roff/groff/groff.cpp
@@ -782,12 +782,12 @@ void synopsis(FILE *stream)
 {
   // Add `J` to the cluster if we ever get ideal(1) support.
   fprintf(stream,
-"usage: %1$s [-abcCeEgGijklNpRsStUVXzZ] [-dCS] [-dNAME=STRING] [-Denc]"
+"usage: %s [-abcCeEgGijklNpRsStUVXzZ] [-dCS] [-dNAME=STRING] [-Denc]"
 " [-fFAM] [-Fdir] [-Idir] [-Kenc] [-Larg] [-mNAME] [-Mdir] [-nNUM]"
 " [-oLIST] [-Parg] [-rCN] [-rREG=EXPR] [-Tdev] [-wNAME] [-Wname]"
 " [FILE ...]\n"
-"usage: %1$s {-h | --help | -v | --version}\n",
-         program_name);
+"usage: %s {-h | --help | -v | --version}\n",
+         program_name, program_name);
 }
 
 void help()
diff --git a/src/roff/troff/env.cpp b/src/roff/troff/env.cpp
index d9b15f8..d6e005f 100644
--- a/src/roff/troff/env.cpp
+++ b/src/roff/troff/env.cpp
@@ -39,7 +39,7 @@ enum { ADJUST_LEFT = 0,
   ADJUST_BOTH = 1,
   ADJUST_CENTER = 3,
   ADJUST_RIGHT = 5,
-  ADJUST_MAX = 5,
+  ADJUST_MAX = 5
 };
 
 enum {
@@ -51,7 +51,7 @@ enum {
   HYPHEN_NOT_FIRST_CHARS = 8,
   HYPHEN_LAST_CHAR = 16,
   HYPHEN_FIRST_CHAR = 32,
-  HYPHEN_MAX = 63,
+  HYPHEN_MAX = 63
 };
 
 struct env_list_node {
diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index 39fd0e0..988fb1e 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -7963,11 +7963,11 @@ static void add_string(const char *s, string_list **p)
 void usage(FILE *stream, const char *prog)
 {
   fprintf(stream,
-"usage: %1$s [-abcCEiRUz] [-dCS] [-dNAME=STRING] [-fFAM] [-Fdir]"
+"usage: %s [-abcCEiRUz] [-dCS] [-dNAME=STRING] [-fFAM] [-Fdir]"
 " [-Idir] [-mNAME] [-Mdir] [-nNUM] [-oLIST] [-rCN] [-rREG=EXPR]"
 " [-Tdev] [-wNAME] [-Wname] [FILE ...]\n"
-"usage: %1$s {-h | --help | -v | --version}\n",
-         prog);
+"usage: %s {-h | --help | -v | --version}\n",
+         prog, prog);
 }
 
 int main(int argc, char **argv)



reply via email to

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