Skip to content

Files

Latest commit

 

History

History
29 lines (20 loc) · 579 Bytes

File metadata and controls

29 lines (20 loc) · 579 Bytes

Pattern: Inexact read with Stream.Read

Issue: -

Description

Stream.Read and Stream.ReadAsync might return fewer bytes than requested, resulting in unreliable code if the return value isn't checked.

Example of incorrect code:

void Test(Stream stream, byte[] buffer)
{
    stream.Read(buffer, 0, buffer.Length);
}

Example of correct code:

void Test(Stream stream, byte[] buffer)
{
    stream.ReadExactly(buffer);
}

Further Reading