Skip to content

Commit 155ab56

Browse files
committed
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 commit 155ab56

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

lib/std/zig.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,8 @@ pub fn readSourceFileToEndAlloc(gpa: Allocator, file_reader: *std.fs.File.Reader
536536

537537
if (file_reader.getSize()) |size| {
538538
const casted_size = std.math.cast(u32, size) orelse return error.StreamTooLong;
539-
try buffer.ensureTotalCapacityPrecise(gpa, casted_size);
539+
// +1 to avoid resizing for the null byte added in toOwnedSliceSentinel below.
540+
try buffer.ensureTotalCapacityPrecise(gpa, casted_size + 1);
540541
} else |_| {}
541542

542543
try file_reader.interface.appendRemaining(gpa, .@"2", &buffer, .limited(max_src_size));

0 commit comments

Comments
 (0)