Closed Neradoc closed 1 year ago
In micropython they do this fwiw:
// If we run out of args then we just finish; CPython would raise struct.error
while (cnt-- && i < n_args) {
mp_binary_set_val(fmt_type, *fmt, args[i++], p_base, &p);
}
(so their behavior, of consistently getting 0-bytes in this case, is intentional)
CircuitPython version
Code/REPL
Behavior
The error when giving too few arguments is "can't convert to int". There is in fact no test in the code for the end of the list of arguments.
It looks like the inner loop for a format with a number like
"4H"
doesn't check for the number of arguments while reading and will (not so) happily read out of bounds. I guess that explains why having less arguments the second time does this, since it then reads the arguments from the previous call, which happen to be in the non allocated area of the stack.No test for
i < n_args
: https://github.com/adafruit/circuitpython/blob/c93560144ba7c93d47ecd7743df9ada3c497a9de/shared-module/struct/__init__.c#L157-L165A similar issue seems to exist in
unpack_from
but I didn't look into it.Description
No response
Additional information
No response