Skip to content

Commit b3e8f69

Browse files
cgohlkepeterpanmj
authored andcommitted
BUG: Fix memory access violations in is_lexsorted (pandas-dev#18005)
1 parent 2f7a5d6 commit b3e8f69

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

pandas/_libs/algos.pyx

+3-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class NegInfinity(object):
8787
@cython.boundscheck(False)
8888
def is_lexsorted(list list_of_arrays):
8989
cdef:
90-
int i
90+
Py_ssize_t i
9191
Py_ssize_t n, nlevels
9292
int64_t k, cur, pre
9393
ndarray arr
@@ -99,11 +99,12 @@ def is_lexsorted(list list_of_arrays):
9999
cdef int64_t **vecs = <int64_t**> malloc(nlevels * sizeof(int64_t*))
100100
for i in range(nlevels):
101101
arr = list_of_arrays[i]
102+
assert arr.dtype.name == 'int64'
102103
vecs[i] = <int64_t*> arr.data
103104

104105
# Assume uniqueness??
105106
with nogil:
106-
for i in range(n):
107+
for i in range(1, n):
107108
for k in range(nlevels):
108109
cur = vecs[k][i]
109110
pre = vecs[k][i -1]

pandas/tests/test_algos.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ def test_is_lexsorted():
12191219
1, 1, 1, 1, 1, 1, 1,
12201220
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12211221
0, 0, 0, 0, 0, 0, 0,
1222-
0, 0, 0, 0, 0, 0, 0, 0, 0]),
1222+
0, 0, 0, 0, 0, 0, 0, 0, 0], dtype='int64'),
12231223
np.array([30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
12241224
15, 14,
12251225
13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 30, 29, 28,
@@ -1231,7 +1231,7 @@ def test_is_lexsorted():
12311231
7, 6, 5, 4, 3, 2, 1, 0, 30, 29, 28, 27, 26, 25, 24, 23, 22,
12321232
21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7,
12331233
6, 5,
1234-
4, 3, 2, 1, 0])]
1234+
4, 3, 2, 1, 0], dtype='int64')]
12351235

12361236
assert (not libalgos.is_lexsorted(failure))
12371237

0 commit comments

Comments
 (0)