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-4947] IndexSet iteration fails catastrophically for empty sets that used to have values #47524

Closed
lilyball mannequin opened this issue May 19, 2017 · 3 comments
Closed
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution SDKOverlay

Comments

@lilyball
Copy link
Mannequin

lilyball mannequin commented May 19, 2017

Previous ID SR-4947
Radar None
Original Reporter @lilyball
Type Bug
Status Closed
Resolution Done
Environment

macOS 10.12.5 (16F73)
Apple Swift version 3.1 (swiftlang-802.0.53 clang-802.0.42)
Target: x86_64-apple-macosx10.9

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug, RunTimeCrash, SDKOverlay
Assignee @lilyball
Priority Medium

md5: bd94fd8a4eb24e170a7ac65ec872ae0e

Issue Description:

If you take an empty IndexSet and iterate over it, everything works fine (which is to say, it does nothing). If you insert an index into that set and then remove the index from that set, iterating over the set throws an out of bounds exception.

This only happens on macOS. Running this same code on Linux does not reproduce this issue.

Code:

import Foundation

var set: IndexSet = []

// works
for x in set {
    print(x)
}

set.insert(3)
set.remove(3)

// crashes
for x in set {
    print(x)
}

Output:

2017-05-19 10:57:28.310 unnamed[24927:6321269] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Index 1 out of bounds [0
...0]'
*** First throw call stack:
(
        0   CoreFoundation                      0x00007fffb3dab2cb __exceptionPreprocess + 171
        1   libobjc.A.dylib                     0x00007fffc8bb648d objc_exception_throw + 48
        2   CoreFoundation                      0x00007fffb3e29c3d +[NSException raise:format:] + 205
        3   Foundation                          0x00007fffb576df66 -[NSIndexSet rangeAtIndex:] + 106
        4   libswiftFoundation.dylib            0x0000000104f828de __NSIndexSetRangeAtIndex + 46
        5   libswiftFoundation.dylib            0x0000000104f35de1 _TTSf4g_n___TFFV10Foundation8IndexSetP33_2D7761BAEB66DCEF0A109CF42C1440A76_rangeFT2atSi_G
Vs5RangeSi_U_FCSo10NSIndexSetGS1_Si_ + 65
        6   libswiftFoundation.dylib            0x0000000104ee7c20 _TFV10Foundation8IndexSet9formIndexfT5afterRVS0_5Index_T_ + 80
        7   libswiftFoundation.dylib            0x0000000104ee9470 _TTWV10Foundation8IndexSets14_IndexableBaseS_FS1_9formIndexfT5afterRwx5Index_T_ + 32
        8   libswiftCore.dylib                  0x00000001048cee9f _TFVs16IndexingIterator4nextfT_GSqwx8_Element_ + 559
        9   unnamed                             0x00000001048c8db0 main + 848
        10  libdyld.dylib                       0x00007fffc949b235 start + 1
        11  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
@lilyball
Copy link
Mannequin Author

lilyball mannequin commented May 19, 2017

If I look at set.indices, before any mutation it looks like

DefaultBidirectionalIndices<IndexSet>(_elements: 0 indexes, _startIndex: index 0 in a range of 0..<0 [range #&#8203;1/0], _endIndex: index 0 in a range of 0..<0 [range #&#8203;0/0])

and has a count of 0. After the mutation, it looks like

DefaultBidirectionalIndices<IndexSet>(_elements: 0 indexes, _startIndex: index 3 in a range of 3..<4 [range #&#8203;1/0], _endIndex: index 0 in a range of 0..<0 [range #&#8203;0/0]) 

and asking for its count just hangs (presumably it's trying to iterate the entire 64-bit integer space).

It appears the problem is the startIndex of the post-mutation indices is after the endIndex.

@lilyball
Copy link
Mannequin Author

lilyball mannequin commented May 19, 2017

Submitted as PR #9795.

@lilyball
Copy link
Mannequin Author

lilyball mannequin commented May 22, 2017

Merged in cf364ec.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution SDKOverlay
Projects
None yet
Development

No branches or pull requests

1 participant