Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sequtils.delete bug with out of bounds indexes #12506

Merged
merged 1 commit into from
Nov 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/pure/collections/sequtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,9 @@ proc delete*[T](s: var seq[T]; first, last: Natural) =
var dest = @[1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1]
dest.delete(3, 8)
assert outcome == dest

doAssert first <= last
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously sending first > last resulted in a IndexError, which doesn't tell the user much about what went wrong. If sending first > last is supposed to work I can fix it instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, first greater than last should silently delete nothing. This how you define empty sequence with inclusive ranges.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And first >= len should be IndexError

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And first >= len should be IndexError

I actually agree, but it would be inconsistent with how the last parameter is handled (out of bounds allowed) and how system.delete behaves. If first >= len results in IndexError then last >= len should also result in IndexError, which would be a breaking change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some Python influence here too, "oobasfd"[:10000] == 'oobasfd', Python is usually silent about index out of bounds for slices.

if first >= s.len:
return
var i = first
var j = min(len(s), last+1)
var newLen = len(s)-j+i
Expand Down Expand Up @@ -1156,6 +1158,9 @@ when isMainModule:
assert outcome == dest, """\
Deleting range 3-9 from [1,1,1,2,2,2,2,2,2,1,1,1,1,1]
is [1,1,1,1,1,1,1,1]"""
var x = @[1, 2, 3]
x.delete(100, 100)
assert x == @[1, 2, 3]

block: # insert tests
var dest = @[1, 1, 1, 1, 1, 1, 1, 1]
Expand Down