groff-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[groff] 27/48: [troff]: Return better `.t` reg val in diversions.


From: G. Branden Robinson
Subject: [groff] 27/48: [troff]: Return better `.t` reg val in diversions.
Date: Thu, 15 Aug 2024 13:15:35 -0400 (EDT)

gbranden pushed a commit to branch master
in repository groff.

commit 5505e986413fb52357239f9b7fbbaebef2ed0566
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Tue Aug 13 09:50:06 2024 -0500

    [troff]: Return better `.t` reg val in diversions.
    
    * src/roff/troff/div.cpp (macro_diversion::distance_to_next_trap):
      Return a value quantized to the vertical resolution of the output
      device.  Add an `assert()` for a paranoia's sake, since there is an
      integer arithmetic land mine here.
---
 ChangeLog              |  8 ++++++++
 src/roff/troff/div.cpp | 13 +++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3d3761f8a..68149c4e3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2024-08-13  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       * src/roff/troff/div.cpp
+       (macro_diversion::distance_to_next_trap): Return a value
+       quantized to the vertical resolution of the output device.  Add
+       an `assert()` for a paranoia's sake, since there is an integer
+       arithmetic land mine here.
+
 2024-08-13  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        * src/roff/groff/tests/arithmetic-works.sh: Add more test cases.
diff --git a/src/roff/troff/div.cpp b/src/roff/troff/div.cpp
index 49d29be5e..772540c09 100644
--- a/src/roff/troff/div.cpp
+++ b/src/roff/troff/div.cpp
@@ -243,11 +243,16 @@ macro_diversion::~macro_diversion()
 
 vunits macro_diversion::distance_to_next_trap()
 {
-  if (!diversion_trap.is_null() && diversion_trap_pos > vertical_position)
-    return diversion_trap_pos - vertical_position;
+  vunits distance = 0;
+  if (!diversion_trap.is_null()
+      && (diversion_trap_pos > vertical_position))
+    distance = diversion_trap_pos - vertical_position;
   else
-    // Subtract vresolution so that vunits::vunits does not overflow.
-    return vunits(INT_MAX - vresolution);
+    // Do the (saturating) arithmetic ourselves to avoid an error
+    // diagnostic from constructor in number.cpp.
+    distance = units(INT_MAX / vresolution);
+  assert(distance >= 0);
+  return distance;
 }
 
 const char *macro_diversion::get_next_trap_name()



reply via email to

[Prev in Thread] Current Thread [Next in Thread]