🐛 Handle truncated gump data safely

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.
This commit is contained in:
2026-11-15 22:06:51 +01:00
parent 2c0eb0cebd
commit d2fb8a16ea
+26 -19
View File
@@ -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;