Cast to an unsigned type before negating
[openssl.git] / crypto / asn1 / a_int.c
index d06d4178dd2faf3c17855c6b7d029f6c7a76053e..833322e89bac145efda755566a75856734522965 100644 (file)
@@ -115,21 +115,21 @@ static size_t i2c_ibuf(const unsigned char *b, size_t blen, int neg,
         memcpy(p, b, blen);
     else {
         /* Begin at the end of the encoding */
-        n = b + blen - 1;
-        p += blen - 1;
+        n = b + blen;
+        p += blen;
         i = blen;
         /* Copy zeros to destination as long as source is zero */
-        while (!*n && i > 1) {
-            *(p--) = 0;
+        while (!n[-1] && i > 1) {
+            *(--p) = 0;
             n--;
             i--;
         }
         /* Complement and increment next octet */
-        *(p--) = ((*(n--)) ^ 0xff) + 1;
+        *(--p) = ((*(--n)) ^ 0xff) + 1;
         i--;
         /* Complement any octets left */
         for (; i > 0; i--)
-            *(p--) = *(n--) ^ 0xff;
+            *(--p) = *(--n) ^ 0xff;
     }
 
     *pp += ret;
@@ -201,18 +201,18 @@ static size_t c2i_ibuf(unsigned char *b, int *pneg,
     /* Must be negative: calculate twos complement */
     if (b) {
         const unsigned char *from = p + plen - 1 + pad;
-        unsigned char *to = b + plen - 1;
+        unsigned char *to = b + plen;
         i = plen;
         while (*from == 0 && i) {
-            *to-- = 0;
+            *--to = 0;
             i--;
             from--;
         }
-        *to-- = (*from-- ^ 0xff) + 1;
+        *--to = (*from-- ^ 0xff) + 1;
         OPENSSL_assert(i != 0);
         i--;
         for (; i > 0; i--)
-            *to-- = *from-- ^ 0xff;
+            *--to = *from-- ^ 0xff;
     }
     return plen;
 }
@@ -289,7 +289,7 @@ static int asn1_get_int64(int64_t *pr, const unsigned char *b, size_t blen,
             ASN1err(ASN1_F_ASN1_GET_INT64, ASN1_R_TOO_SMALL);
             return 0;
         }
-        *pr = -(int64_t)r;
+        *pr = -(uint64_t)r;
     } else {
         if (r > INT64_MAX) {
             ASN1err(ASN1_F_ASN1_GET_INT64, ASN1_R_TOO_LARGE);
@@ -595,7 +595,7 @@ int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
     return ASN1_ENUMERATED_set_int64(a, v);
 }
 
-long ASN1_ENUMERATED_get(ASN1_ENUMERATED *a)
+long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a)
 {
     int i;
     int64_t r;