Skip to content

Commit 26051d7

Browse files
Int overflow (#629)
* Prevent ndarray from overflowing size_t * Use size_t for polyval array len * Fix infinite arange * 6.3.1 version
1 parent 2cde128 commit 26051d7

File tree

4 files changed

+7
-3
lines changed

4 files changed

+7
-3
lines changed

code/ndarray.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,10 @@ ndarray_obj_t *ndarray_new_ndarray(uint8_t ndim, size_t *shape, int32_t *strides
622622
ndarray->len = multiply_size(ndarray->len, shape[i-1]);
623623
}
624624

625+
if (SIZE_MAX / ndarray->itemsize <= ndarray->len) {
626+
mp_raise_ValueError(translate("ndarray length overflows"));
627+
}
628+
625629
// if the length is 0, still allocate a single item, so that contractions can be handled
626630
size_t len = multiply_size(ndarray->itemsize, MAX(1, ndarray->len));
627631
uint8_t *array = m_new0(byte, len);

code/numpy/create.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ mp_obj_t create_arange(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_arg
158158
mp_raise_msg(&mp_type_ZeroDivisionError, MP_ERROR_TEXT("divide by zero"));
159159
}
160160

161-
if(isnan(start) || isnan(stop) || isnan(step)) {
161+
if(!isfinite(start) || !isfinite(stop) || !isfinite(step)) {
162162
mp_raise_ValueError(translate("arange: cannot compute length"));
163163
}
164164

code/numpy/poly.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ mp_obj_t poly_polyval(mp_obj_t o_p, mp_obj_t o_x) {
161161
}
162162
#endif
163163
// p had better be a one-dimensional standard iterable
164-
uint8_t plen = mp_obj_get_int(mp_obj_len_maybe(o_p));
164+
size_t plen = (size_t)mp_obj_get_int(mp_obj_len_maybe(o_p));
165165
mp_float_t *p = m_new(mp_float_t, plen);
166166
mp_obj_iter_buf_t p_buf;
167167
mp_obj_t p_item, p_iterable = mp_getiter(o_p, &p_buf);

code/ulab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "user/user.h"
3434
#include "utils/utils.h"
3535

36-
#define ULAB_VERSION 6.3.0
36+
#define ULAB_VERSION 6.3.1
3737
#define xstr(s) str(s)
3838
#define str(s) #s
3939

0 commit comments

Comments
 (0)