🐛 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:
+9
-2
@@ -122,6 +122,7 @@ begin
|
|||||||
begin
|
begin
|
||||||
AData.Position := AIndex.Lookup;
|
AData.Position := AIndex.Lookup;
|
||||||
block := TMemoryStream.Create;
|
block := TMemoryStream.Create;
|
||||||
|
try
|
||||||
block.CopyFrom(AData, AIndex.Size);
|
block.CopyFrom(AData, AIndex.Size);
|
||||||
block.Position := 0;
|
block.Position := 0;
|
||||||
for i := 0 to AIndex.Height - 1 do
|
for i := 0 to AIndex.Height - 1 do
|
||||||
@@ -135,16 +136,22 @@ begin
|
|||||||
iCurrentWidth := 0;
|
iCurrentWidth := 0;
|
||||||
while iCurrentWidth < AIndex.Width do
|
while iCurrentWidth < AIndex.Width do
|
||||||
begin
|
begin
|
||||||
block.Read(Value, SizeOf(Word));
|
if (block.Read(Value, SizeOf(Word)) < SizeOf(Word)) or
|
||||||
block.Read(Run, SizeOf(Word));
|
(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
|
if Value > 0 then Value := Value or $8000; //Set alpha bit of non-black colors
|
||||||
for i := 0 to Run - 1 do
|
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;
|
PWordArray(FGraphic.Bits + iCurrentHeight * AIndex.Width * 2)^[iCurrentWidth + i] := Value;
|
||||||
|
end;
|
||||||
inc(iCurrentWidth, Run);
|
inc(iCurrentWidth, Run);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
finally
|
||||||
block.Free;
|
block.Free;
|
||||||
end;
|
end;
|
||||||
|
end;
|
||||||
FGraphic.Format := ifA8R8G8B8;
|
FGraphic.Format := ifA8R8G8B8;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user