Skip to content

Commit fb64b33

Browse files
Clamp read palette indices.
1 parent 8e6532a commit fb64b33

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

src/ImageSharp/Formats/Png/PngScanlineProcessor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,12 @@ public static void ProcessInterlacedPaletteScanline<TPixel>(
199199
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan);
200200
ref Color paletteBase = ref MemoryMarshal.GetReference(palette.Value.Span);
201201
uint offset = pixelOffset + frameControl.XOffset;
202+
int maxIndex = palette.Value.Length - 1;
202203

203204
for (nuint x = offset, o = 0; x < frameControl.XMax; x += increment, o++)
204205
{
205206
uint index = Unsafe.Add(ref scanlineSpanRef, o);
206-
pixel.FromRgba32(Unsafe.Add(ref paletteBase, index).ToRgba32());
207+
pixel.FromRgba32(Unsafe.Add(ref paletteBase, (int)Math.Min(index, maxIndex)).ToRgba32());
207208
Unsafe.Add(ref rowSpanRef, x) = pixel;
208209
}
209210
}

tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,4 +693,12 @@ public void Info_BadZTXT(string file)
693693
string path = Path.GetFullPath(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, file));
694694
_ = Image.Identify(path);
695695
}
696+
697+
[Theory]
698+
[InlineData(TestImages.Png.Bad.Issue2714BadPalette)]
699+
public void Decode_BadPalette(string file)
700+
{
701+
string path = Path.GetFullPath(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, file));
702+
using Image image = Image.Load(path);
703+
}
696704
}

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ public static class Bad
192192

193193
public const string BadZTXT = "Png/issues/bad-ztxt.png";
194194
public const string BadZTXT2 = "Png/issues/bad-ztxt2.png";
195+
196+
public const string Issue2714BadPalette = "Png/issues/Issue_2714.png";
195197
}
196198
}
197199

Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)