From 88de4be27b1e190e8b518455cabf0ec778ba7aa3 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Fri, 26 Jan 2024 01:23:33 -0500 Subject: [PATCH] Fix too-long string values --- Extensions.PortableExecutable.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Extensions.PortableExecutable.cs b/Extensions.PortableExecutable.cs index 64dbaec1..951c42ec 100644 --- a/Extensions.PortableExecutable.cs +++ b/Extensions.PortableExecutable.cs @@ -1280,6 +1280,7 @@ namespace SabreTools.Serialization { var stringData = new StringData(); + int dataStartOffset = offset; stringData.Length = data.ReadUInt16(ref offset); stringData.ValueLength = data.ReadUInt16(ref offset); stringData.ResourceType = (VersionResourceType)data.ReadUInt16(ref offset); @@ -1294,7 +1295,8 @@ namespace SabreTools.Serialization if (stringData.ValueLength > 0) { - byte[]? valueBytes = data.ReadBytes(ref offset, stringData.ValueLength * sizeof(ushort)); + int bytesReadable = Math.Min(stringData.ValueLength * sizeof(ushort), stringData.Length - (offset - dataStartOffset)); + byte[]? valueBytes = data.ReadBytes(ref offset, bytesReadable); if (valueBytes != null) stringData.Value = Encoding.Unicode.GetString(valueBytes); }