Skip to content

Commit

Permalink
Int overflow (#629)
Browse files Browse the repository at this point in the history
* Prevent ndarray from overflowing size_t

* Use size_t for polyval array len

* Fix infinite arange

* 6.3.1 version
  • Loading branch information
FelixNumworks committed Jun 22, 2023
1 parent 2cde128 commit 26051d7
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 3 deletions.
4 changes: 4 additions & 0 deletions code/ndarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,10 @@ ndarray_obj_t *ndarray_new_ndarray(uint8_t ndim, size_t *shape, int32_t *strides
ndarray->len = multiply_size(ndarray->len, shape[i-1]);
}

if (SIZE_MAX / ndarray->itemsize <= ndarray->len) {
mp_raise_ValueError(translate("ndarray length overflows"));
}

// if the length is 0, still allocate a single item, so that contractions can be handled
size_t len = multiply_size(ndarray->itemsize, MAX(1, ndarray->len));
uint8_t *array = m_new0(byte, len);
Expand Down
2 changes: 1 addition & 1 deletion code/numpy/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ mp_obj_t create_arange(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_arg
mp_raise_msg(&mp_type_ZeroDivisionError, MP_ERROR_TEXT("divide by zero"));
}

if(isnan(start) || isnan(stop) || isnan(step)) {
if(!isfinite(start) || !isfinite(stop) || !isfinite(step)) {
mp_raise_ValueError(translate("arange: cannot compute length"));
}

Expand Down
2 changes: 1 addition & 1 deletion code/numpy/poly.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ mp_obj_t poly_polyval(mp_obj_t o_p, mp_obj_t o_x) {
}
#endif
// p had better be a one-dimensional standard iterable
uint8_t plen = mp_obj_get_int(mp_obj_len_maybe(o_p));
size_t plen = (size_t)mp_obj_get_int(mp_obj_len_maybe(o_p));
mp_float_t *p = m_new(mp_float_t, plen);
mp_obj_iter_buf_t p_buf;
mp_obj_t p_item, p_iterable = mp_getiter(o_p, &p_buf);
Expand Down
2 changes: 1 addition & 1 deletion code/ulab.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "user/user.h"
#include "utils/utils.h"

#define ULAB_VERSION 6.3.0
#define ULAB_VERSION 6.3.1
#define xstr(s) str(s)
#define str(s) #s

Expand Down

0 comments on commit 26051d7

Please sign in to comment.