Skip to content

Commit c0d85cd

Browse files
authored
std.fs.File: limit initial_cap according to max_bytes in readToEndAllocOptions
1 parent b01d6b1 commit c0d85cd

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

lib/std/fs/File.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ pub fn readToEndAllocOptions(
11561156
// The file size returned by stat is used as hint to set the buffer
11571157
// size. If the reported size is zero, as it happens on Linux for files
11581158
// in /proc, a small buffer is allocated instead.
1159-
const initial_cap = (if (size > 0) size else 1024) + @intFromBool(optional_sentinel != null);
1159+
const initial_cap = @min((if (size > 0) size else 1024), max_bytes) + @intFromBool(optional_sentinel != null);
11601160
var array_list = try std.ArrayListAligned(u8, alignment).initCapacity(allocator, initial_cap);
11611161
defer array_list.deinit();
11621162

0 commit comments

Comments
 (0)