aboutsummaryrefslogtreecommitdiff
path: root/lib/std/Thread.zig
diff options
context:
space:
mode:
authorIsaac Freund <mail@isaacfreund.com>2023-01-22 16:40:00 +0100
committerAndrew Kelley <andrew@ziglang.org>2023-01-29 15:07:06 -0500
commit23b7d28896609e3f01765730599119baf53a56c9 (patch)
treecebad2d68d4c0a3d7059f181af6c2b7e1fff0116 /lib/std/Thread.zig
parent7c2ba950a758b86893bfbe73521b29895f7ac4f0 (diff)
downloadzig-23b7d28896609e3f01765730599119baf53a56c9.tar.gz
zig-23b7d28896609e3f01765730599119baf53a56c9.zip
std: restrict mem.span() and mem.len() to sentinel terminated pointers
These functions are currently footgunny when working with pointers to arrays and slices. They just return the stated length of the array/slice without iterating and looking for the first sentinel, even if the array/slice is a sentinel terminated type. From looking at the quite small list of places in the standard library/compiler that this change breaks existing code, the new code looks to be more readable in all cases. The usage of std.mem.span/len was totally unneeded in most of the cases affected by this breaking change. We could remove these functions entirely in favor of other existing functions in std.mem such as std.mem.sliceTo(), but that would be a somewhat nasty breaking change as std.mem.span() is very widely used for converting sentinel terminated pointers to slices. It is however not at all widely used for anything else. Therefore I think it is better to break these few non-standard and potentially incorrect usages of these functions now and at some later time, if deemed worthwhile, finally remove these functions. If we wait for at least a full release cycle so that everyone adapts to this change first, updating for the removal could be a simple find and replace without needing to worry about the semantics.
Diffstat (limited to 'lib/std/Thread.zig')
-rw-r--r--lib/std/Thread.zig2
1 files changed, 1 insertions, 1 deletions
diff --git a/lib/std/Thread.zig b/lib/std/Thread.zig
index d27474584f..8004f94d7f 100644
--- a/lib/std/Thread.zig
+++ b/lib/std/Thread.zig
@@ -166,7 +166,7 @@ pub const GetNameError = error{
pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]const u8 {
buffer_ptr[max_name_len] = 0;
- var buffer = std.mem.span(buffer_ptr);
+ var buffer: [:0]u8 = buffer_ptr;
switch (target.os.tag) {
.linux => if (use_pthreads and is_gnu) {