v923z / micropython-ulab

a numpy-like fast vector module for micropython, circuitpython, and their derivatives
https://micropython-ulab.readthedocs.io/en/latest
MIT License
403 stars 111 forks source link

Check that array size doesn't overflow at construction time #578

Closed jepler closed 1 year ago

jepler commented 1 year ago

Now, requesting to allocate an array directly from .ones() or .zeros() that is too big gives the exception 'array is too big', like numpy.

This does depend on a gcc extension, __builtin_mul_overflow, present since at least version 5. This extension is also supported in clang. msvc is probably the only compiler of note that does not support it.

Closes: #576

There are still a few suspicious allocations of multiplied sizes elsewhere, e.g., this one from poly.c:

    XT = m_new(mp_float_t, (deg+1)*leny); // XT is a matrix of shape (deg+1, len) (rows, columns)                                                                                             

there are also some allocations of square arrays that are the same size as square arrays that already exist, so no overflow checking would be needed:

    mp_float_t *tmp = m_new(mp_float_t, N * N);                                                                                                                                               
v923z commented 1 year ago

@jepler Looks good, many thanks!

Could you, please, add a comment in https://github.com/v923z/micropython-ulab/blob/master/docs/ulab-change-log.md, and move the version number in https://github.com/v923z/micropython-ulab/blob/6fcfeda58da8632bb7774858a9bf974afe65d5dd/code/ulab.c#L36

I don't need to see this after that, you can merge your branch.