From 0d96f1906b66139beebc13c55078aaf610c76968 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 8 Oct 2019 10:48:25 +0900 Subject: mp_binary_get_int: avoid undefined behavior Left shift of negative numbers is undefined in the "C" standard. Multiplying by 256 has the intended effect (in the absence of integer overflow, anyway), can be implemented using the same shift instruction, but does not invoke undefined behavior. This problem was found using clang 7's scan-build static analyzer. --- py/binary.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'py/binary.c') diff --git a/py/binary.c b/py/binary.c index 6b46425cf..2ec12fa93 100644 --- a/py/binary.c +++ b/py/binary.c @@ -184,7 +184,7 @@ long long mp_binary_get_int(mp_uint_t size, bool is_signed, bool big_endian, con val = -1; } for (uint i = 0; i < size; i++) { - val <<= 8; + val *= 256; val |= *src; src += delta; } -- cgit v1.2.3