Skip to content

Commit be66c27

Browse files
committed
BUG: possible out of bounds access on 0-length Series, close #917
1 parent 38db00f commit be66c27

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

pandas/src/util.pxd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ cdef inline object get_value_at(ndarray arr, object loc):
2222
i = <Py_ssize_t> loc
2323
sz = cnp.PyArray_SIZE(arr)
2424

25-
if i < 0:
25+
if i < 0 and sz > 0:
2626
i += sz
27-
elif i >= sz:
27+
elif i >= sz or sz == 0:
2828
raise IndexError('index out of bounds')
2929

3030
return get_value_1d(arr, i)

pandas/tests/test_series.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,10 @@ def test_getitem_out_of_bounds(self):
465465
# don't segfault, GH #495
466466
self.assertRaises(IndexError, self.ts.__getitem__, len(self.ts))
467467

468+
# GH #917
469+
s = Series([])
470+
self.assertRaises(IndexError, s.__getitem__, -1)
471+
468472
def test_getitem_box_float64(self):
469473
value = self.ts[5]
470474
self.assert_(isinstance(value, np.float64))

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,8 @@ def srcpath(name=None, suffix='.pyx', subdir='src'):
347347
include_dirs=[np.get_include()])
348348

349349
engines_ext = Extension('pandas._engines',
350-
depends=['pandas/src/numpy_helper.h'],
350+
depends=['pandas/src/numpy_helper.h',
351+
'pandas/src/util.pxd'],
351352
sources=[srcpath('engines', suffix=suffix)],
352353
include_dirs=[np.get_include()])
353354

0 commit comments

Comments
 (0)