-
-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Description
In almost all cases, if a memory allocation in our C & C++ code fails, the failure is handled so that eventually a MemoryError exception is raised by Python. I think that is a general rule that we apply to all low-level code: failure to allocate memory should ultimately result in a Python exception and not crash the interpreter.
Here are a few places where we don't check for a memory allocation failure:
-
numpy/numpy/_core/src/multiarray/dtype_transfer.c
Lines 171 to 173 in f39abd4
_any_to_object_auxdata *res = PyMem_Malloc(sizeof(_any_to_object_auxdata)); res->base = data->base; -
numpy/numpy/_core/src/multiarray/stringdtype/casts.cpp
Lines 856 to 861 in f39abd4
char *buf = (char *)PyMem_RawCalloc(sizeof(char), plen + nlen + slen + 1); // memcpy instead of strcpy/strncat to avoid stringop-truncation warning, // since we are not including the trailing null character char *p = buf; memcpy(p, prefix, plen); -
numpy/numpy/_core/src/multiarray/stringdtype/casts.cpp
Lines 1117 to 1118 in f39abd4
char *buf = (char *)PyMem_RawMalloc(s.size + 1); memcpy(buf, s.buf, s.size); -
char *new_buf = (char *)PyMem_RawCalloc(s1.size, 1); -
char *new_buf = (char *)PyMem_RawCalloc(s.size, 1); -
char *new_buf = (char *)PyMem_RawCalloc(max_size, 1); -
char *new_buf = (char *)PyMem_RawCalloc(new_buf_size, 1);
The following uses of std::unordered_set in unique.cpp can throw an exception if an allocation fails, which will crash Python:
-
numpy/numpy/_core/src/multiarray/unique.cpp
Lines 212 to 214 in f39abd4
std::unordered_set<T *, decltype(hash), decltype(equal)> hashset( std::min(isize, (npy_intp)HASH_TABLE_INITIAL_BUCKETS), hash, equal ); -
numpy/numpy/_core/src/multiarray/unique.cpp
Line 220 in f39abd4
hashset.insert((T *)idata);
(This was not an exhaustive search.)