Navigation Menu

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-2483] caret new line String "\r\n" crashes in String.components #4332

Closed
swift-ci opened this issue Aug 25, 2016 · 4 comments
Closed

[SR-2483] caret new line String "\r\n" crashes in String.components #4332

swift-ci opened this issue Aug 25, 2016 · 4 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-2483
Radar None
Original Reporter vadim (JIRA User)
Type Bug
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug, Linux, RunTimeCrash
Assignee @pushkarnk
Priority Medium

md5: d145092765f170e4683f665e97822e31

relates to:

  • SR-1627 String.replacingOccurrences(of:"\r\n" with: "\n") Illegal Instruction (core dumped)

Issue Description:

The following line produces core dump on Linux:
let _ = "\r\n".components(separatedBy: CharacterSet.whitespacesAndNewlines)

@pushkarnk
Copy link
Collaborator

The backtrace is:

4  libFoundation.so 0x00007f140474d8f2 _TTSf4s_n___TFC10Foundation8NSString9substringfT4withVS_8_NSRange_SS + 562
5  libFoundation.so 0x00007f1404741b2a _TFC10Foundation8NSString10componentsfT11separatedByVS_12CharacterSet_GSaSS_ + 474
6  libFoundation.so 0x00007f1404753381 _TFE10FoundationSS10componentsfT11separatedByVS_12CharacterSet_GSaSS_ + 49
7  libFoundation.so 0x00007f140fde1098 _TFE10FoundationSS10componentsfT11separatedByVS_12CharacterSet_GSaSS_ + 191421768

On closer examination I found that the String.init(utf16View) call here, returns nil:
https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSString.swift#L349

The reason for String(utf16View) returning nil can be seen the following code that prints nil on both Darwin and Linux:

let str = "\r\n"
let start = str.utf16.startIndex
let min = start.advanced(by: 1)
let max = start.advanced(by: 1)
print(String(str.utf16[min..<max]))

I'd propose that on line 349 in NSString.swift, we check for nil and return an empty string for a nil.

#647 – This doesn't fix SR-1627

@pushkarnk
Copy link
Collaborator

In short for "\r\n", the utf16View[0..<0] is "" but utf16View[1..<1] is nil

This behaviour is consistent with Darwin

@pushkarnk
Copy link
Collaborator

@pushkarnk
Copy link
Collaborator

"\r\n" is (are?) counted as 1 character.

print("\r\n".characters.count)  //this prints 1

That explains why "\r\n". utf16View[1..<1] returns nil

@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
This issue was closed.
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