gbranden pushed a commit to branch master
in repository groff.

commit baadf5f03bfacec08e34ef2e0263f2de8bd1f2ef
Author: G. Branden Robinson <[email protected]>
AuthorDate: Mon Jul 15 10:41:34 2024 -0500

    [troff]: Fix Savannah #64301 (10/15).
    
    * src/roff/troff/hvunits.h (hunits operator -): Use `ckd_sub()` instead
      of primitive operation, and throw error diagnostic if arithmetic
      wraps.
---
 ChangeLog                | 5 +++--
 src/roff/troff/hvunits.h | 7 +++++--
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 684dbb5f8..e367861e9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -15,8 +15,9 @@
 
        * src/roff/troff/hvunits.h: Include `config.h` and `stdckdint.h`
        headers.
-       (vunits operator -): Use `ckd_sub()` instead of primitive
-       operation, and throw error diagnostic if arithmetic wraps.
+       (vunits operator -, hunits operator -): Use `ckd_sub()` instead
+       of primitive operation, and throw error diagnostic if arithmetic
+       wraps.
        (vunits::to_units, hunits::to_units): Use `ckd_mul()` instead of
        primitive operation, and throw error diagnostic if arithmetic
        wraps.
diff --git a/src/roff/troff/hvunits.h b/src/roff/troff/hvunits.h
index 1dde42c02..641c46c12 100644
--- a/src/roff/troff/hvunits.h
+++ b/src/roff/troff/hvunits.h
@@ -231,7 +231,8 @@ inline hunits operator -(const hunits & x, const hunits & y)
 {
   hunits r;
   r = x;
-  r.n -= y.n;
+  if (ckd_sub(&r.n, r.n, y.n))
+    error("integer subtraction wrapped");
   return r;
 }
 
@@ -239,7 +240,9 @@ inline hunits operator -(const hunits & x)
 {
   hunits r;
   r = x;
-  r.n = -x.n;
+  // Why?  Consider -(INT_MIN) in two's complement.
+  if (ckd_mul(&r.n, x.n, -1))
+    error("integer subtraction wrapped");
   return r;
 }
 

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

Reply via email to