Skip to content

Properly write non-ASCII file names on Windows for file creation #1059

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

Conversation

jmschonfeld
Copy link
Contributor

Previously, using Data.write(to:) (or FileManager.createFile(atPath:) which calls to that function) with a path that contained non-ASCII characters would crash. This crash was caused by an incorrect buffer length calculation that used String.count instead of the actual length of the buffer (which will differ for non-ASCII paths). Fixing the crash however revealed incorrect behavior where file creation would fail due to providing a UTF-8 file name to _sopen_s. Instead, we need to transcode the file name to UTF-16 and open the file with _wsopen_s to ensure that non-ASCII file names can be opened. The added unit test previously crashed (and then failed when the crash was resolved) but now successfully creates the file.

Resolves #1058

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@compnerd
Copy link
Member

Might be good to pull this into the 6.0 branch.

@jmschonfeld jmschonfeld merged commit 4f6c892 into swiftlang:main Dec 2, 2024
3 checks passed
@jmschonfeld jmschonfeld deleted the windows/non-ascii-file-name-writing branch December 2, 2024 23:12
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.

File creation in directory with umlaut crashes on Windows
3 participants