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-10329] String.init(decodingCString:as:) returns non-empty string for empty array with UTF8 encoding #52729

Open
dduan opened this issue Apr 6, 2019 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@dduan
Copy link
Collaborator

dduan commented Apr 6, 2019

Previous ID SR-10329
Radar None
Original Reporter @dduan
Type Bug

Attachment: Download

Environment

Swift 5.0 shipped on Version 10.2 (10E125).

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

md5: fcf9948ec7b20dfa006a0d8178f4d6b3

Issue Description:

The following code returns an non-empty string. While it should return an empty string:

String(decodingCString: [], as: UTF8.self)

@ole
Copy link
Contributor

ole commented Apr 7, 2019

I'm not sure, but I'd expect this to be undefined behavior because the empty array is not a valid C string since the terminating null byte is missing. In contrast, this works fine:

String(decodingCString: "", as: UTF8.self) // returns ""

because the compiler adds the null byte. But it doesn‘t do that for an array. A properly null-terminated C string in array form should look like this:

String(decodingCString: [0], as: UTF8.self) // returns ""

@weissi
Copy link
Member

weissi commented Apr 7, 2019

@ole this doesn't look like a bug to me. If you pass a String there "", Swift will do some magic which under the hood will actually call "".withCString for you. withCString will always give you a NUL terminated String. So that's consistent with what you see.

For array, there's also special Swift magic involved and it'll do withUnsafePointer on the array but that might or might not happen to be 0 terminated.

So I'd expect to see exactly what you're seeing. So String(decodingCString: [], as: Unicode.UTF8.self) does trigger undefined behaviour (as you're reading into arbitrary bits of memory until you find a 0) and may return whatever it wants.

I think the behaviour you want is String(decoding: [], as: Unicode.UTF8.self) which is defined on Collection and won't read past the end.

@ole
Copy link
Contributor

ole commented Apr 7, 2019

@weissi I agree it's not a bug.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

3 participants