authorgravatar for 118280419+ruihe774@users.noreply.github.comRui He <118280419+ruihe774@users.noreply.github.com> 2025-01-30 23:42:15+08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-01-30 16:42:15+01:00
logc0d85cda53fd9ea056641d5e91cc8f736b34c0e7
tree1cab079ea3504484aea45e6fb1813b36fc3e0060
parentb01d6b156cf4d273be40a6e6288f4766f71f4a29
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.fs.File: limit initial_cap according to max_bytes in readToEndAllocOptions


1 files changed, 1 insertions(+), 1 deletions(-)

lib/std/fs/File.zig+1-1
......@@ -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