From 3280fc98f3b0400c4ce0b8c54a157f1858490351 Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Sun, 10 Aug 2025 12:43:21 -0700 Subject: [PATCH] Writer: Delete writePreserve/writeAllPreserve This is one way of partially addressing https://github.com/ziglang/zig/issues/24767 - These functions are unused - These functions are untested - These functions are broken + The same dangling pointer bug from 6219c015d8e8c958d96e5caa5ef0dbab9c414996 exists in `writePreserve` + The order of the bytes preserved in relation to the `bytes` being written can differ depending on unused buffer capacity at the time of the call and the drain implementation. If there ends up being a need for these functions, they can be fixed and added back. --- lib/std/Io/Writer.zig | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/lib/std/Io/Writer.zig b/lib/std/Io/Writer.zig index 997dd1e09f..c05dd99a68 100644 --- a/lib/std/Io/Writer.zig +++ b/lib/std/Io/Writer.zig @@ -498,25 +498,6 @@ pub fn write(w: *Writer, bytes: []const u8) Error!usize { return w.vtable.drain(w, &.{bytes}, 1); } -/// Asserts `buffer` capacity exceeds `preserve_len`. -pub fn writePreserve(w: *Writer, preserve_len: usize, bytes: []const u8) Error!usize { - assert(preserve_len <= w.buffer.len); - if (w.end + bytes.len <= w.buffer.len) { - @branchHint(.likely); - @memcpy(w.buffer[w.end..][0..bytes.len], bytes); - w.end += bytes.len; - return bytes.len; - } - const temp_end = w.end -| preserve_len; - const preserved = w.buffer[temp_end..w.end]; - w.end = temp_end; - defer w.end += preserved.len; - const n = try w.vtable.drain(w, &.{bytes}, 1); - assert(w.end <= temp_end + preserved.len); - @memmove(w.buffer[w.end..][0..preserved.len], preserved); - return n; -} - /// Calls `drain` as many times as necessary such that all of `bytes` are /// transferred. pub fn writeAll(w: *Writer, bytes: []const u8) Error!void { @@ -524,18 +505,6 @@ pub fn writeAll(w: *Writer, bytes: []const u8) Error!void { while (index < bytes.len) index += try w.write(bytes[index..]); } -/// Calls `drain` as many times as necessary such that all of `bytes` are -/// transferred. -/// -/// When draining the buffer, ensures that at least `preserve_len` bytes -/// remain buffered. -/// -/// Asserts `buffer` capacity exceeds `preserve_len`. -pub fn writeAllPreserve(w: *Writer, preserve_len: usize, bytes: []const u8) Error!void { - var index: usize = 0; - while (index < bytes.len) index += try w.writePreserve(preserve_len, bytes[index..]); -} - /// Renders fmt string with args, calling `writer` with slices of bytes. /// If `writer` returns an error, the error is returned from `format` and /// `writer` is not called again.