-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Description
Port, board and/or hardware
any port configured for MICROPY_LONGINT_IMPL_LONGLONG (tested on Unix, Windows and ARM Cortex M4)
MicroPython version
MicroPython v1.25.0-preview.389.gf187c77da.dirty on 2025-03-14; linux [GCC 13.3.0]
Reproduction
The LONGLONG implementation of mp_obj_new_int_from_str_len
is broken. It does not take the sign into account, and assumes the string buffer is nul-terminated, which is wrong as mp_parse_num_integer
is mostly used on vstr buffers.
To reproduce, make a standard Unix build with
#define MICROPY_LONGINT_IMPL MICROPY_LONGINT_IMPL_LONGLONG
- Try to evaluate
json.loads("-9111222333444555666")
It should return a negative number, but returns a positive number.
- Try to evaluate
["9111222333444555666777", 9111222333444555666]
It fails with SyntaxError: invalid syntax for integer with base 10: '9111222333444555666'
Removing the first element of array will make the number parse properly.
This issue can be reproduced using json.loads()
as well.
Test cases that exhibit the issues have been added to tests/extmod/json_loads.py
and submitted as PR #16931
Expected behaviour
- Should return
-9111222333444555666
- Should return
["9111222333444555666777", 9111222333444555666]
Observed behaviour
- Returns 9111222333444555666
- Fails with
SyntaxError: invalid syntax for integer with base 10: '9111222333444555666'
Additional Information
Replacing the call to strtoll
by code similar to mp_parse_num_integer
but using a long long
to accumulate the result does fix the issue. It might however be more efficient in terms of code size to use directly a long long
in mp_parse_num_integer
when compiled with MICROPY_LONGINT_IMPL_LONGLONG
Code of Conduct
Yes, I agree