summaryrefslogtreecommitdiff
path: root/py/binary.c
AgeCommit message (Collapse)Author
2025-06-16py: Fix undefined left shift operations.Jeff Epler
By ensuring the value to be shifted is an unsigned of the appropriate type. This fixes several runtime diagnostics such as: ../../py/binary.c:199:28: runtime error: left shift of 32768 by 16 places cannot be represented in type 'int' Signed-off-by: Jeff Epler <jepler@gmail.com>
2024-07-04all: Use new mp_obj_new_str_from_cstr() function.Jon Foster
Use new function mp_obj_new_str_from_cstr() where appropriate. It simplifies the code, and makes it smaller too. Signed-off-by: Jon Foster <jon@jon-foster.co.uk>
2024-03-20py/binary: Support half-float 'e' format in struct pack/unpack.Matthias Urlichs
This commit implements the 'e' half-float format: 10-bit mantissa, 5-bit exponent. It uses native _Float16 if supported by the compiler, otherwise uses custom bitshifting encoding/decoding routines. Signed-off-by: Matthias Urlichs <matthias@urlichs.de> Signed-off-by: Damien George <damien@micropython.org>
2022-08-23py: Clean up formatting of union definitions.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2022-07-18py/obj: Add static safety checks to mp_obj_is_type().Yonatan Goldschmidt
Commit d96cfd13e3a464862c introduced a regression by breaking existing users of mp_obj_is_type(.., &mp_obj_bool). This function (and associated helpers like mp_obj_is_int()) have some specific nuances, and mistakes like this one can happen again. This commit adds mp_obj_is_exact_type() which behaves like the the old mp_obj_is_type(). The new mp_obj_is_type() has the same prototype but it attempts to statically assert that it's not called with types which should be checked using mp_obj_is_type(). If called with any of these types: int, str, bool, NoneType - it will cause a compilation error. Additional checked types (e.g function types) can be added in the future. Existing users of mp_obj_is_type() with the now "invalid" types, were translated to use mp_obj_is_exact_type(). The use of MP_STATIC_ASSERT() is not bulletproof - usually GCC (and other compilers) can't statically check conditions that are only known during link-time (like variables' addresses comparison). However, in this case, GCC is able to statically detect these conditions, probably because it's the exact same object - `&mp_type_int == &mp_type_int` is detected. Misuses of this function with runtime-chosen types (e.g: `mp_obj_type_t *x = ...; mp_obj_is_type(..., x);` won't be detected. MSC is unable to detect this, so we use MP_STATIC_ASSERT_NOT_MSC(). Compiling with this commit and without the fix for d96cfd13e3a464862c shows that it detects the problem. Signed-off-by: Yonatan Goldschmidt <yon.goldschmidt@gmail.com>
2021-06-24all: Fix signed shifts and NULL access errors from -fsanitize=undefined.Jeff Epler
Fixes the following (the line numbers match commit 0e87459e2bfd07): ../../extmod/crypto-algorithms/sha256.c:49:19: runtime error: left shif... ../../extmod/moduasyncio.c:106:35: runtime error: member access within ... ../../py/binary.c:210:13: runtime error: left shift of negative value -... ../../py/mpz.c:744:16: runtime error: negation of -9223372036854775808 ... ../../py/objint.c:109:22: runtime error: left shift of 1 by 31 places c... ../../py/objint_mpz.c:374:9: runtime error: left shift of 4611686018427... ../../py/objint_mpz.c:374:9: runtime error: left shift of negative valu... ../../py/parsenum.c:106:14: runtime error: left shift of 46116860184273... ../../py/runtime.c:395:33: runtime error: left shift of negative value ... ../../py/showbc.c:177:28: runtime error: left shift of negative value -... ../../py/vm.c:321:36: runtime error: left shift of negative value -1``` Testing was done on an amd64 Debian Buster system using gcc-8.3 and these settings: CFLAGS += -g3 -Og -fsanitize=undefined LDFLAGS += -fsanitize=undefined The introduced TASK_PAIRHEAP macro's conditional (x ? &x->i : NULL) assembles (under amd64 gcc 8.3 -Os) to the same as &x->i, since i is the initial field of the struct. However, for the purposes of undefined behavior analysis the conditional is needed. Signed-off-by: Jeff Epler <jepler@gmail.com>
2021-02-04all: Rename BYTES_PER_WORD to MP_BYTES_PER_OBJ_WORD.Damien George
The "word" referred to by BYTES_PER_WORD is actually the size of mp_obj_t which is not always the same as the size of a pointer on the target architecture. So rename this config value to better reflect what it measures, and also prefix it with MP_. For uses of BYTES_PER_WORD in setting the stack limit this has been changed to sizeof(void *), because the stack usually grows with machine-word sized values (eg an nlr_buf_t has many machine words in it). Signed-off-by: Damien George <damien@micropython.org>
2020-11-11py/binary: Fix sign extension setting wide integer on 32-bit archs.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2020-04-18all: Fix implicit floating point to integer conversions.stijn
These are found when building with -Wfloat-conversion.
2020-04-18all: Fix implicit conversion from double to float.stijn
These are found when building with -Wfloat-conversion.
2020-04-18all: Fix implicit floating point promotion.stijn
Initially some of these were found building the unix coverage variant on MacOS because that build uses clang and has -Wdouble-promotion enabled, and clang performs more vigorous promotion checks than gcc. Additionally the codebase has been compiled with clang and msvc (the latter with warning level 3), and with MICROPY_FLOAT_IMPL_FLOAT to find the rest of the conversions. Fixes are implemented either as explicit casts, or by using the correct type, or by using one of the utility functions to handle floating point casting; these have been moved from nativeglue.c to the public API.
2020-04-18Revert "all: Fix implicit casts of float/double, and signed comparison."stijn
This reverts commit a2110bd3fca59df8b16a2b5fe4645a4af30b06ed. There's nothing inherently wrong with it, but upcoming commits will apply similar fixes in a slightly different way.
2020-04-05all: Use MP_ERROR_TEXT for all error messages.Jim Mussared
2020-03-30all: Fix implicit casts of float/double, and signed comparison.David Lechner
These were found by buiding the unix coverage variant on macOS (so clang compiler). Mostly, these are fixing implicit cast of float/double to mp_float_t which is one of those two and one mp_int_t to size_t fix for good measure.
2020-02-28all: Reformat C and Python source code with tools/codeformat.py.Damien George
This is run with uncrustify 0.70.1, and black 19.10b0.
2020-02-28py: Removing dangling "else" to improve code format consistency.Damien George
2019-09-02py/binary: Change mp_uint_t to size_t for index, size, align args.Damien George
Reduces code size for nan-box builds, otherwise changes nothing.
2019-09-02py/modstruct: Fix struct.pack_into with unaligned offset of native type.Damien George
Following the same fix for unpack.
2019-09-02py/modstruct: Fix struct.unpack with unaligned offset of native type.Tom McDermott
With this patch alignment is done relative to the start of the buffer that is being unpacked, not the raw pointer value, as per CPython. Fixes issue #3314.
2019-05-17various: Update early copyright years to match actual edit history.Damien George
2019-05-17various: Add and update my copyright line based on git history.Paul Sokolovsky
For modules I initially created or made substantial contributions to.
2019-02-12py: Downcase all MP_OBJ_IS_xxx macros to make a more consistent C API.Damien George
These macros could in principle be (inline) functions so it makes sense to have them lower case, to match the other C API functions. The remaining macros that are upper case are: - MP_OBJ_TO_PTR, MP_OBJ_FROM_PTR - MP_OBJ_NEW_SMALL_INT, MP_OBJ_SMALL_INT_VALUE - MP_OBJ_NEW_QSTR, MP_OBJ_QSTR_VALUE - MP_OBJ_FUN_MAKE_SIG - MP_DECLARE_CONST_xxx - MP_DEFINE_CONST_xxx These must remain macros because they are used when defining const data (at least, MP_OBJ_NEW_SMALL_INT is so it makes sense to have MP_OBJ_SMALL_INT_VALUE also a macro). For those macros that have been made lower case, compatibility macros are provided for the old names so that users do not need to change their code immediately.
2017-11-16py/objstr: Remove "make_qstr_if_not_already" arg from mp_obj_new_str.Damien George
This patch simplifies the str creation API to favour the common case of creating a str object that is not forced to be interned. To force interning of a new str the new mp_obj_new_str_via_qstr function is added, and should only be used if warranted. Apart from simplifying the mp_obj_new_str function (and making it have the same signature as mp_obj_new_bytes), this patch also reduces code size by a bit (-16 bytes for bare-arm and roughly -40 bytes on the bare-metal archs).
2017-08-15py/binary.c: Fix bug when packing big-endian 'Q' values.Bas van Sisseren
Without bugfix: struct.pack('>Q', 16) b'\x00\x00\x00\x10\x00\x00\x00\x00' With bugfix: struct.pack('>Q', 16) b'\x00\x00\x00\x00\x00\x00\x00\x10'
2017-07-31all: Use the name MicroPython consistently in commentsAlexander Steffen
There were several different spellings of MicroPython present in comments, when there should be only one.
2017-07-04py/binary: Add missing "break" statements.Damien George
2017-05-09py/binary: Handle storing big-ints to all arrays types.Damien George
Prior to this patch only 'q' and 'Q' type arrays could store big-int values. With this patch any big int that is stored to an array is handled by the big-int implementation, regardless of the typecode of the array. This allows arrays to work with all type sizes on all architectures.
2017-01-17py/binary: mp_binary_get_size: Raise error on unsupported typecodes.Paul Sokolovsky
Previouly, we had errors checked in callers, which led to duplicate code or missing checks in some places.
2016-12-12py/binary: Do zero extension when storing a value larger than word size.Damien George
2015-11-29py: Wrap all obj-ptr conversions in MP_OBJ_TO_PTR/MP_OBJ_FROM_PTR.Damien George
This allows the mp_obj_t type to be configured to something other than a pointer-sized primitive type. This patch also includes additional changes to allow the code to compile when sizeof(mp_uint_t) != sizeof(void*), such as using size_t instead of mp_uint_t, and various casts.
2015-11-27py/binary: Make use of MP_ALIGN.Damien George
2015-10-12py/objarray: Allow to create array of void pointers, as extension to CPython.Paul Sokolovsky
Using 'P' format specifier (matches struct module). This is another shortcut for FFI, just as previously introduced "array of objects" ('O').
2015-09-01py/binary: Add support for array('q') and array('Q').Damien George
2015-05-17py/binary: Make return type of mp_binary_get_size size_t instead of int.Kaspar Schleiser
Fixes sign-compare warning.
2015-04-25py: Fix handling of negative numbers in struct.pack of q/Q.Damien George
2015-04-25py: Support conversion of bignum to bytes.Damien George
This gets int.to_bytes working for bignum, and also struct.pack with 'q' and 'Q' args on 32-bit machines. Addresses issue #1155.
2015-03-05objarray: Support array('O'), array of objects, as extension to CPython.Paul Sokolovsky
Might be useful at least for memoryview hacks.
2015-01-27py: Add support for floats in mp_binary_{get,set}_val()David Steinberg
- This then provides support for floats in the struct package
2015-01-24binary: Rework array accessors. They work with native, not stdint types.Paul Sokolovsky
2015-01-16py, unix: Allow to compile with -Wsign-compare.Damien George
See issue #699.
2015-01-01py: Move to guarded includes, everywhere in py/ core.Damien George
Addresses issue #1022.
2014-12-05py: Rename mp_obj_int_get to mp_obj_int_get_truncated; fix struct.pack.Damien George
mp_obj_int_get_truncated is used as a "fast path" int accessor that doesn't check for overflow and returns the int truncated to the machine word size, ie mp_int_t. Use mp_obj_int_get_truncated to fix struct.pack when packing maximum word sized values. Addresses issues #779 and #998.
2014-11-21py: Add support for float/double arrays in array module.Damien George
Addresses issue #981.
2014-10-06py: Make mp_binary_set_val work on big endian machine.Damien George
2014-10-03py: Convert [u]int to mp_[u]int_t where appropriate.Damien George
Addressing issue #50.
2014-09-10py: Enable struct/binary-helper to parse q and Q sized ints.Damien George
Addresses issue #848.
2014-09-06modstruct: Implement 'O', 'P', 's' types for packed structs.Paul Sokolovsky
This is required to deal with, well, packed C structs containing pointers.
2014-08-30py: Change uint to mp_uint_t in runtime.h, stackctrl.h, binary.h.Damien George
Part of code cleanup, working towards resolving issue #50.
2014-08-10py: binary.c: Properly implement alignment for native unpacked structs.Paul Sokolovsky
2014-07-05binary: Factor out mp_binary_set_int().Paul Sokolovsky