diff options
| author | Ryan Liptak <squeek502@hotmail.com> | 2024-02-13 16:56:50 -0800 |
|---|---|---|
| committer | Ryan Liptak <squeek502@hotmail.com> | 2024-02-24 14:05:24 -0800 |
| commit | 68b87918df9ad82cf3161f323c55f2e238319922 (patch) | |
| tree | c802aea2b636236f767e6c1cdd864f01c2b47d15 /lib/std/Thread.zig | |
| parent | f6b6b8a4ae4780f21c45824d34d8e14b3c3b5037 (diff) | |
| download | zig-68b87918df9ad82cf3161f323c55f2e238319922.tar.gz zig-68b87918df9ad82cf3161f323c55f2e238319922.zip | |
Fix handling of Windows (WTF-16) and WASI (UTF-8) paths
Windows paths now use WTF-16 <-> WTF-8 conversion everywhere, which is lossless. Previously, conversion of ill-formed UTF-16 paths would either fail or invoke illegal behavior.
WASI paths must be valid UTF-8, and the relevant function calls have been updated to handle the possibility of failure due to paths not being encoded/encodable as valid UTF-8.
Closes #18694
Closes #1774
Closes #2565
Diffstat (limited to 'lib/std/Thread.zig')
| -rw-r--r-- | lib/std/Thread.zig | 13 |
1 files changed, 4 insertions, 9 deletions
diff --git a/lib/std/Thread.zig b/lib/std/Thread.zig index daeecc9914..ae794f44af 100644 --- a/lib/std/Thread.zig +++ b/lib/std/Thread.zig @@ -91,7 +91,7 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void { }, .windows => { var buf: [max_name_len]u16 = undefined; - const len = try std.unicode.utf8ToUtf16Le(&buf, name); + const len = try std.unicode.wtf8ToWtf16Le(&buf, name); const byte_len = math.cast(c_ushort, len * 2) orelse return error.NameTooLong; // Note: NT allocates its own copy, no use-after-free here. @@ -157,17 +157,12 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void { } pub const GetNameError = error{ - // For Windows, the name is converted from UTF16 to UTF8 - CodepointTooLarge, - Utf8CannotEncodeSurrogateHalf, - DanglingSurrogateHalf, - ExpectedSecondSurrogateHalf, - UnexpectedSecondSurrogateHalf, - Unsupported, Unexpected, } || os.PrctlError || os.ReadError || std.fs.File.OpenError || std.fmt.BufPrintError; +/// On Windows, the result is encoded as [WTF-8](https://simonsapin.github.io/wtf-8/). +/// On other platforms, the result is an opaque sequence of bytes with no particular encoding. pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]const u8 { buffer_ptr[max_name_len] = 0; var buffer: [:0]u8 = buffer_ptr; @@ -213,7 +208,7 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co )) { .SUCCESS => { const string = @as(*const os.windows.UNICODE_STRING, @ptrCast(&buf)); - const len = try std.unicode.utf16LeToUtf8(buffer, string.Buffer[0 .. string.Length / 2]); + const len = std.unicode.wtf16LeToWtf8(buffer, string.Buffer[0 .. string.Length / 2]); return if (len > 0) buffer[0..len] else null; }, .NOT_IMPLEMENTED => return error.Unsupported, |
