gbranden pushed a commit to branch master
in repository groff.

commit 26b53881cbe6fe43a6cf50ff934cba34a725cb54
Author: G. Branden Robinson <[email protected]>
AuthorDate: Mon Mar 23 17:56:19 2026 -0500

    [troff]: Fix `-Wformat-truncation` warnings.
    
    * src/roff/troff/input.cpp (charinfo::dump):
    * src/roff/troff/node.cpp (glyph_node::asciify): When preparing to
      format the hexadecimal representation of a Unicode code point with
      `get_unicode_code()`, prepare a buffer of sufficient size to store a
      32-bit unsigned integer, as that's what snprintf(3)'s `%X` conversion
      produces.  It doesn't know that Unicode is only a 20.1-bit code.  Even
      if it did, our use of `int` to store the value fails to communicate
      that constraint to snprintf(3).
    
      Even the use of `int` a bit slippery of us, since it's a signed type
      with a 31-bit significand.  But libgroff's `glyph_to_unicode()`, which
      `get_unicode_code()` calls, uses "-1" to signal an error in-band.
---
 ChangeLog                | 19 +++++++++++++++++++
 src/roff/troff/input.cpp |  5 ++++-
 src/roff/troff/node.cpp  |  5 ++++-
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 716ccf5cc..98045874f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,22 @@
+2026-03-23  G. Branden Robinson <[email protected]>
+
+       [troff]: Fix `-Wformat-truncation` warnings.
+
+       * src/roff/troff/input.cpp (charinfo::dump):
+       * src/roff/troff/node.cpp (glyph_node::asciify): When preparing
+       to format the hexadecimal representation of a Unicode code
+       point with `get_unicode_code()`, prepare a buffer of sufficient
+       size to store a 32-bit unsigned integer, as that's what
+       snprintf(3)'s `%X` conversion produces.  It doesn't know that
+       Unicode is only a 20.1-bit code.  Even if it did, our use of
+       `int` to store the value fails to communicate that constraint to
+       snprintf(3).
+
+       Even the use of `int` a bit slippery of us, since it's a signed
+       type with a 31-bit significand.  But libgroff's
+       `glyph_to_unicode()`, which `get_unicode_code()` calls, uses
+       "-1" to signal an error in-band.
+
 2026-03-23  G. Branden Robinson <[email protected]>
 
        * configure.ac: Stop interpolating `GROFF_CXX_CHECK` macro.
diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index f859bb569..91196b946 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -11223,7 +11223,10 @@ void charinfo::dump()
     // Also see node.cpp::glyph_node::asciify().
     int mapping = get_unicode_mapping();
     if (mapping >= 0) {
-      const size_t buflen = 6; // enough for five hex digits + '\0'
+      // All we need is `sizeof "10FFFF"` but GCC's
+      // "-Wformat-truncation" warning doesn't know that Unicode code
+      // points are limited in range.
+      const size_t buflen = sizeof "FFFFFFFF";
       char hexbuf[buflen];
       (void) memset(hexbuf, '\0', buflen);
       (void) snprintf(hexbuf, buflen, "%.4X", mapping);
diff --git a/src/roff/troff/node.cpp b/src/roff/troff/node.cpp
index 9901a21d3..e4ea93d2f 100644
--- a/src/roff/troff/node.cpp
+++ b/src/roff/troff/node.cpp
@@ -3946,7 +3946,10 @@ void glyph_node::asciify(macro *m)
            break;
          default:
            m->append_str("\\[u");
-           const size_t buflen = sizeof "10FFFF";
+           // All we need is `sizeof "10FFFF"` but GCC's
+           // "-Wformat-truncation" warning doesn't know that Unicode
+           // code points are limited in range.
+           const size_t buflen = sizeof "FFFFFFFF";
            char hexbuf[buflen];
            (void) memset(hexbuf, '\0', buflen);
            (void) snprintf(hexbuf, buflen, "%.4X", unicode_mapping);

_______________________________________________
groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit

Reply via email to