diff --git a/S7.Net/PlcAsynchronous.cs b/S7.Net/PlcAsynchronous.cs index 26cf05c..868ddb1 100644 --- a/S7.Net/PlcAsynchronous.cs +++ b/S7.Net/PlcAsynchronous.cs @@ -39,11 +39,16 @@ namespace S7.Net var s7data = await COTP.TSDU.ReadAsync(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]); } diff --git a/S7.Net/PlcSynchronous.cs b/S7.Net/PlcSynchronous.cs index 34fa143..7cb37f4 100644 --- a/S7.Net/PlcSynchronous.cs +++ b/S7.Net/PlcSynchronous.cs @@ -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)