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-10079] Iterating over a reversed substring can crash #52481

Open
martinr448 opened this issue Mar 11, 2019 · 2 comments
Open

[SR-10079] Iterating over a reversed substring can crash #52481

martinr448 opened this issue Mar 11, 2019 · 2 comments
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 standard library Area: Standard library umbrella

Comments

@martinr448
Copy link

Previous ID SR-10079
Radar None
Original Reporter @martinr448
Type Bug
Environment

Tested with Xcode 10.1 (Swift 4.2) and Xcode 10.2 beta 3 (Swift 5).

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug, RunTimeCrash
Assignee None
Priority Medium

md5: 9ba13aeb1c246a44632281938881173b

Issue Description:

Iterating over a ReversedCollection<String.SubSequence> can crash if the substring starts within an extended grapheme cluster:

let string = "abc\r\nxyz"
let idx = string.range(of: "\n")!.lowerBound
let substring = string[idx...]
let rev = substring.reversed()

let revString = String(rev) // Fatal error: Out of bounds: index < startIndex

The same crash happens with any of the following:

let arr = Array(rev)
for c in rev { print(c) }
print(rev.last!)
@theblixguy
Copy link
Collaborator

Hmm this seems to crash in `public subscript(i: Index) -> Character {}` in `Substring.swift`. When I print the start and end indices of `rev`, then I get empty indices. Printing random elements works but fails when printing the newline character with the same crash. Reversing `rev` and creating a String works as well.

@belkadan
Copy link
Contributor

range(of:) is implemented in terms of NSString and thus doesn't understand grapheme clusters. :-( cc @milseman

@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
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 standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

4 participants