Skip to content

Commit 54d9725

Browse files
committed
modstruct: Improve compliance with python3
While checking whether we can enable -Wimplicit-fallthrough, I encountered a diagnostic in mp_binary_set_val_array_from_int which led to discovering the following bug: ``` >>> struct.pack("xb", 3) b'\x03\x03' ``` That is, the next value (3) was used as the value of a padding byte, while standard Python always fills "x" bytes with zeros. I initially thought this had to do with the unintentional fallthrough, but it doesn't. Instead, this code would relate to an array.array with a typecode of padding ('x'), which is ALSO not desktop Python compliant: ``` >>> array.array('x', (1, 2, 3)) array('x', [1, 0, 0]) ``` Possibly this is dead code that used to be shared between struct-setting and array-setting, but it no longer is. I also discovered that the argument list length for struct.pack and struct.pack_into were not checked, and that the length of binary data passed to array.array was not checked to be a multiple of the element size. I have corrected all of these to conform more closely to standard Python and revised some tests where necessary. Some tests for micropython-specific behavior that does not conform to standard Python and is not present in CircuitPython was deleted outright.
1 parent b24d3b8 commit 54d9725

File tree

8 files changed

+46
-63
lines changed

8 files changed

+46
-63
lines changed

py/binary.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) {
126126
break;
127127
case BYTEARRAY_TYPECODE:
128128
case 'B':
129-
case 'x': // value will be discarded
130129
val = ((unsigned char*)p)[index];
131130
break;
132131
case 'h':
@@ -330,7 +329,11 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
330329
}
331330
}
332331

333-
mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val);
332+
if (val_type == 'x') {
333+
memset(p, 0, 1);
334+
} else {
335+
mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val);
336+
}
334337
}
335338

336339
void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t val_in) {
@@ -379,8 +382,6 @@ void mp_binary_set_val_array_from_int(char typecode, void *p, mp_uint_t index, m
379382
case 'B':
380383
((unsigned char*)p)[index] = val;
381384
break;
382-
case 'x':
383-
((unsigned char*)p)[index] = 0;
384385
case 'h':
385386
((short*)p)[index] = val;
386387
break;

py/modstruct.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,21 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_unpack_from_obj, 2, 3, struct_unpack_
183183

184184
// This function assumes there is enough room in p to store all the values
185185
STATIC void struct_pack_into_internal(mp_obj_t fmt_in, byte *p, size_t n_args, const mp_obj_t *args) {
186+
size_t size;
187+
size_t count = calc_size_items(mp_obj_str_get_str(fmt_in), &size);
188+
if (count != n_args) {
189+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
190+
mp_raise_ValueError(NULL);
191+
#else
192+
mp_raise_ValueError_varg(translate("pack expected %d items for packing (got %d)"), count, n_args);
193+
#endif
194+
}
186195
const char *fmt = mp_obj_str_get_str(fmt_in);
187196
char fmt_type = get_fmt_type(&fmt);
188197

189198
size_t i;
190199
for (i = 0; i < n_args;) {
191200
mp_uint_t cnt = 1;
192-
if (*fmt == '\0') {
193-
// more arguments given than used by format string; CPython raises struct.error here
194-
break;
195-
}
196201
if (unichar_isdigit(*fmt)) {
197202
cnt = get_fmt_num(&fmt);
198203
}
@@ -208,8 +213,7 @@ STATIC void struct_pack_into_internal(mp_obj_t fmt_in, byte *p, size_t n_args, c
208213
memset(p + to_copy, 0, cnt - to_copy);
209214
p += cnt;
210215
} else {
211-
// If we run out of args then we just finish; CPython would raise struct.error
212-
while (cnt-- && i < n_args) {
216+
while (cnt--) {
213217
mp_binary_set_val(fmt_type, *fmt, args[i], &p);
214218
// Pad bytes don't have a corresponding argument.
215219
if (*fmt != 'x') {
@@ -222,7 +226,6 @@ STATIC void struct_pack_into_internal(mp_obj_t fmt_in, byte *p, size_t n_args, c
222226
}
223227

224228
STATIC mp_obj_t struct_pack(size_t n_args, const mp_obj_t *args) {
225-
// TODO: "The arguments must match the values required by the format exactly."
226229
mp_int_t size = MP_OBJ_SMALL_INT_VALUE(struct_calcsize(args[0]));
227230
vstr_t vstr;
228231
vstr_init_len(&vstr, size);

py/objarray.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ STATIC void array_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t
9797

9898
#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
9999
STATIC mp_obj_array_t *array_new(char typecode, size_t n) {
100+
if (typecode == 'x') {
101+
mp_raise_ValueError(translate("bad typecode"));
102+
}
100103
int typecode_size = mp_binary_get_size('@', typecode, NULL);
101104
mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
102105
#if MICROPY_PY_BUILTINS_BYTEARRAY && MICROPY_PY_ARRAY
@@ -126,8 +129,10 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
126129
|| (MICROPY_PY_BUILTINS_BYTEARRAY && MP_OBJ_IS_TYPE(initializer, &mp_type_bytearray)))))
127130
&& mp_get_buffer(initializer, &bufinfo, MP_BUFFER_READ)) {
128131
// construct array from raw bytes
129-
// we round-down the len to make it a multiple of sz (CPython raises error)
130132
size_t sz = mp_binary_get_size('@', typecode, NULL);
133+
if (bufinfo.len % sz) {
134+
mp_raise_ValueError(translate("bytes length not a multiple of item size"));
135+
}
131136
size_t len = bufinfo.len / sz;
132137
mp_obj_array_t *o = array_new(typecode, len);
133138
memcpy(o->items, bufinfo.buf, len * sz);

tests/basics/struct1.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@
3939
# network byte order
4040
print(struct.pack('!i', 123))
4141

42+
# too short / too long arguments
43+
buf = bytearray(b'>>>123<<<')
44+
try:
45+
struct.pack_into('bb', buf, 0, 3)
46+
except:
47+
print('struct.error')
48+
49+
try:
50+
struct.pack_into('bb', buf, 0, 3, 1, 4)
51+
except:
52+
print('struct.error')
53+
54+
try:
55+
struct.pack('bb', 3)
56+
except:
57+
print('struct.error')
58+
59+
try:
60+
struct.pack('bb', 3, 1, 4)
61+
except:
62+
print('struct.error')
63+
4264
# check that we get an error if the buffer is too small
4365
try:
4466
struct.unpack('I', b'\x00\x00\x00')
@@ -96,3 +118,6 @@
96118
print(struct.unpack_from('<b', buf, -11))
97119
except:
98120
print('struct.error')
121+
122+
# check padding bytes
123+
print(struct.pack("xb", 3))

tests/basics/struct_micropython.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

tests/basics/struct_micropython.py.exp

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/misc/non_compliant.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@
105105
except NotImplementedError:
106106
print('NotImplementedError')
107107

108-
# struct pack with too many args, not checked by uPy
109-
print(struct.pack('bb', 1, 2, 3))
110-
111-
# struct pack with too few args, not checked by uPy
112-
print(struct.pack('bb', 1))
113-
114108
# array slice assignment with unsupported RHS
115109
try:
116110
bytearray(4)[0:1] = [1, 2]

tests/misc/non_compliant.py.exp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ NotImplementedError
1414
NotImplementedError
1515
NotImplementedError
1616
NotImplementedError
17-
b'\x01\x02'
18-
b'\x01\x00'
1917
NotImplementedError
2018
AttributeError
2119
TypeError

0 commit comments

Comments
 (0)