diff --git a/Objects/longobject.c b/Objects/longobject.c index 98bf50d01cc079..23f50c7ed28abb 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -1046,8 +1046,14 @@ _PyLong_AsByteArray(PyLongObject* v, just above didn't get to ensure there's a sign bit, and the loop below wouldn't add one either. Make sure a sign bit exists. */ - unsigned char msb = *(p - pincr); - int sign_bit_set = msb >= 0x80; + int sign_bit_set; + if (n > 0) { + unsigned char msb = *(p - pincr); + sign_bit_set = msb >= 0x80; + } + else { + sign_bit_set = 0; + } assert(accumbits == 0); if (sign_bit_set == do_twos_comp) return 0;