From 1fef5662ab96a27c2b279082607103bbe5da9de5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 5 Nov 2020 22:35:02 +1100 Subject: [PATCH] py/mpz: Do sign extension in mpz_as_bytes for negative values. Signed-off-by: Damien George --- py/mpz.c | 10 +++++++++- py/objint_mpz.c | 1 - tests/basics/struct1_intbig.py | 8 ++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/py/mpz.c b/py/mpz.c index b3d8806792..7515930538 100644 --- a/py/mpz.c +++ b/py/mpz.c @@ -1609,7 +1609,6 @@ bool mpz_as_uint_checked(const mpz_t *i, mp_uint_t *value) { return true; } -// writes at most len bytes to buf (so buf should be zeroed before calling) void mpz_as_bytes(const mpz_t *z, bool big_endian, size_t len, byte *buf) { byte *b = buf; if (big_endian) { @@ -1641,6 +1640,15 @@ void mpz_as_bytes(const mpz_t *z, bool big_endian, size_t len, byte *buf) { } } } + + // fill remainder of buf with zero/sign extension of the integer + if (big_endian) { + len = b - buf; + } else { + len = buf + len - b; + buf = b; + } + memset(buf, z->neg ? 0xff : 0x00, len); } #if MICROPY_PY_BUILTINS_FLOAT diff --git a/py/objint_mpz.c b/py/objint_mpz.c index 6e52073a6e..ef3e017967 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -116,7 +116,6 @@ mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf) { assert(mp_obj_is_type(self_in, &mp_type_int)); mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in); - memset(buf, 0, len); mpz_as_bytes(&self->mpz, big_endian, len, buf); } diff --git a/tests/basics/struct1_intbig.py b/tests/basics/struct1_intbig.py index 380293f36c..24541c8a42 100644 --- a/tests/basics/struct1_intbig.py +++ b/tests/basics/struct1_intbig.py @@ -36,3 +36,11 @@ print(struct.unpack("": + for type_ in "bhiq": + fmt = endian + type_ + b = struct.pack(fmt, -2 + bigzero) + print(fmt, b, struct.unpack(fmt, b))