aboutsummaryrefslogtreecommitdiff
path: root/py/mpz.c
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2020-11-05 22:35:02 +1100
committerDamien George <damien@micropython.org>2020-11-11 22:18:24 +1100
commit1fef5662ab96a27c2b279082607103bbe5da9de5 (patch)
treec8724e5b2a6ef3e22aa361e329041393fcfddbd4 /py/mpz.c
parent7789cd5f16b068f032e58be8e5e60bf17cb03d40 (diff)
py/mpz: Do sign extension in mpz_as_bytes for negative values.
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'py/mpz.c')
-rw-r--r--py/mpz.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/py/mpz.c b/py/mpz.c
index b3d880679..751593053 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