Skip to content

Cython guard against [c|m|re]alloc failures #57705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pandas/_libs/algos.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ def is_lexsorted(list_of_arrays: list) -> bool:
n = len(list_of_arrays[0])

cdef int64_t **vecs = <int64_t**>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"
Expand Down
4 changes: 4 additions & 0 deletions pandas/_libs/groupby.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ cdef float64_t median_linear_mask(float64_t* a, int n, uint8_t* mask) noexcept n
return NaN

tmp = <float64_t*>malloc((n - na_count) * sizeof(float64_t))
if tmp is NULL:
raise MemoryError()

j = 0
for i in range(n):
Expand Down Expand Up @@ -118,6 +120,8 @@ cdef float64_t median_linear(float64_t* a, int n) noexcept nogil:
return NaN

tmp = <float64_t*>malloc((n - na_count) * sizeof(float64_t))
if tmp is NULL:
raise MemoryError()

j = 0
for i in range(n):
Expand Down
4 changes: 4 additions & 0 deletions pandas/_libs/hashing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ def hash_object_array(

# create an array of bytes
vecs = <char **>malloc(n * sizeof(char *))
if vecs is NULL:
raise MemoryError()
lens = <uint64_t*>malloc(n * sizeof(uint64_t))
if lens is NULL:
raise MemoryError()

for i in range(n):
val = arr[i]
Expand Down
12 changes: 10 additions & 2 deletions pandas/_libs/hashtable_class_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ cdef class StringVector(Vector):
self.data.n = 0
self.data.m = _INIT_VEC_CAP
self.data.data = <char **>malloc(self.data.m * sizeof(char *))
if not self.data.data:
if self.data.data is NULL:
raise MemoryError()

cdef resize(self):
Expand All @@ -270,7 +270,7 @@ cdef class StringVector(Vector):

orig_data = self.data.data
self.data.data = <char **>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]
Expand Down Expand Up @@ -975,6 +975,8 @@ cdef class StringHashTable(HashTable):
const char **vecs

vecs = <const char **>malloc(n * sizeof(char *))
if vecs is NULL:
raise MemoryError()
for i in range(n):
val = values[i]
v = get_c_string(val)
Expand Down Expand Up @@ -1005,6 +1007,8 @@ cdef class StringHashTable(HashTable):

# these by-definition *must* be strings
vecs = <const char **>malloc(n * sizeof(char *))
if vecs is NULL:
raise MemoryError()
for i in range(n):
val = values[i]

Expand Down Expand Up @@ -1041,6 +1045,8 @@ cdef class StringHashTable(HashTable):

# these by-definition *must* be strings
vecs = <const char **>malloc(n * sizeof(char *))
if vecs is NULL:
raise MemoryError()
for i in range(n):
val = values[i]

Expand Down Expand Up @@ -1116,6 +1122,8 @@ cdef class StringHashTable(HashTable):

# assign pointers and pre-filter out missing (if ignore_na)
vecs = <const char **>malloc(n * sizeof(char *))
if vecs is NULL:
raise MemoryError()
for i in range(n):
val = values[i]

Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/sas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <uint8_t *>calloc(length, sizeof(uint8_t))
if data == NULL:
if data is NULL:
raise MemoryError(f"Failed to allocate {length} bytes")
return Buffer(data, length)

Expand Down
2 changes: 2 additions & 0 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,8 @@ cdef char* c_strftime(npy_datetimestruct *dts, char *fmt):
c_date.tm_isdst = -1

result = <char*>malloc(result_len * sizeof(char))
if result is NULL:
raise MemoryError()

strftime(result, result_len, fmt, &c_date)

Expand Down