gbranden pushed a commit to branch master
in repository groff.

commit 760b312a3076f42b127d9fc2147f63dff713f15d
Author: G. Branden Robinson <g.branden.robin...@gmail.com>
AuthorDate: Mon Jul 14 20:00:16 2025 -0500

    [grolj4]: Add sanity check.
    
    * src/devices/grolj4/lj4.cpp (lookup_paper_size): Sanity-check the size
      of the `paper_table` array.  It's statically defined, so only hacking
      on the source can violate the invariant.
    
    Annotate a C++11-shaped alternative solution.
---
 ChangeLog                  | 6 ++++++
 src/devices/grolj4/lj4.cpp | 8 +++++++-
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 82cfb8c18..4769a5891 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2025-07-14  G. Branden Robinson <g.branden.robin...@gmail.com>
+
+       * src/devices/grolj4/lj4.cpp (lookup_paper_size): Sanity-check
+       the size of the `paper_table` array.  It's statically defined,
+       so only hacking on the source can violate the invariant.
+
 2025-07-14  G. Branden Robinson <g.branden.robin...@gmail.com>
 
        * src/devices/grolj4/lj4.cpp: Relocate definition of
diff --git a/src/devices/grolj4/lj4.cpp b/src/devices/grolj4/lj4.cpp
index eedcc0025..d2f26b691 100644
--- a/src/devices/grolj4/lj4.cpp
+++ b/src/devices/grolj4/lj4.cpp
@@ -178,7 +178,13 @@ void lj4_font::handle_unknown_font_command(const char 
*command,
 
 static ssize_t lookup_paper_size(const char *s)
 {
-  for (size_t i = 0; i < array_length(paper_table); i++) {
+  // C++11: constexpr
+  const size_t paper_table_length = array_length(paper_table);
+  // ...and once it's a constexpr, we can do this...
+  //static_assert(paper_table_length < INT_MAX);
+  // ...but until then...
+  assert(paper_table_length < INT_MAX);
+  for (size_t i = 0; i < paper_table_length; i++) {
     // FIXME Perhaps allow unique prefix.
     if (strcasecmp(s, paper_table[i].name) == 0)
       return int(i);

_______________________________________________
groff-commit mailing list
groff-commit@gnu.org
https://lists.gnu.org/mailman/listinfo/groff-commit

Reply via email to