gbranden pushed a commit to branch master
in repository groff.

commit 36e7af47a23de3ee62f9f482112500004837b478
Author: G. Branden Robinson <g.branden.robin...@gmail.com>
AuthorDate: Tue Jun 3 03:31:23 2025 -0500

    [troff]: Add `ft` warning diagnostic.
    
    * src/roff/troff/env.cpp (select_font): Emit warning diagnostic in
      "range" when the `ft` request is given an out-of-range mounting
      position.
    
    Illustration:
    
    Formerly (with groff 1.23.0 and earlier):
    
    $ groff -ww
    .ft
    .ft P
    .ft bogus
    troff:<standard input>:3: warning: cannot select font 'bogus'
    .ft -1
    troff:<standard input>:4: warning: cannot select font '-1'
    .ft 0
    troff:<standard input>:5: warning: no font mounted at position 0
    .ft 1
    .ft 2147483647
    troff:<standard input>:7: warning: no font mounted at position 2147483647
    .ft 2147483648
    troff:<standard input>:8: warning: no font mounted at position -2147483648
    .ft 9999999999
    troff:<standard input>:9: warning: no font mounted at position 1410065407
    
    Now:
    
    $ groff -ww
    .ft
    .ft P
    .ft bogus
    troff:<standard input>:3: warning: cannot select font 'bogus'
    .ft -1
    troff:<standard input>:4: warning: font mounting position must be in range 
0..2147483647, got -1
    .ft 0
    troff:<standard input>:5: warning: no font mounted at position 0
    .ft 1
    .ft 2147483647
    troff:<standard input>:7: warning: no font mounted at position 2147483647
    .ft 2147483648
    troff:<standard input>:8: warning: font mounting position must be in range 
0..2147483647, got 2147483648
    .ft 9999999999
    troff:<standard input>:1: warning: font mounting position must be in range 
0..2147483647, got 9999999999
---
 ChangeLog              |  6 ++++++
 src/roff/troff/env.cpp | 19 ++++++++++++++-----
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index a02c20664..3791807d3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2025-06-03  G. Branden Robinson <g.branden.robin...@gmail.com>
+
+       * src/roff/troff/env.cpp (select_font): Emit warning diagnostic
+       in "range" when the `ft` request is given an out-of-range
+       mounting position.
+
 2025-06-02  Deri James  <d...@chuzzlewit.myzen.co.uk>
 
        * src/utils/afmtodit/afmtodit.pl: Fix logic error; when falling
diff --git a/src/roff/troff/env.cpp b/src/roff/troff/env.cpp
index 529f79968..71801838d 100644
--- a/src/roff/troff/env.cpp
+++ b/src/roff/troff/env.cpp
@@ -1282,9 +1282,11 @@ static void select_font()
   if (s == P_symbol)
     is_number = false;
   else {
-    for (const char *p = s.contents();
-        p != 0 /* nullptr */ && *p != '\0';
-        p++)
+    const char *p = s.contents();
+    assert(*p != 0 /* nullptr */);
+    if ((csdigit(*p)) || ('-' == *p))
+      p++;
+    for (; p != 0 /* nullptr */ && *p != '\0'; p++)
       if (!csdigit(*p)) {
        is_number = false;
        break;
@@ -1292,8 +1294,15 @@ static void select_font()
   }
   // environment::set_font warns if a bogus mounting position is
   // requested.  We must warn here if a bogus font name is selected.
-  if (is_number)
-    (void) curenv->set_font(atoi(s.contents()));
+  if (is_number) {
+    errno = 0;
+    long val = strtol(s.contents(), NULL, 10);
+    if ((ERANGE == errno) || (val > INT_MAX) || (val < 0))
+      warning(WARN_RANGE, "font mounting position must be in range"
+             " 0..%1, got %2", INT_MAX, s.contents());
+    else
+      (void) curenv->set_font(int(val));
+  }
   else {
     if (s == "DESC")
       error("'%1' is not a valid font name", s.contents());

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

Reply via email to