Cast to an unsigned type before negating
authorKurt Roeckx <kurt@roeckx.be>
Sun, 17 Jul 2016 13:28:09 +0000 (15:28 +0200)
committerRichard Levitte <levitte@openssl.org>
Mon, 20 Mar 2017 21:10:54 +0000 (22:10 +0100)
llvm's ubsan reported:
runtime error: negation of -9223372036854775808 cannot be represented in type
'long'; cast to an unsigned type to negate this value to itself

Found using afl

Reviewed-by: Rich Salz <rsalz@openssl.org>
GH: #1325
(cherry picked from commit 1618679ac478c8f41fc5f320fb4d8a33883b3868)

crypto/asn1/x_long.c
crypto/bio/b_print.c

index 98562a18ba9eebe535a9ef34210771374499890e..efdf574e24752c580d5d544ce2881217adf01bbe 100644 (file)
@@ -126,7 +126,7 @@ static int long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
      * set.
      */
     if (ltmp < 0)
-        utmp = -ltmp - 1;
+        utmp = -(unsigned long)ltmp - 1;
     else
         utmp = ltmp;
     clen = BN_num_bits_word(utmp);
index 987fe068c6de1074d1eebb57e109f2e5413d2a94..eb3ab759349cde0589be8a850fe8c1355433e787 100644 (file)
@@ -502,7 +502,7 @@ fmtint(char **sbuffer,
     if (!(flags & DP_F_UNSIGNED)) {
         if (value < 0) {
             signvalue = '-';
-            uvalue = -value;
+            uvalue = -(unsigned LLONG)value;
         } else if (flags & DP_F_PLUS)
             signvalue = '+';
         else if (flags & DP_F_SPACE)