fixed undefined behavior when parsing negative ASN.1 Integer

This commit is contained in:
Frank Morgner 2019-08-29 11:15:29 +02:00
parent 3e110995bc
commit f621305140
1 changed files with 4 additions and 2 deletions

View File

@ -707,17 +707,19 @@ static int encode_bit_field(const u8 *inbuf, size_t inlen,
int sc_asn1_decode_integer(const u8 * inbuf, size_t inlen, int *out)
{
int a = 0;
int a = 0, is_negative = 0;
size_t i;
if (inlen > sizeof(int) || inlen == 0)
return SC_ERROR_INVALID_ASN1_OBJECT;
if (inbuf[0] & 0x80)
a = -1;
is_negative = 1;
for (i = 0; i < inlen; i++) {
a <<= 8;
a |= *inbuf++;
}
if (is_negative)
a *= -1;
*out = a;
return 0;
}