From d2fb8a16ead219545a9edf48d050258bcfb4300e Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Sun, 15 Nov 2026 08:46:03 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Handle=20truncated=20gump=20data?= =?UTF-8?q?=20safely?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A zero run length or a short read at the end of the data left the row decoder spinning forever, and runs that overshot the row width wrote past the image buffer. The decode buffer is now freed on errors as well. --- UOLib/UGump.pas | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/UOLib/UGump.pas b/UOLib/UGump.pas index 5916366..308afe0 100644 --- a/UOLib/UGump.pas +++ b/UOLib/UGump.pas @@ -122,28 +122,35 @@ begin begin AData.Position := AIndex.Lookup; block := TMemoryStream.Create; - block.CopyFrom(AData, AIndex.Size); - block.Position := 0; - for i := 0 to AIndex.Height - 1 do - begin - block.Read(Offset, SizeOf(Integer)); - RowLookup[i] := Offset * 4; - end; - for iCurrentHeight := 0 to AIndex.Height - 1 do - begin - block.Position := RowLookup[iCurrentHeight]; - iCurrentWidth := 0; - while iCurrentWidth < AIndex.Width do + try + block.CopyFrom(AData, AIndex.Size); + block.Position := 0; + for i := 0 to AIndex.Height - 1 do begin - block.Read(Value, SizeOf(Word)); - block.Read(Run, SizeOf(Word)); - if Value > 0 then Value := Value or $8000; //Set alpha bit of non-black colors - for i := 0 to Run - 1 do - PWordArray(FGraphic.Bits + iCurrentHeight * AIndex.Width * 2)^[iCurrentWidth + i] := Value; - inc(iCurrentWidth, Run); + block.Read(Offset, SizeOf(Integer)); + RowLookup[i] := Offset * 4; end; + for iCurrentHeight := 0 to AIndex.Height - 1 do + begin + block.Position := RowLookup[iCurrentHeight]; + iCurrentWidth := 0; + while iCurrentWidth < AIndex.Width do + begin + if (block.Read(Value, SizeOf(Word)) < SizeOf(Word)) or + (block.Read(Run, SizeOf(Word)) < SizeOf(Word)) or (Run = 0) then + Break; + if Value > 0 then Value := Value or $8000; //Set alpha bit of non-black colors + for i := 0 to Run - 1 do + begin + if iCurrentWidth + i >= AIndex.Width then Break; + PWordArray(FGraphic.Bits + iCurrentHeight * AIndex.Width * 2)^[iCurrentWidth + i] := Value; + end; + inc(iCurrentWidth, Run); + end; + end; + finally + block.Free; end; - block.Free; end; FGraphic.Format := ifA8R8G8B8; end;