From 155ab56cc63d775cca77d7bedbcac6ee65558818 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 17 Jul 2025 09:33:25 -0700 Subject: [PATCH] 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 --- lib/std/zig.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/std/zig.zig b/lib/std/zig.zig index d113129b7e..51eac1b0a6 100644 --- 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));