@@ -68,10 +68,10 @@ multiply_resolve_descriptors(
68
68
} \
69
69
npy_##shortname factor = *(npy_##shortname *)iin; \
70
70
size_t cursize = is.size; \
71
- /* FIXME: check for overflow? */ \
72
71
size_t newsize = cursize * factor; \
73
- \
74
- if (npy_string_newemptysize (newsize , ops ) < 0 ) { \
72
+ /* newsize can only be less than cursize if there is overflow */ \
73
+ if (((newsize < cursize ) || \
74
+ npy_string_newemptysize (newsize , ops ) < 0 )) { \
75
75
gil_error (PyExc_MemoryError , \
76
76
"Failed to allocate string in string mutiply" ); \
77
77
return -1 ; \
@@ -81,6 +81,8 @@ multiply_resolve_descriptors(
81
81
npy_load_string (ops , & os ); \
82
82
for (size_t i = 0 ; i < (size_t )factor ; i ++ ) { \
83
83
/* excplicitly discard const; initializing new buffer */ \
84
+ /* multiply can't overflow because cursize * factor */ \
85
+ /* has already been checked and doesn't overflow */ \
84
86
memcpy ((char * )os .buf + i * cursize , is .buf , cursize ); \
85
87
} \
86
88
\
@@ -245,6 +247,12 @@ add_strided_loop(PyArrayMethod_Context *context, char *const data[],
245
247
}
246
248
}
247
249
250
+ if ((s1 .size + s2 .size ) < s1 .size ) {
251
+ // overflow
252
+ gil_error (PyExc_MemoryError ,
253
+ "Failed to allocate string in string add" );
254
+ }
255
+
248
256
if (npy_string_newemptysize (s1 .size + s2 .size , ops ) < 0 ) {
249
257
return -1 ;
250
258
}
0 commit comments