diff --git a/RELEASE.rst b/RELEASE.rst index f512b1c255a04..b7099e1b6f767 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -231,6 +231,7 @@ pandas 0.11.0 - PeriodIndex.tolist now boxes to Period (GH3178_) - PeriodIndex.get_loc KeyError now reports Period instead of ordinal (GH3179_) - df.to_records bug when handling MultiIndex (GH3189) + - Bug in integer out-of-bounds indexing in Series causing segfault (GH3168_) .. _GH622: https://github.com/pydata/pandas/issues/622 .. _GH797: https://github.com/pydata/pandas/issues/797 @@ -304,9 +305,13 @@ pandas 0.11.0 .. _GH3075: https://github.com/pydata/pandas/issues/3075 .. _GH3094: https://github.com/pydata/pandas/issues/3094 .. _GH3130: https://github.com/pydata/pandas/issues/3130 +<<<<<<< HEAD .. _GH3178: https://github.com/pydata/pandas/issues/3178 .. _GH3179: https://github.com/pydata/pandas/issues/3179 .. _GH3189: https://github.com/pydata/pandas/issues/3189 +======= +.. _GH3168: https://github.com/pydata/pandas/issues/3168 +>>>>>>> BUG: GH3168 bug in integer out-of-bounds indexing in Series causing segfault (GH3168_) pandas 0.10.1 ============= diff --git a/pandas/src/util.pxd b/pandas/src/util.pxd index 7a30f018e623e..7ccf0e5adc587 100644 --- a/pandas/src/util.pxd +++ b/pandas/src/util.pxd @@ -36,6 +36,8 @@ cdef inline object get_value_at(ndarray arr, object loc): if i < 0 and sz > 0: i += sz + if i > sz or i < 0: + raise IndexError('index out of bounds') elif i >= sz or sz == 0: raise IndexError('index out of bounds') @@ -53,7 +55,9 @@ cdef inline set_value_at(ndarray arr, object loc, object value): if i < 0: i += sz - elif i >= sz: + if i > sz or i < 0: + raise IndexError('index out of bounds') + elif i >= sz or i < 0: raise IndexError('index out of bounds') assign_value_1d(arr, i, value) diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index 353f03eaf70a8..6557c0f5ab391 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -3988,6 +3988,31 @@ def test_int_indexing(self): self.assertRaises(KeyError, s.__getitem__, 'c') self.assertRaises(KeyError, s.__setitem__, 'c', 0) + def test_neg_indices_indexing(self): + + from pandas.util.testing import makeCustomDataframe as mkdf + s = mkdf(10,5,r_idx_type='dt').C_l0_g0 + + # GH 3168 getting + self.assert_(s[0] == 'R0C0') + self.assert_(s[-1] == 'R9C0') + + self.assertRaises(KeyError, s.__getitem__, tuple([-11])) + + # GH 3168 setting + s[0] = 'foo' + self.assert_(s[0] == 'foo') + s[-1] = 'foo' + self.assert_(s[-1] == 'foo') + + self.assertRaises(KeyError, s.__setitem__, tuple([-11]), 'foo') + + # 0-len + s = Series([]) + for l in [0,1,-1]: + self.assertRaises(KeyError, s.__getitem__, tuple([l])) + self.assertRaises(KeyError, s.__setitem__, tuple([l]), 'foo') + def test_datetime_indexing(self): from pandas import date_range diff --git a/pandas/tslib.pyx b/pandas/tslib.pyx index 324627ce91f46..48269d9236b91 100644 --- a/pandas/tslib.pyx +++ b/pandas/tslib.pyx @@ -369,6 +369,8 @@ cpdef object get_value_box(ndarray arr, object loc): if i < 0 and sz > 0: i += sz + if i > sz or i < 0: + raise IndexError('index out of bounds') elif i >= sz or sz == 0: raise IndexError('index out of bounds')