Commit 155ab56cc6

Andrew Kelley <andrew@ziglang.org>
2025-07-17 18:33:25
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
1 parent 5784500
Changed files (1)
lib
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));