[PATCH xserver] squash! sync: Convert from "CARD64" to int64_t.

Eric Anholt eric at anholt.net
Thu Aug 24 18:57:39 UTC 2017


---

We pass the overflow unit tests both before and after this change, but
this should be safer.

 include/misc.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/include/misc.h b/include/misc.h
index 0feeaebc7c1a..9d0e422e36b4 100644
--- a/include/misc.h
+++ b/include/misc.h
@@ -327,7 +327,11 @@ bswap_32(uint32_t x)
 static inline Bool
 checked_int64_add(int64_t *out, int64_t a, int64_t b)
 {
-    int64_t result = a + b;
+    /* Do the potentially overflowing math as uint64_t, as signed
+     * integers in C are undefined on overflow (and the compiler may
+     * optimize out our overflow check below, otherwise)
+     */
+    int64_t result = (uint64_t)a + (uint64_t)b;
     /* signed addition overflows if operands have the same sign, and
      * the sign of the result doesn't match the sign of the inputs.
      */
@@ -341,7 +345,7 @@ checked_int64_add(int64_t *out, int64_t a, int64_t b)
 static inline Bool
 checked_int64_subtract(int64_t *out, int64_t a, int64_t b)
 {
-    int64_t result = a - b;
+    int64_t result = (uint64_t)a - (uint64_t)b;
     Bool overflow = (a < 0) != (b < 0) && (a < 0) != (result < 0);
 
     *out = result;
-- 
2.14.1



More information about the xorg-devel mailing list