Skip to content

Commit 9e8f316

Browse files
committed
extmod/moductypes: Fix bigint handling for 32-bit ports.
1 parent 3e5cd35 commit 9e8f316

4 files changed

Lines changed: 67 additions & 1 deletion

File tree

extmod/moductypes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ STATIC void set_aligned(uint val_type, void *p, mp_int_t index, mp_obj_t val) {
360360
return;
361361
}
362362
#endif
363-
mp_int_t v = mp_obj_get_int(val);
363+
mp_int_t v = mp_obj_get_int_truncated(val);
364364
switch (val_type) {
365365
case UINT8:
366366
((uint8_t*)p)[index] = (uint8_t)v; return;

py/objint_mpz.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf
116116
void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf) {
117117
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
118118
mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
119+
memset(buf, 0, len);
119120
mpz_as_bytes(&self->mpz, big_endian, len, buf);
120121
}
121122

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# This test checks previously known problem values for 32-bit ports.
2+
# It's less useful for 64-bit ports.
3+
try:
4+
import uctypes
5+
except ImportError:
6+
import sys
7+
print("SKIP")
8+
sys.exit()
9+
10+
buf = b"12345678abcd"
11+
struct = uctypes.struct(
12+
uctypes.addressof(buf),
13+
{"f32": uctypes.UINT32 | 0, "f64": uctypes.UINT64 | 4},
14+
uctypes.LITTLE_ENDIAN
15+
)
16+
17+
struct.f32 = 0x7fffffff
18+
print(buf)
19+
20+
struct.f32 = 0x80000000
21+
print(buf)
22+
23+
struct.f32 = 0xff010203
24+
print(buf)
25+
26+
struct.f64 = 0x80000000
27+
print(buf)
28+
29+
struct.f64 = 0x80000000 * 2
30+
print(buf)
31+
32+
print("=")
33+
34+
buf = b"12345678abcd"
35+
struct = uctypes.struct(
36+
uctypes.addressof(buf),
37+
{"f32": uctypes.UINT32 | 0, "f64": uctypes.UINT64 | 4},
38+
uctypes.BIG_ENDIAN
39+
)
40+
41+
struct.f32 = 0x7fffffff
42+
print(buf)
43+
44+
struct.f32 = 0x80000000
45+
print(buf)
46+
47+
struct.f32 = 0xff010203
48+
print(buf)
49+
50+
struct.f64 = 0x80000000
51+
print(buf)
52+
53+
struct.f64 = 0x80000000 * 2
54+
print(buf)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
b'\xff\xff\xff\x7f5678abcd'
2+
b'\x00\x00\x00\x805678abcd'
3+
b'\x03\x02\x01\xff5678abcd'
4+
b'\x03\x02\x01\xff\x00\x00\x00\x80\x00\x00\x00\x00'
5+
b'\x03\x02\x01\xff\x00\x00\x00\x00\x01\x00\x00\x00'
6+
=
7+
b'\x7f\xff\xff\xff5678abcd'
8+
b'\x80\x00\x00\x005678abcd'
9+
b'\xff\x01\x02\x035678abcd'
10+
b'\xff\x01\x02\x03\x00\x00\x00\x00\x80\x00\x00\x00'
11+
b'\xff\x01\x02\x03\x00\x00\x00\x01\x00\x00\x00\x00'

0 commit comments

Comments
 (0)