@@ -901,8 +901,7 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
901
901
{
902
902
PyArrayObject_fields * fa ;
903
903
int i ;
904
- size_t sd ;
905
- npy_intp size ;
904
+ npy_intp nbytes ;
906
905
907
906
if (descr -> subarray ) {
908
907
PyObject * ret ;
@@ -929,10 +928,9 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
929
928
return NULL ;
930
929
}
931
930
932
- /* Check dimensions */
933
- size = 1 ;
934
- sd = (size_t ) descr -> elsize ;
935
- if (sd == 0 ) {
931
+ /* Check datatype element size */
932
+ nbytes = descr -> elsize ;
933
+ if (nbytes == 0 ) {
936
934
if (!PyDataType_ISSTRING (descr )) {
937
935
PyErr_SetString (PyExc_TypeError , "Empty data-type" );
938
936
Py_DECREF (descr );
@@ -943,13 +941,14 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
943
941
return NULL ;
944
942
}
945
943
if (descr -> type_num == NPY_STRING ) {
946
- sd = descr -> elsize = 1 ;
944
+ nbytes = descr -> elsize = 1 ;
947
945
}
948
946
else {
949
- sd = descr -> elsize = sizeof (npy_ucs4 );
947
+ nbytes = descr -> elsize = sizeof (npy_ucs4 );
950
948
}
951
949
}
952
950
951
+ /* Check dimensions and multiply them to nbytes */
953
952
for (i = 0 ; i < nd ; i ++ ) {
954
953
npy_intp dim = dims [i ];
955
954
@@ -974,9 +973,10 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
974
973
* multiplying the dimensions together to get the total size of the
975
974
* array.
976
975
*/
977
- if (npy_mul_with_overflow_intp (& size , size , dim )) {
976
+ if (npy_mul_with_overflow_intp (& nbytes , nbytes , dim )) {
978
977
PyErr_SetString (PyExc_ValueError ,
979
- "array is too big." );
978
+ "array is too big; `arr.size * arr.dtype.itemsize` "
979
+ "is larger than the maximum possible size." );
980
980
Py_DECREF (descr );
981
981
return NULL ;
982
982
}
@@ -1015,17 +1015,16 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
1015
1015
}
1016
1016
fa -> strides = fa -> dimensions + nd ;
1017
1017
memcpy (fa -> dimensions , dims , sizeof (npy_intp )* nd );
1018
- if (strides == NULL ) { /* fill it in */
1019
- sd = _array_fill_strides (fa -> strides , dims , nd , sd ,
1020
- flags , & (fa -> flags ));
1018
+ if (strides == NULL ) { /* fill it in */
1019
+ _array_fill_strides (fa -> strides , dims , nd , descr -> elsize ,
1020
+ flags , & (fa -> flags ));
1021
1021
}
1022
1022
else {
1023
1023
/*
1024
1024
* we allow strides even when we create
1025
1025
* the memory, but be careful with this...
1026
1026
*/
1027
1027
memcpy (fa -> strides , strides , sizeof (npy_intp )* nd );
1028
- sd *= size ;
1029
1028
}
1030
1029
}
1031
1030
else {
@@ -1039,19 +1038,18 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
1039
1038
* e.g. shape=(0,) -- otherwise buffer exposure
1040
1039
* (a.data) doesn't work as it should.
1041
1040
*/
1042
-
1043
- if (sd == 0 ) {
1044
- sd = descr -> elsize ;
1041
+ if (nbytes == 0 ) {
1042
+ nbytes = descr -> elsize ;
1045
1043
}
1046
1044
/*
1047
1045
* It is bad to have unitialized OBJECT pointers
1048
1046
* which could also be sub-fields of a VOID array
1049
1047
*/
1050
1048
if (zeroed || PyDataType_FLAGCHK (descr , NPY_NEEDS_INIT )) {
1051
- data = npy_alloc_cache_zero (sd );
1049
+ data = npy_alloc_cache_zero (nbytes );
1052
1050
}
1053
1051
else {
1054
- data = npy_alloc_cache (sd );
1052
+ data = npy_alloc_cache (nbytes );
1055
1053
}
1056
1054
if (data == NULL ) {
1057
1055
PyErr_NoMemory ();
@@ -3772,9 +3770,11 @@ PyArray_FromIter(PyObject *obj, PyArray_Descr *dtype, npy_intp count)
3772
3770
* If data is not given but created here, then flags will be NPY_ARRAY_DEFAULT
3773
3771
* and a non-zero flags argument can be used to indicate a FORTRAN style
3774
3772
* array is desired.
3773
+ *
3774
+ * Dimensions and itemsize must have been checked for validity.
3775
3775
*/
3776
3776
3777
- NPY_NO_EXPORT size_t
3777
+ NPY_NO_EXPORT void
3778
3778
_array_fill_strides (npy_intp * strides , npy_intp * dims , int nd , size_t itemsize ,
3779
3779
int inflag , int * objflags )
3780
3780
{
@@ -3845,7 +3845,7 @@ _array_fill_strides(npy_intp *strides, npy_intp *dims, int nd, size_t itemsize,
3845
3845
* objflags |= (NPY_ARRAY_C_CONTIGUOUS |NPY_ARRAY_F_CONTIGUOUS );
3846
3846
}
3847
3847
}
3848
- return itemsize ;
3848
+ return ;
3849
3849
}
3850
3850
3851
3851
/*
0 commit comments