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-1627] String.replacingOccurrences(of:"\r\n" with: "\n") Illegal Instruction (core dumped) #4572

Closed
swift-ci opened this issue May 26, 2016 · 4 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-1627
Radar None
Original Reporter carlosbh (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Linux Ubuntu:
"Welcome to Ubuntu 14.04.4 LTS (GNU/Linux 3.13.0-86-generic x86_64)"
Swift version:
swift-DEVELOPMENT-SNAPSHOT-2016-05-09-a-ubuntu14.04

Additional Detail from JIRA
Votes 3
Component/s Foundation, Standard Library
Labels Bug, Linux, RunTimeCrash
Assignee mamabusi (JIRA)
Priority Medium

md5: 512ba86e0dcfe65d34df2f6e3e84cb6c

relates to:

  • SR-2483 caret new line String "\r\n" crashes in String.components

Issue Description:

Hi,

I’m currently using swift to build a project, and have found a pretty hard to bypass roadBlock.

The main workflow of the project is

  • Read a csv file

  • Extract the values of the rows and columns

  • Process data extracted from file

  • Write some results to another file

Pretty simple and straightforward. Got it working on OSX with 2016-05-09 (a) toolchain without problems, using the CSV parser from https://github.com/Daniel1of1/CSwiftV with the minimal modifications to compile with swift 3.

But i need it to compile and work with linux ubuntu.
I prepared a work environment with the instructions in https://www.raywenderlich.com/122189/introduction-to-open-source-swift-on-linux
and it works and compiles swift code wonderfully, even installed the same snapshot of the swift toolchain to make it all happy.

The problem i found is some of the string functionality i need for the csv parser to work, is not working ok in linux.

  • String.replacingOccurences(of: stringToFind, with: stringToPutInstead)
    does not work the same in both environments.

i have a csv file obtainable from https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients
That contains the pattern “\r\n” and i need it to be replaced with “\n” to later on process the rest of the file.

the code i use is:

        #if os(Linux)
            print("Let's change those carriage returns...")
            processedString = string.replacingOccurrences(of: "\r\n", with: "\n")
        #else
            processedString = string.replacingOccurrences(of: "\r\n", with: "\n")
        #endif

And it compiles flawlessly in linux, but when it’s time to execute, it throws an error message like:
Let's change those carriage returns...
Illegal instruction (core dumped)
I tried escaping the pattern ( “
r
n”) and it does not throw the error, but neither replaces the code
When i tried executing the same code in the Linux virtual machine in the REPL environment, the sequence of events was:

 1> import Foundation
  2> let path = "/vagrant/credit_card.csv" 
path: String = "/vagrant/credit_card.csv"
  3> let text = NSString(contentsOfFile: path as String, encoding: NSUTF8StringEncoding) 
text: Foundation.NSString = {
  Foundation.NSObject = {}
  _cfinfo = {
    info = 1920
    pad = 0
  }
  _storage = "ID,LIMIT_BAL,SEX,EDUCATION,MARRIAGE,AGE,PAY_0,PAY_2,PAY_3,PAY_4,PAY_5,PAY_6,BILL_AMT1,BILL_AMT2,BILL_AMT3,BILL_AMT4,BILL_AMT5,BILL_AMT6,PAY_AMT1,PAY_AMT2,PAY_AMT3,PAY_AMT4,PAY_AMT5,PAY_AMT6,default payment next month\r\n1,20000,2,2,1,24,2,2,-1,-1,-2,-2,3913,3102,689,0,0,0,0,689,0,0,0,0,1\r\n2,120000,2,2,2,26,-1,2,0,0,0,2,2682,1725,2682,3272,3455,3261,0,1000,1000,1000,0,2000,1\r\n3,90000,2,2,2,34,0,0,0,0,0,0,29239,14027,13559,14331,14948,15549,1518,1500,1000,1000,1000,5000,0\r\n4,50000,2,2,1,37,0,0,0,0,0,0,46990,48233,49291,28314,28959,29547,2000,2019,1200,1100,1069,1000,0\r\n5,50000,1,2,1,57,-1,0,-1,0,0,0,8617,5670,35835,20940,19146,19131,2000,36681,10000,9000,689,679,0\r\n6,50000,1,1,2,37,0,0,0,0,0,0,64400,57069,57608,19394,19619,20024,2500,1815,657,1000,1000,800,0\r\n7,500000,1,1,2,29,0,0,0,0,0,0,367965,412023,445007,542653,483003,473944,55000,40000,38000,20239,13750,13770,0\r\n8,100000,2,2,2,23,0,-1,-1,0,0,-1,11876,380,601,221,-159,567,380,601,0,581,1687,1542,0\r\n9,140000,2,3,1,28,0,0,2,0,0,0,11285,14096,12108,12211,11793,"...
}
  4> var tmp = text.replacingOccurrences(of: "\r\n", with: "\n")
tmp: String = {
  _core = {
    _baseAddress = <extracting data from value failed>

    _countAndFlags = <extracting data from value failed>

    _owner = <extracting data from value failed>

  }
}
Execution interrupted. Enter code to recover and continue.
Enter LLDB commands to investigate (type :help for assistance.)

And i found myself without more options to know what’s happening.

@swift-ci
Copy link
Contributor Author

Comment by Carlos Barrera Hernandez (JIRA)

I attached a simple swift file that when "swift exampleCodeThatProducesCoreDump.swift" is executed, produces a core dump with the following stack:

0  swift            0x000000000313efc8 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift            0x000000000313d796 llvm::sys::RunSignalHandlers() + 54
2  swift            0x000000000313fafa
3  libpthread.so.0  0x00007f0b95cfd340
4  libFoundation.so 0x00007f0b8ff75d5c _TTSf4s_gs_n___TFC10Foundation15NSMutableString17replaceCharactersfT2inVS_8_NSRange4withSS_T_ + 1468
5  libFoundation.so 0x00007f0b8ff76245 _TTSf4gs_gs_n_s_n___TFC10Foundation15NSMutableString18replaceOccurrencesfT2ofSS4withSS7optionsVS_22NSStringCompareOptions5rangeVS_8_NSRange_Si + 1141
6  libFoundation.so 0x00007f0b8ff768ab _TTSf4gs_gs_n_s_n___TFC10Foundation8NSString20replacingOccurrencesfT2ofSS4withSS7optionsVS_22NSStringCompareOptions5rangeVS_8_NSRange_SS + 971
7  libFoundation.so 0x00007f0b8ff652b8 _TFC10Foundation8NSString20replacingOccurrencesfT2ofSS4withSS_SS + 408
8  libFoundation.so 0x00007f0b8ff7be58 _TFE10FoundationSS20replacingOccurrencesfT2ofSS4withSS7optionsVS_22NSStringCompareOptions5rangeGSqGVs5RangeVVSS13CharacterView5Index___SS + 312
9  libFoundation.so 0x00007f0b960a41e9 _TFE10FoundationSS20replacingOccurrencesfT2ofSS4withSS7optionsVS_22NSStringCompareOptions5rangeGSqGVs5RangeVVSS13CharacterView5Index___SS + 101876937
10 swift            0x0000000000bd3be4 llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 996
11 swift            0x0000000000bd71df llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, std::vector<std::string, std::allocator<std::string> > const&, char const* const*) + 1215
12 swift            0x00000000007d0bda swift::RunImmediately(swift::CompilerInstance&, std::vector<std::string, std::allocator<std::string> > const&, swift::IRGenOptions&, swift::SILOptions const&) + 2090
13 swift            0x00000000007cac8e
14 swift            0x00000000007c5e86 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2838
15 swift            0x0000000000791f18 main + 2840
16 libc.so.6        0x00007f0b946bfec5 __libc_start_main + 245
17 swift            0x000000000078f936
Stack dump:
0.  Program arguments: /home/vagrant/swift-DEVELOPMENT-SNAPSHOT-2016-05-09-a-ubuntu14.04/usr/bin/swift -frontend -interpret exampleCodeThatProducesCoreDump.swift -target x86_64-unknown-linux-gnu -disable-objc-interop -color-diagnostics -module-name exampleCodeThatProducesCoreDump 
Illegal instruction (core dumped)

