Skip to content

Commit be98e8c

Browse files
committed
Check the return value of Read
Fixes #1221 If Read returns 0 unexpectedly, we have reached the end of the stream.
1 parent b67f4fb commit be98e8c

File tree

1 file changed

+13
-2
lines changed
  • projects/RabbitMQ.Client/client/impl

1 file changed

+13
-2
lines changed

projects/RabbitMQ.Client/client/impl/Frame.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,20 +237,31 @@ internal static InboundFrame ReadFrom(Stream reader, byte[] frameHeaderBuffer, A
237237
break;
238238
}
239239

240-
reader.Read(frameHeaderBuffer, 0, frameHeaderBuffer.Length);
240+
if (reader.Read(frameHeaderBuffer, 0, frameHeaderBuffer.Length) == 0)
241+
{
242+
throw new EndOfStreamException("Reached the end of the stream while reading the frame header length.");
243+
}
244+
241245
int channel = NetworkOrderDeserializer.ReadUInt16(new ReadOnlySpan<byte>(frameHeaderBuffer));
242246
int payloadSize = NetworkOrderDeserializer.ReadInt32(new ReadOnlySpan<byte>(frameHeaderBuffer, 2, 4)); // FIXME - throw exn on unreasonable value
243247

244248
const int EndMarkerLength = 1;
245249
// Is returned by InboundFrame.Dispose in Connection.MainLoopIteration
246250
var readSize = payloadSize + EndMarkerLength;
247251
byte[] payloadBytes = pool.Rent(readSize);
252+
int read = 0;
248253
int bytesRead = 0;
249254
try
250255
{
251256
while (bytesRead < readSize)
252257
{
253-
bytesRead += reader.Read(payloadBytes, bytesRead, readSize - bytesRead);
258+
read = reader.Read(payloadBytes, bytesRead, readSize - bytesRead);
259+
if (read == 0)
260+
{
261+
throw new EndOfStreamException("Reached the end of the stream while reading frame payload.");
262+
}
263+
264+
bytesRead += read;
254265
}
255266
}
256267
catch (Exception)

0 commit comments

Comments
 (0)