zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 155ab56cc63d775cca77d7bedbcac6ee65558818 (tree)
parent 5784500572e3d12b0342cc5b09210c1d1f49edc5
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Thu, 17 Jul 2025 09:33:25 -0700

std.zig.readSourceFileToEndAlloc: avoid resizing

+1 on the ensure total capacity to account for the fact that we add a
null byte before returning.

thanks matklad

Diffstat:
Mlib/std/zig.zig | 3++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/std/zig.zig b/lib/std/zig.zig @@ -536,7 +536,8 @@ pub fn readSourceFileToEndAlloc(gpa: Allocator, file_reader: *std.fs.File.Reader if (file_reader.getSize()) |size| { const casted_size = std.math.cast(u32, size) orelse return error.StreamTooLong; - try buffer.ensureTotalCapacityPrecise(gpa, casted_size); + // +1 to avoid resizing for the null byte added in toOwnedSliceSentinel below. + try buffer.ensureTotalCapacityPrecise(gpa, casted_size + 1); } else |_| {} try file_reader.interface.appendRemaining(gpa, .@"2", &buffer, .limited(max_src_size));