Merge pull request #308 from scamille/fb-fixWarnings

Fix some null deref warnings.
This commit is contained in:
Michael Croes
2020-09-09 21:03:25 +02:00
committed by GitHub
2 changed files with 5 additions and 1 deletions

View File

@@ -13,6 +13,10 @@ namespace S7.Net.Protocol
public static byte[] SerializeDataItem(DataItem dataItem)
{
if (dataItem.Value == null)
{
throw new Exception($"DataItem.Value is null, cannot serialize. StartAddr={dataItem.StartByteAdr} VarType={dataItem.VarType}");
}
if (dataItem.Value is string s)
return dataItem.VarType == VarType.StringEx
? StringEx.ToByteArray(s, dataItem.Count)

View File

@@ -24,7 +24,7 @@ namespace S7.Net.Types
/// <summary>
/// Converts an array of T repesented as S7 binary data to an array of T
/// </summary>
public static T[] ToArray<T>(byte[] bytes, Func<byte[], T> converter)
public static T[] ToArray<T>(byte[] bytes, Func<byte[], T> converter) where T : struct
{
var typeSize = Marshal.SizeOf(default(T));
var entries = bytes.Length / typeSize;