Add some response length checks in connection Open()

I don't know what the correct expected connection response size is, so I just added checks for the minimal index access by the current code.

This change will just change NullReferenceExceptions into WrongNumberOfBytesException when the PLC response with not enough data for a connection attempt.
This commit is contained in:
Serge Camille
2020-08-21 21:27:56 +02:00
parent d530f1e422
commit 592d21c3aa
2 changed files with 10 additions and 0 deletions

View File

@@ -34,11 +34,16 @@ namespace S7.Net
var s7data = COTP.TSDU.Read(stream);
if (s7data == null)
throw new WrongNumberOfBytesException("No data received in response to Communication Setup");
if (s7data.Length < 2)
throw new WrongNumberOfBytesException("Not enough data received in response to Communication Setup");
//Check for S7 Ack Data
if (s7data[1] != 0x03)
throw new InvalidDataException("Error reading Communication Setup response", s7data, 1, 0x03);
if (s7data.Length < 20)
throw new WrongNumberOfBytesException("Not enough data received in response to Communication Setup");
MaxPDUSize = (short)(s7data[18] * 256 + s7data[19]);
}
catch (Exception exc)