gbranden pushed a commit to branch master
in repository groff.

commit a0e5e1fb3cb06859fe8f01c673d8f207126ca3b5
Author: G. Branden Robinson <[email protected]>
AuthorDate: Sat Apr 26 03:17:48 2025 -0500

    src/libs/libgroff/nametoindex.cpp: Refactor.
    
    ...to use `reinterpret_cast`.
    
    * src/libs/libgroff/nametoindex.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 | 4 +++-
 src/roff/troff/input.cpp          | 5 ++---
 3 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 85b2e3833..40e5728b4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2025-04-26  G. Branden Robinson <[email protected]>
+
+       * src/libs/libgroff/nametoindex.cpp (glyph_to_name): Use
+       explicit `reinterpret_cast` C++ operator instead of C-style
+       cast.  Annotate why we use this footgun.
+
 2025-04-26  G. Branden Robinson <[email protected]>
 
        * src/libs/libgroff/nametoindex.cpp: Fix code style nits.
diff --git a/src/libs/libgroff/nametoindex.cpp 
b/src/libs/libgroff/nametoindex.cpp
index a4dbc211f..88ba1532d 100644
--- a/src/libs/libgroff/nametoindex.cpp
+++ b/src/libs/libgroff/nametoindex.cpp
@@ -171,7 +171,9 @@ glyph *name_to_glyph(const char *s)
 
 const char *glyph_to_name(glyph *g)
 {
-  charinfo *ci = (charinfo *)g; // Every glyph is actually a charinfo.
+  // In both libgroff and troff, `charinfo` has `glyph` as a base class.
+  // But in troff, `charinfo` stores much more information.
+  charinfo *ci = reinterpret_cast<charinfo *>(g);
   return ci->name;
 }
 
diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index 88dad3235..5875e4ffd 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -10408,9 +10408,8 @@ glyph *number_to_glyph(int n)
 
 const char *glyph_to_name(glyph *g)
 {
-  // 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.
+  // In both libgroff and troff, `charinfo` has `glyph` as a base class.
+  // But in troff, `charinfo` stores much more information.
   charinfo *ci = reinterpret_cast<charinfo *>(g);
   return ((ci->nm != UNNAMED_SYMBOL) ? ci->nm.contents()
                                     : 0 /* nullptr */);

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

Reply via email to