Skip to content

[stdlib] String.debugDescription: Fix quoting behavior #63048

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

Merged
merged 1 commit into from
Jan 17, 2023

Conversation

lorentey
Copy link
Member

@lorentey lorentey commented Jan 16, 2023

String.debugDescription currently fails to protect the opening or closing characters (or characters in an escaped scalar) from combining with the contents of the string itself:

let s = “\u{301}A\n\u{302}B\u{70F}print(s.debugDescription)
// ⟹ “́A\n̂B܏”  (characters: “́, A, \, n̂, B, ܏”)

This can make debug output difficult to read.

This change fixes this by force-escaping the problematic scalars in these cases:

let s = “\u{301}A\n\u{302}B\u{70F}print(s.debugDescription)
// ⟹ “\u{0301}A\n\u{0302}B\u{070F}”

Of course, Unicode scalars that don’t engage in such behavior are still allowed to pass through unchanged:

let s = “Cafe\u{301}print(s.debugDescription)
// ⟹ “Café”

Other types such as Substring, StaticString, String.UTF8View etc. implement CustomDebugStringConvertible by delegating to String, so this PR fixes our output for all of them.

rdar://103972032

@lorentey
Copy link
Member Author

@swift-ci test

@lorentey lorentey requested a review from Azoy January 16, 2023 06:52
@lorentey
Copy link
Member Author

D'oh, the test will only pass with #63046

`String.debugDescription` currently fails to protect the contents of
the string from combining with the opening or closing `”` characters
or one of the characters of a quoted scalar:

```swift
let s = “\u{301}A\n\u{302}B\u{70F}”
print(s.debugDescription)
// ⟹ “́A\n̂B܏”  (characters: “́, A, \, n̂, B, ܏”)
```

This can make debug output difficult to read, as string contents are
allowed to spread over and pollute neighboring meta-characters.

This change fixes this by force-quoting the problematic scalars in
these cases:

```swift
let s = “\u{301}A\n\u{302}B\u{70F}”
print(s.debugDescription)
// ⟹ “\u{301}A\n\u{302}B\u{70F}”
```

Of course, Unicode scalars that don’t engage in such behavior are
still allowed to pass through unchanged:

```swift
let s = “Cafe\u{301}”
print(s.debugDescription)
// ⟹ “Café”
```
@lorentey lorentey force-pushed the fix-String-debugDescription branch from 1355e0e to 1241df3 Compare January 16, 2023 09:16
@lorentey
Copy link
Member Author

@swift-ci test

@lorentey lorentey merged commit 4551d21 into swiftlang:main Jan 17, 2023
@lorentey lorentey deleted the fix-String-debugDescription branch January 17, 2023 02:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants