gbranden pushed a commit to branch master
in repository groff.

commit 701b34bf637dad61e4e451de04e20b9ccf39177e
Author: G. Branden Robinson <[email protected]>
AuthorDate: Fri Apr 25 00:21:01 2025 -0500

    src/roff/troff/input.cpp: Use `reinterpret_cast`.
    
    ...instead of C's equally omnipotent but more deceptively
    innocent-looking operator.
    
    * src/roff/troff/input.cpp (glyph_to_name): Use explicit
      `reinterpret_cast` C++ operator instead of C-style cast.  Annotate why
      we use this footgun.
---
 ChangeLog                         | 6 ++++++
 src/libs/libgroff/nametoindex.cpp | 2 +-
 src/roff/troff/input.cpp          | 5 ++++-
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index cf472d2b6..0b87db90a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2025-04-25  G. Branden Robinson <[email protected]>
+
+       * src/roff/troff/input.cpp (glyph_to_name): Use explicit
+       `reinterpret_cast` C++ operator instead of C-style cast.
+       Annotate why we use this footgun.
+
 2025-04-25  G. Branden Robinson <[email protected]>
 
        * src/roff/troff/node.cpp: Fix code style nits.
diff --git a/src/libs/libgroff/nametoindex.cpp 
b/src/libs/libgroff/nametoindex.cpp
index fe654d763..bf97ab981 100644
--- a/src/libs/libgroff/nametoindex.cpp
+++ b/src/libs/libgroff/nametoindex.cpp
@@ -147,7 +147,7 @@ glyph *number_to_glyph(int n)
   return indexer.numbered_char_glyph(n);
 }
 
-// troff overrides this function with its own version.
+// troff overrides these functions with its own versions.
 
 glyph *name_to_glyph(const char *s)
 {
diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index f60c78fe4..4a6742d39 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -10402,7 +10402,10 @@ glyph *number_to_glyph(int n)
 
 const char *glyph_to_name(glyph *g)
 {
-  charinfo *ci = (charinfo *)g; // Every glyph is actually a charinfo.
+  // In troff, every `glyph` `g` is actually created as a `charinfo`.
+  // The `glyph` type is used by libgroff, which doesn't need to know
+  // about the `charinfo` type.
+  charinfo *ci = reinterpret_cast<charinfo *>(g);
   return (ci->nm != UNNAMED_SYMBOL ? ci->nm.contents() : 0);
 }
 

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

Reply via email to