@swift-ci
Copy link
Contributor Author

Comment by Carlos Barrera Hernandez (JIRA)

Just to help pinpoint the problem, i managed to obtain the functionality using a workaround:
let toPurge = NSCharacterSet(charactersIn: "\r")
processedString = string.components(separatedBy: toPurge).reduce("", combine: {$0 + $1})
works flawlessly, because it technically takes the "\r" out of the components, and then we rejoin them again.
It works, but my spider senses tell me maybe it's not the most efficient solution available.
Hope it further helps pinpoint the problem.

@swift-ci
Copy link
Contributor Author

Comment by Oleksandr Rudavka (JIRA)

carlosbh (JIRA User) gabhaska (JIRA User)
I faced the same bug! And I can tell more details:
it works only if you try to call components(separatedBy) function to string that contains "\r\n" in it.
If it has same characters in reverse order - everything is fine: "\n\r" runs successfully.
If there's at least 1 other symbol between them - it runs successfully: \n_\r".
This is really weird :\
Hope it helped!

@spevans
Copy link
Collaborator

spevans commented Aug 26, 2018

This was fixed in swift 3.1

$ ~/swift-3.1-RELEASE-ubuntu16.04/usr/bin/swift exampleCodeThatProducesCoreDump.swift 
Testing
some string
stuff that
should work ok in
theory
 

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 6, 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