Skip to content

Commit 114a84d

Browse files
authored
Cython guard against [c|m|re]alloc failures (pandas-dev#57705)
1 parent eb55bca commit 114a84d

File tree

6 files changed

+23
-3
lines changed

6 files changed

+23
-3
lines changed

pandas/_libs/algos.pyx

+2
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ def is_lexsorted(list_of_arrays: list) -> bool:
180180
n = len(list_of_arrays[0])
181181

182182
cdef int64_t **vecs = <int64_t**>malloc(nlevels * sizeof(int64_t*))
183+
if vecs is NULL:
184+
raise MemoryError()
183185
for i in range(nlevels):
184186
arr = list_of_arrays[i]
185187
assert arr.dtype.name == "int64"

pandas/_libs/groupby.pyx

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ cdef float64_t median_linear_mask(float64_t* a, int n, uint8_t* mask) noexcept n
8181
return NaN
8282

8383
tmp = <float64_t*>malloc((n - na_count) * sizeof(float64_t))
84+
if tmp is NULL:
85+
raise MemoryError()
8486

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

120122
tmp = <float64_t*>malloc((n - na_count) * sizeof(float64_t))
123+
if tmp is NULL:
124+
raise MemoryError()
121125

122126
j = 0
123127
for i in range(n):

pandas/_libs/hashing.pyx

+4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ def hash_object_array(
6868

6969
# create an array of bytes
7070
vecs = <char **>malloc(n * sizeof(char *))
71+
if vecs is NULL:
72+
raise MemoryError()
7173
lens = <uint64_t*>malloc(n * sizeof(uint64_t))
74+
if lens is NULL:
75+
raise MemoryError()
7276

7377
for i in range(n):
7478
val = arr[i]

pandas/_libs/hashtable_class_helper.pxi.in

+10-2
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ cdef class StringVector(Vector):
257257
self.data.n = 0
258258
self.data.m = _INIT_VEC_CAP
259259
self.data.data = <char **>malloc(self.data.m * sizeof(char *))
260-
if not self.data.data:
260+
if self.data.data is NULL:
261261
raise MemoryError()
262262

263263
cdef resize(self):
@@ -270,7 +270,7 @@ cdef class StringVector(Vector):
270270

271271
orig_data = self.data.data
272272
self.data.data = <char **>malloc(self.data.m * sizeof(char *))
273-
if not self.data.data:
273+
if self.data.data is NULL:
274274
raise MemoryError()
275275
for i in range(m):
276276
self.data.data[i] = orig_data[i]
@@ -975,6 +975,8 @@ cdef class StringHashTable(HashTable):
975975
const char **vecs
976976

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

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

@@ -1041,6 +1045,8 @@ cdef class StringHashTable(HashTable):
10411045

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

@@ -1116,6 +1122,8 @@ cdef class StringHashTable(HashTable):
11161122

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

pandas/_libs/sas.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ cdef bytes buf_as_bytes(Buffer buf, size_t offset, size_t length):
4949

5050
cdef Buffer buf_new(size_t length) except *:
5151
cdef uint8_t *data = <uint8_t *>calloc(length, sizeof(uint8_t))
52-
if data == NULL:
52+
if data is NULL:
5353
raise MemoryError(f"Failed to allocate {length} bytes")
5454
return Buffer(data, length)
5555

pandas/_libs/tslibs/period.pyx

+2
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,8 @@ cdef char* c_strftime(npy_datetimestruct *dts, char *fmt):
679679
c_date.tm_isdst = -1
680680

681681
result = <char*>malloc(result_len * sizeof(char))
682+
if result is NULL:
683+
raise MemoryError()
682684

683685
strftime(result, result_len, fmt, &c_date)
684686

0 commit comments

Comments
 (0)