gbranden pushed a commit to branch master
in repository groff.
commit a3e0a4fe337a1482410a307180825e55c0fc0869
Author: G. Branden Robinson <[email protected]>
AuthorDate: Thu Jul 25 15:10:39 2024 -0500
src/roff/troff/number.cpp: Fix Savannah #64301.
* src/roff/troff/number.cpp (vunits::vunits, hunits::hunits): Migrate to
C23 checked arithmetic macros. Throw error if addition overflows.
Fixes <https://savannah.gnu.org/bugs/?64301>. (For real this time,
fingers crossed.)
---
ChangeLog | 9 +++++++++
src/roff/troff/number.cpp | 32 ++++++++++++++++++++++++++------
2 files changed, 35 insertions(+), 6 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index a99a5f3fe..731f6b00e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2024-07-25 G. Branden Robinson <[email protected]>
+
+ * src/roff/troff/number.cpp (vunits::vunits, hunits::hunits):
+ Migrate to C23 checked arithmetic macros. Throw error if
+ addition overflows.
+
+ Fixes <https://savannah.gnu.org/bugs/?64301>. (For real this
+ time, fingers crossed.)
+
2024-07-25 G. Branden Robinson <[email protected]>
* src/roff/groff/tests/arithmetic-works.sh: Add more tests.
diff --git a/src/roff/troff/number.cpp b/src/roff/troff/number.cpp
index 7f43c0772..0d91cfe96 100644
--- a/src/roff/troff/number.cpp
+++ b/src/roff/troff/number.cpp
@@ -661,9 +661,19 @@ vunits::vunits(units x)
else {
// Don't depend on rounding direction when dividing neg integers.
int vcrement = (vresolution / 2) - 1;
- n = (x < 0
- ? -((-x + vcrement) / vresolution)
- : (x + vcrement) / vresolution);
+ bool is_overflowing = false;
+ if (x < 0) {
+ if (ckd_add(&n, -x, vcrement))
+ is_overflowing = true;
+ n = -n;
+ }
+ else {
+ if (ckd_add(&n, x, vcrement))
+ is_overflowing = true;
+ }
+ n /= vresolution;
+ if (is_overflowing)
+ error("integer addition wrapped");
}
}
@@ -674,9 +684,19 @@ hunits::hunits(units x)
else {
// Don't depend on rounding direction when dividing neg integers.
int hcrement = (hresolution / 2) - 1;
- n = (x < 0
- ? -((-x + hcrement) / hresolution)
- : (x + hcrement) / hresolution);
+ bool is_overflowing = false;
+ if (x < 0) {
+ if (ckd_add(&n, -x, hcrement))
+ is_overflowing = true;
+ n = -n;
+ }
+ else {
+ if (ckd_add(&n, x, hcrement))
+ is_overflowing = true;
+ }
+ n /= hresolution;
+ if (is_overflowing)
+ error("integer addition wrapped");
}
}
_______________________________________________
Groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit