Skip to content

Commit 25cf292

Browse files
KevinRansombaronfel
authored andcommitted
Merge pull request #7461 from KevinRansom/cvtres
Fix native resource issue with empty streams
2 parents 5030e29 + 4d6e769 commit 25cf292

1 file changed

Lines changed: 34 additions & 32 deletions

File tree

src/absil/cvtres.fs

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -54,37 +54,39 @@ type CvtResFile() =
5454
static member ReadResFile(stream : Stream) =
5555
let mutable reader = new BinaryReader(stream, Encoding.Unicode)
5656
let mutable resourceNames = new List<RESOURCE>()
57-
let mutable startPos = stream.Position
58-
let mutable initial32Bits = reader.ReadUInt32 ()
59-
if initial32Bits <> uint32 0
60-
then raise <| ResourceException("Stream does not begin with a null resource and is not in .RES format.")
61-
stream.Position <- startPos
62-
while (stream.Position < stream.Length) do
63-
let mutable cbData = reader.ReadUInt32 ()
64-
let mutable cbHdr = reader.ReadUInt32 ()
65-
if cbHdr < 2u * uint32 sizeof<DWORD>
66-
then raise <| ResourceException(String.Format ("Resource header beginning at offset 0x{0:x} is malformed.", (stream.Position - 8L)))
67-
if cbData = 0u
68-
then
69-
stream.Position <- stream.Position + int64 cbHdr - 2L * int64 sizeof<DWORD>
70-
else
71-
let mutable pAdditional = RESOURCE()
72-
pAdditional.HeaderSize <- cbHdr
73-
pAdditional.DataSize <- cbData
74-
pAdditional.pstringType <- CvtResFile.ReadStringOrID (reader)
75-
pAdditional.pstringName <- CvtResFile.ReadStringOrID (reader)
76-
stream.Position <- stream.Position + 3L &&& ~~~3L
77-
pAdditional.DataVersion <- reader.ReadUInt32 ()
78-
pAdditional.MemoryFlags <- reader.ReadUInt16 ()
79-
pAdditional.LanguageId <- reader.ReadUInt16 ()
80-
pAdditional.Version <- reader.ReadUInt32 ()
81-
pAdditional.Characteristics <- reader.ReadUInt32 ()
82-
pAdditional.data <- Array.zeroCreate (int pAdditional.DataSize)
83-
reader.Read (pAdditional.data, 0, pAdditional.data.Length) |> ignore<int>
84-
stream.Position <- stream.Position + 3L &&& ~~~3L
85-
if pAdditional.pstringType.theString = Unchecked.defaultof<_> && (pAdditional.pstringType.Ordinal = uint16 CvtResFile.RT_DLGINCLUDE)
86-
then () (* ERROR ContinueNotSupported *)
87-
else resourceNames.Add (pAdditional)
57+
// The stream might be empty, so let's check
58+
if not (reader.PeekChar() = -1) then
59+
let mutable startPos = stream.Position
60+
let mutable initial32Bits = reader.ReadUInt32 ()
61+
if initial32Bits <> uint32 0
62+
then raise <| ResourceException("Stream does not begin with a null resource and is not in .RES format.")
63+
stream.Position <- startPos
64+
while (stream.Position < stream.Length) do
65+
let mutable cbData = reader.ReadUInt32 ()
66+
let mutable cbHdr = reader.ReadUInt32 ()
67+
if cbHdr < 2u * uint32 sizeof<DWORD>
68+
then raise <| ResourceException(String.Format ("Resource header beginning at offset 0x{0:x} is malformed.", (stream.Position - 8L)))
69+
if cbData = 0u
70+
then
71+
stream.Position <- stream.Position + int64 cbHdr - 2L * int64 sizeof<DWORD>
72+
else
73+
let mutable pAdditional = RESOURCE()
74+
pAdditional.HeaderSize <- cbHdr
75+
pAdditional.DataSize <- cbData
76+
pAdditional.pstringType <- CvtResFile.ReadStringOrID (reader)
77+
pAdditional.pstringName <- CvtResFile.ReadStringOrID (reader)
78+
stream.Position <- stream.Position + 3L &&& ~~~3L
79+
pAdditional.DataVersion <- reader.ReadUInt32 ()
80+
pAdditional.MemoryFlags <- reader.ReadUInt16 ()
81+
pAdditional.LanguageId <- reader.ReadUInt16 ()
82+
pAdditional.Version <- reader.ReadUInt32 ()
83+
pAdditional.Characteristics <- reader.ReadUInt32 ()
84+
pAdditional.data <- Array.zeroCreate (int pAdditional.DataSize)
85+
reader.Read (pAdditional.data, 0, pAdditional.data.Length) |> ignore<int>
86+
stream.Position <- stream.Position + 3L &&& ~~~3L
87+
if pAdditional.pstringType.theString = Unchecked.defaultof<_> && (pAdditional.pstringType.Ordinal = uint16 CvtResFile.RT_DLGINCLUDE)
88+
then () (* ERROR ContinueNotSupported *)
89+
else resourceNames.Add (pAdditional)
8890
resourceNames
8991
static member private ReadStringOrID(fhIn : BinaryReader) =
9092
let mutable (pstring : RESOURCE_STRING) = RESOURCE_STRING()
@@ -720,4 +722,4 @@ type Win32ResourceConversions() =
720722
resWriter.Write (0x0000us)
721723
resWriter.Write (0x00000000u)
722724
resWriter.Write (0x00000000u)
723-
manifestStream.CopyTo (resStream)
725+
manifestStream.CopyTo (resStream)

0 commit comments

Comments
 (0)