gbranden pushed a commit to branch master
in repository groff.

commit 64711b3a875ce9ea3a023401d641b9922de61cc8
Author: G. Branden Robinson <[email protected]>
AuthorDate: Fri Nov 14 11:44:18 2025 -0600

    [troff]: Make numeric expression parser stricter.
    
    * src/roff/troff/number.cpp: Refactor numeric expression parser to
      simplify and heighten severity of diagnostics.  This isn't a behavior
      change; character sequences that were invalid as numeric expressions
      were ignored before and remain so.  But since we're aborting
      interpretation of an argument to a request or escape sequence, where
      in at least one case AT&T troff did not--it would read through
      spurious tab characters to reach a valid numeric expression
      afterward--an error seems more appropriate.
    
      (is_valid_expression_start): Drop special handling of tab and right-
      brace escape sequences.  These get handled anyway when the term at the
      start of the expression gets interpreted...
    
      (is_valid_term): ...thus.  Raise diagnostic severity from warning (in
      "number" category) to error.
    
    Illustration:
    
    Before
    ------
    $ printf '.nr a A2\n.tm a=\\na\n' | ~/groff-1.23.0/bin/groff -z -ww
    troff:<standard input>:1: warning: expected numeric expression, got 'A'
    a=troff:<standard input>:2: warning: register 'a' not defined
    0
    $ printf '.nr a \t2\n.tm a=\\na\n' | ~/groff-1.23.0/bin/groff -z -ww
    troff:<standard input>:1: warning: expected numeric expression, got a tab 
character
    a=troff:<standard input>:2: warning: register 'a' not defined
    0
    $ printf '.nr a \\}2\n.tm a=\\na\n' | ~/groff-1.23.0/bin/groff -z -ww
    troff:<standard input>:1: warning: expected numeric expression, got 
rightbrace escape sequence
    a=troff:<standard input>:2: warning: register 'a' not defined
    0
    
    After
    -----
    $ printf '.nr a A2\n.tm a=\\na\n' | ./build/test-groff -z -ww
    troff:<standard input>:1: error: ignoring invalid numeric expression 
starting with character 'A'
    a=troff:<standard input>:2: warning: register 'a' not defined
    0
    $ printf '.nr a \t2\n.tm a=\\na\n' | ./build/test-groff -z -ww
    troff:<standard input>:1: error: ignoring invalid numeric expression 
starting with a tab character
    a=troff:<standard input>:2: warning: register 'a' not defined
    0
    $ printf '.nr a \\}2\n.tm a=\\na\n' | ./build/test-groff -z -ww
    troff:<standard input>:1: error: ignoring invalid numeric expression 
starting with an escaped '}'
    a=troff:<standard input>:2: warning: register 'a' not defined
    0
    
    DWB 3.3
    -------
    $ printf '.nr a A2\n.tm a=\\na\n' | DWBHOME=~/dwb ~/dwb/bin/nroff >/dev/null
    a=0
    $ printf '.nr a \t2\n.tm a=\\na\n' | DWBHOME=~/dwb ~/dwb/bin/nroff 
>/dev/null
    a=2
    $ printf '.nr a \\}2\n.tm a=\\na\n' | DWBHOME=~/dwb ~/dwb/bin/nroff 
>/dev/null
    a=0
---
 ChangeLog                 | 17 +++++++++++++++++
 src/roff/troff/number.cpp | 14 ++------------
 2 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index fa634c7f4..074135e68 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2025-11-14  G. Branden Robinson <[email protected]>
+
+       * src/roff/troff/number.cpp: Refactor numeric expression parser
+       to simplify and heighten severity of diagnostics.  This isn't
+       a behavior change; character sequences that were invalid as
+       numeric expressions were ignored before and remain so.  But
+       since we're aborting interpretation of an argument to a request
+       or escape sequence, where in at least one case AT&T troff did
+       not--it would read through spurious tab characters to reach a
+       valid numeric expression afterward--an error seems more
+       appropriate.
+       (is_valid_expression_start): Drop special handling of tab and
+       right-brace escape sequences.  These get handled anyway when the
+       term at the start of the expression gets interpreted...
+       (is_valid_term): ...thus.  Raise diagnostic severity from
+       warning (in "number" category) to error.
+
 2025-11-14  G. Branden Robinson <[email protected]>
 
        * src/roff/troff/input.cpp (token::description): Improve
diff --git a/src/roff/troff/number.cpp b/src/roff/troff/number.cpp
index f17629d23..78b5de285 100644
--- a/src/roff/troff/number.cpp
+++ b/src/roff/troff/number.cpp
@@ -245,16 +245,6 @@ static bool is_valid_expression_start()
     warning(WARN_MISSING, "numeric expression missing");
     return false;
   }
-  if (tok.is_tab()) {
-    warning(WARN_TAB, "expected numeric expression, got %1",
-           tok.description());
-    return false;
-  }
-  if (tok.is_right_brace()) {
-    warning(WARN_RIGHT_BRACE, "expected numeric expression, got right"
-           " brace escape sequence");
-    return false;
-  }
   return true;
 }
 
@@ -512,8 +502,8 @@ static bool is_valid_term(units *u, int scaling_unit,
     *u = 0;
     return !is_mandatory;
   default:
-    warning(WARN_NUMBER, "expected numeric expression, got %1",
-           tok.description());
+    error("ignoring invalid numeric expression starting with %1",
+         tok.description());
     return false;
   }
   int divisor = 1;

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

Reply via email to