From 9ff34504956f193c0d0eb842f79bbf5eba50c613 Mon Sep 17 00:00:00 2001 From: cgohlke Date: Fri, 27 Oct 2017 12:29:19 -0700 Subject: [PATCH 1/3] BUG: Fix memory access violations in is_lexsorted --- pandas/_libs/algos.pyx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/algos.pyx b/pandas/_libs/algos.pyx index d159761c3f5e6..5ce1788bc6d66 100644 --- a/pandas/_libs/algos.pyx +++ b/pandas/_libs/algos.pyx @@ -87,7 +87,7 @@ class NegInfinity(object): @cython.boundscheck(False) def is_lexsorted(list list_of_arrays): cdef: - int i + Py_ssize_t i Py_ssize_t n, nlevels int64_t k, cur, pre ndarray arr @@ -99,11 +99,14 @@ def is_lexsorted(list list_of_arrays): cdef int64_t **vecs = malloc(nlevels * sizeof(int64_t*)) for i in range(nlevels): arr = list_of_arrays[i] + if arr.dtype.name != 'int64': + raise ValueError( + 'Invalid dtype for is_lexsorted [%s]' % arr.dtype.name) vecs[i] = arr.data # Assume uniqueness?? with nogil: - for i in range(n): + for i in range(1, n): for k in range(nlevels): cur = vecs[k][i] pre = vecs[k][i -1] From a2e566c0370b6ad9142b5913e0b2ae2cd076a8a0 Mon Sep 17 00:00:00 2001 From: cgohlke Date: Fri, 27 Oct 2017 12:31:46 -0700 Subject: [PATCH 2/3] TST: Use correct array dtype for libalgos.is_lexsorted --- pandas/tests/test_algos.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 38625bfb29917..df9297312a6f3 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -1219,7 +1219,7 @@ def test_is_lexsorted(): 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0]), + 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype='int64'), np.array([30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 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(): 7, 6, 5, 4, 3, 2, 1, 0, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, - 4, 3, 2, 1, 0])] + 4, 3, 2, 1, 0], dtype='int64')] assert (not libalgos.is_lexsorted(failure)) From 8335ea8f6945e7ccd4bff1513f79047dc5650db5 Mon Sep 17 00:00:00 2001 From: cgohlke Date: Sat, 28 Oct 2017 09:25:44 -0700 Subject: [PATCH 3/3] Use assert --- pandas/_libs/algos.pyx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/_libs/algos.pyx b/pandas/_libs/algos.pyx index 5ce1788bc6d66..a44a7288bda45 100644 --- a/pandas/_libs/algos.pyx +++ b/pandas/_libs/algos.pyx @@ -99,9 +99,7 @@ def is_lexsorted(list list_of_arrays): cdef int64_t **vecs = malloc(nlevels * sizeof(int64_t*)) for i in range(nlevels): arr = list_of_arrays[i] - if arr.dtype.name != 'int64': - raise ValueError( - 'Invalid dtype for is_lexsorted [%s]' % arr.dtype.name) + assert arr.dtype.name == 'int64' vecs[i] = arr.data # Assume uniqueness??