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

[SR-14203] Enforce IndexSet range consistently #4225

Open
swift-ci opened this issue Feb 12, 2021 · 1 comment
Open

[SR-14203] Enforce IndexSet range consistently #4225

swift-ci opened this issue Feb 12, 2021 · 1 comment

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-14203
Radar rdar://problem/74303930
Original Reporter ken (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Improvement
Assignee None
Priority Medium

md5: 1dbcd1cd10f286ca3383989f246531c6

Issue Description:

The IndexSet comments/documentation says:

"The range of valid integer values is 0..<INT_MAX-1. Anything outside this range is an error."

(I'm assuming for the rest of this bug report that this is a documentation mistake, and it should actually read "0..<Int.max". See SR-14192 for details.)

The behavior when you exceed this range is inconsistent.

  • 0 ... Int.max-1: success

  • Int.max (aka NSNotFound): NSRangeException

  • Int.min ... -2: NSRangeException

  • -1: returns (inserted:true) but then strange things happen

For example, if you make a new IndexSet, and then insert -1, and then insert 0, both insertions claim to succeed and it reports count==2, but min()==nil. (In fact, if -1 is the first value inserted, you can keep re-inserting -1, and count will keep incrementing.)

However, if you insert 0 first and then -1, it reports count==1 and min()==0 as expected, though the -1 insertion returned (inserted:true).

Swift has precondition(), and this would be a great place to use it. It would be better to abort consistently when invalid data is first seen, rather than claiming to succeed and then failing in strange ways later.

I believe only 'insert', 'update', and 'init' would need a range check. All of the other funcs/vars which query the IndexSet already safely ignore out-of-range Ints.

@typesanitizer
Copy link

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants