diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/File.cs b/src/libraries/System.Private.CoreLib/src/System/IO/File.cs index 751ba5a1e6a275..ceab437c253c3e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/File.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/File.cs @@ -742,7 +742,9 @@ public static byte[] ReadAllBytes(string path) int index = 0; int count = (int)fileLength; - byte[] bytes = new byte[count]; + // The entire array is overwritten by the read loop below (an exception is thrown + // if the file is truncated before all the bytes are read), so it does not need to be zeroed. + byte[] bytes = GC.AllocateUninitializedArray(count); while (count > 0) { int n = RandomAccess.ReadAtOffset(sfh, bytes.AsSpan(index, count), index); @@ -1225,7 +1227,9 @@ private static async Task InternalReadAllBytesAsync(SafeFileHandle sfh, using (sfh) { int index = 0; - byte[] bytes = new byte[count]; + // The entire array is overwritten by the read loop below (an exception is thrown + // if the file is truncated before all the bytes are read), so it does not need to be zeroed. + byte[] bytes = GC.AllocateUninitializedArray(count); do { int n = await RandomAccess.ReadAtOffsetAsync(sfh, bytes.AsMemory(index), index, cancellationToken).ConfigureAwait(false);