From 9a717a319a8bab854e1f69da652216f167c8009d Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Tue, 29 Dec 2020 22:17:15 -0600 Subject: [PATCH 1/3] use unsigned abs to avoid overflow --- vm/src/sliceable.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/sliceable.rs b/vm/src/sliceable.rs index 1af55078b..d6cf9a462 100644 --- a/vm/src/sliceable.rs +++ b/vm/src/sliceable.rs @@ -391,7 +391,7 @@ impl TryFromObject for SequenceIndex { // Use PySliceableSequence::wrap_index for implementors pub(crate) fn wrap_index(p: isize, len: usize) -> Option { let neg = p.is_negative(); - let p = p.abs().to_usize()?; + let p = p.wrapping_abs() as usize; if neg { len.checked_sub(p) } else if p >= len { From eecebef0e02e8cf3bbf8be6441d4307c024bc0cf Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Wed, 30 Dec 2020 10:56:00 -0600 Subject: [PATCH 2/3] Add test --- extra_tests/snippets/builtin_slice.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/extra_tests/snippets/builtin_slice.py b/extra_tests/snippets/builtin_slice.py index 6c2dd847b..89ef5dba9 100644 --- a/extra_tests/snippets/builtin_slice.py +++ b/extra_tests/snippets/builtin_slice.py @@ -1,4 +1,5 @@ from testutils import assert_raises +import sys a = [] assert a[:] == [] @@ -129,3 +130,5 @@ def test_all_slices(): test_all_slices() + +assert_raises(IndexError, lambda: b[::0], _msg='bytearray index out of range') \ No newline at end of file From 3ca003a94d64ff1a97a3af368fff3bc18b9d8948 Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Wed, 30 Dec 2020 11:53:49 -0600 Subject: [PATCH 3/3] Fix test --- extra_tests/snippets/builtin_slice.py | 3 --- extra_tests/snippets/bytearray.py | 7 ++++++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/extra_tests/snippets/builtin_slice.py b/extra_tests/snippets/builtin_slice.py index 89ef5dba9..6c2dd847b 100644 --- a/extra_tests/snippets/builtin_slice.py +++ b/extra_tests/snippets/builtin_slice.py @@ -1,5 +1,4 @@ from testutils import assert_raises -import sys a = [] assert a[:] == [] @@ -130,5 +129,3 @@ def test_all_slices(): test_all_slices() - -assert_raises(IndexError, lambda: b[::0], _msg='bytearray index out of range') \ No newline at end of file diff --git a/extra_tests/snippets/bytearray.py b/extra_tests/snippets/bytearray.py index 0abfc0127..3550149a3 100644 --- a/extra_tests/snippets/bytearray.py +++ b/extra_tests/snippets/bytearray.py @@ -1,5 +1,6 @@ from testutils import assert_raises import pickle +import sys # new assert bytearray([1, 2, 3]) @@ -753,4 +754,8 @@ b = pickle.loads(pickle.dumps(a, 4)) assert type(a) == type(b) assert a.x == b.x assert a.y == b.y -assert a == b \ No newline at end of file +assert a == b + +a = bytearray() +for i in range(-1, 2, 1): + assert_raises(IndexError, lambda: a[-sys.maxsize - i], _msg='bytearray index out of range') \ No newline at end of file