diff --git a/pandas/_libs/algos.pyx b/pandas/_libs/algos.pyx index e70ac26a2c28e..e2e93c5242b24 100644 --- a/pandas/_libs/algos.pyx +++ b/pandas/_libs/algos.pyx @@ -180,6 +180,8 @@ def is_lexsorted(list_of_arrays: list) -> bool: n = len(list_of_arrays[0]) cdef int64_t **vecs = malloc(nlevels * sizeof(int64_t*)) + if vecs is NULL: + raise MemoryError() for i in range(nlevels): arr = list_of_arrays[i] assert arr.dtype.name == "int64" diff --git a/pandas/_libs/groupby.pyx b/pandas/_libs/groupby.pyx index 391bb4a3a3fd3..2ff45038d6a3e 100644 --- a/pandas/_libs/groupby.pyx +++ b/pandas/_libs/groupby.pyx @@ -81,6 +81,8 @@ cdef float64_t median_linear_mask(float64_t* a, int n, uint8_t* mask) noexcept n return NaN tmp = malloc((n - na_count) * sizeof(float64_t)) + if tmp is NULL: + raise MemoryError() j = 0 for i in range(n): @@ -118,6 +120,8 @@ cdef float64_t median_linear(float64_t* a, int n) noexcept nogil: return NaN tmp = malloc((n - na_count) * sizeof(float64_t)) + if tmp is NULL: + raise MemoryError() j = 0 for i in range(n): diff --git a/pandas/_libs/hashing.pyx b/pandas/_libs/hashing.pyx index be6958e3315e9..8b424e96973d3 100644 --- a/pandas/_libs/hashing.pyx +++ b/pandas/_libs/hashing.pyx @@ -68,7 +68,11 @@ def hash_object_array( # create an array of bytes vecs = malloc(n * sizeof(char *)) + if vecs is NULL: + raise MemoryError() lens = malloc(n * sizeof(uint64_t)) + if lens is NULL: + raise MemoryError() for i in range(n): val = arr[i] diff --git a/pandas/_libs/hashtable_class_helper.pxi.in b/pandas/_libs/hashtable_class_helper.pxi.in index 629b6b42db852..e8827c58b5924 100644 --- a/pandas/_libs/hashtable_class_helper.pxi.in +++ b/pandas/_libs/hashtable_class_helper.pxi.in @@ -257,7 +257,7 @@ cdef class StringVector(Vector): self.data.n = 0 self.data.m = _INIT_VEC_CAP self.data.data = malloc(self.data.m * sizeof(char *)) - if not self.data.data: + if self.data.data is NULL: raise MemoryError() cdef resize(self): @@ -270,7 +270,7 @@ cdef class StringVector(Vector): orig_data = self.data.data self.data.data = malloc(self.data.m * sizeof(char *)) - if not self.data.data: + if self.data.data is NULL: raise MemoryError() for i in range(m): self.data.data[i] = orig_data[i] @@ -975,6 +975,8 @@ cdef class StringHashTable(HashTable): const char **vecs vecs = malloc(n * sizeof(char *)) + if vecs is NULL: + raise MemoryError() for i in range(n): val = values[i] v = get_c_string(val) @@ -1005,6 +1007,8 @@ cdef class StringHashTable(HashTable): # these by-definition *must* be strings vecs = malloc(n * sizeof(char *)) + if vecs is NULL: + raise MemoryError() for i in range(n): val = values[i] @@ -1041,6 +1045,8 @@ cdef class StringHashTable(HashTable): # these by-definition *must* be strings vecs = malloc(n * sizeof(char *)) + if vecs is NULL: + raise MemoryError() for i in range(n): val = values[i] @@ -1116,6 +1122,8 @@ cdef class StringHashTable(HashTable): # assign pointers and pre-filter out missing (if ignore_na) vecs = malloc(n * sizeof(char *)) + if vecs is NULL: + raise MemoryError() for i in range(n): val = values[i] diff --git a/pandas/_libs/sas.pyx b/pandas/_libs/sas.pyx index 9e1af2cb9c3e7..209e82c6284f5 100644 --- a/pandas/_libs/sas.pyx +++ b/pandas/_libs/sas.pyx @@ -49,7 +49,7 @@ cdef bytes buf_as_bytes(Buffer buf, size_t offset, size_t length): cdef Buffer buf_new(size_t length) except *: cdef uint8_t *data = calloc(length, sizeof(uint8_t)) - if data == NULL: + if data is NULL: raise MemoryError(f"Failed to allocate {length} bytes") return Buffer(data, length) diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 3da0fa182faf3..838b5b9f4595f 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -679,6 +679,8 @@ cdef char* c_strftime(npy_datetimestruct *dts, char *fmt): c_date.tm_isdst = -1 result = malloc(result_len * sizeof(char)) + if result is NULL: + raise MemoryError() strftime(result, result_len, fmt, &c_date)