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-7455] Difference in Data to String between iOS and Linux #4025

Closed
swift-ci opened this issue Apr 17, 2018 · 3 comments
Closed

[SR-7455] Difference in Data to String between iOS and Linux #4025

swift-ci opened this issue Apr 17, 2018 · 3 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-7455
Radar None
Original Reporter andrew.lees11@ibm.com (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Tested on:

MacOS High Sierra Version 10.13.3
running Xcode 9.3 and swift 4.1

Ubuntu 14.04
Running swift 4.1
(Same results running: trunk Development master)

Additional Detail from JIRA
Votes 1
Component/s Foundation
Labels Bug, Linux
Assignee None
Priority Medium

md5: c5658bb6ac71236289a1eb0d264f1e62

Issue Description:

There is a difference in behavior with how the null character is handled when converting between Data and UTF8 between Linux and iOS.

When using the following code snippet:

import Foundation

var testString = "\u{00} This is a test string"
var somedata = testString.data(using: String.Encoding.utf8)!
print(somedata)
if let text = String(data: somedata, encoding: .utf8) {
    print(text.count)
    print(text)
}

somedata will be "23 bytes"

On iOS:
text.count will be 23
text will be " This is a test string"

On linux:
text.count will be 0
text will be ""

So in the conversion from data to utf8 String, Linux will stop at the null character and ignore anything after it whereas iOS will convert everything including and after the null character.

@kirilltitov
Copy link

Confirm, in Linux all strings are null-terminated, bytesNoCopy initializer didn't help as well

@spevans
Copy link
Collaborator

spevans commented Aug 29, 2018

#1677

@spevans
Copy link
Collaborator

spevans commented Nov 14, 2018

This is fixed in swift-DEVELOPMENT-SNAPSHOT-2018-11-13

$ cat sr_7455.swift 
import Foundation
 
var testString = "\u{00} This is a test string"
var somedata = testString.data(using: String.Encoding.utf8)!
print(somedata)
if let text = String(data: somedata, encoding: .utf8) {
    print(text.count)
    print(text)
}
$ ~/swift-DEVELOPMENT-SNAPSHOT-2018-11-13-a-ubuntu18.04/usr/bin/swift sr_7455.swift 
23 bytes
23
 This is a test string

@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

3 participants