gbranden pushed a commit to branch master
in repository groff.

commit 40d51279673ac09f05285cf6cd8818a833eca117
Author: G. Branden Robinson <[email protected]>
AuthorDate: Mon Jul 15 09:59:55 2024 -0500

    [troff]: Fix Savannah #64301 (7/15).
    
    * src/roff/troff/number.cpp (get_vunits): Use `ckd_add()` (with
      temporary variable, annotating why) instead of primitive operation,
      and throw error diagnostic if arithmetic wraps.
---
 ChangeLog                 |  3 +++
 src/roff/troff/number.cpp | 11 +++++++++--
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 7078592c5..59e11fa41 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -9,6 +9,9 @@
        `ckd_sub()`, and `ckd_mul()` instead of primitive operations,
        and throw error diagnostic if arithmetic wraps.
        (is_valid_expression): Remove manual detection of overflow.
+       (get_vunits): Use `ckd_add()` (with temporary variable,
+       annotating why) instead of primitive operation, and throw error
+       diagnostic if arithmetic wraps.
 
        * src/roff/troff/hvunits.h: Include `config.h` and `stdckdint.h`
        headers.
diff --git a/src/roff/troff/number.cpp b/src/roff/troff/number.cpp
index 9ce2d2d80..c1de807a8 100644
--- a/src/roff/troff/number.cpp
+++ b/src/roff/troff/number.cpp
@@ -118,6 +118,9 @@ static incr_number_result get_incr_number(units *res, 
unsigned char);
 bool get_vunits(vunits *res, unsigned char si, vunits prev_value)
 {
   units v;
+  // Use a primitive temporary because having the ckd macros store to
+  // &(res->n) requires `friend` access and produces wrong results.
+  int i;
   switch (get_incr_number(&v, si)) {
   case INVALID:
     return false;
@@ -125,10 +128,14 @@ bool get_vunits(vunits *res, unsigned char si, vunits 
prev_value)
     *res = v;
     break;
   case INCREMENT:
-    *res = prev_value + v;
+    if (ckd_add(&i, prev_value.to_units(), v))
+      error("integer addition wrapped");
+    *res = i;
     break;
   case DECREMENT:
-    *res = prev_value - v;
+    if (ckd_sub(&i, prev_value.to_units(), v))
+      error("integer subtraction wrapped");
+    *res = i;
     break;
   default:
     assert(0 == "unhandled case returned by get_incr_number()");

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

Reply via email to