From 7e4825332b685992e8e965ed61b703125b7bdde7 Mon Sep 17 00:00:00 2001 From: Ynse Hoornenborg Date: Wed, 21 Jun 2023 19:14:21 +0200 Subject: [PATCH] Out of range fix --- src/ImageSharp/Formats/Pbm/BinaryDecoder.cs | 3 ++- src/ImageSharp/Formats/Pbm/BinaryEncoder.cs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/Formats/Pbm/BinaryDecoder.cs b/src/ImageSharp/Formats/Pbm/BinaryDecoder.cs index 449548f3d2..f629282340 100644 --- a/src/ImageSharp/Formats/Pbm/BinaryDecoder.cs +++ b/src/ImageSharp/Formats/Pbm/BinaryDecoder.cs @@ -161,7 +161,8 @@ internal class BinaryDecoder for (int x = 0; x < width;) { int raw = stream.ReadByte(); - for (int bit = 0; bit < 8; bit++) + int stopBit = Math.Min(8, width - x); + for (int bit = 0; bit < stopBit; bit++) { bool bitValue = (raw & (0x80 >> bit)) != 0; rowSpan[x] = bitValue ? black : white; diff --git a/src/ImageSharp/Formats/Pbm/BinaryEncoder.cs b/src/ImageSharp/Formats/Pbm/BinaryEncoder.cs index ca1e4eaa2e..b9bc812f0d 100644 --- a/src/ImageSharp/Formats/Pbm/BinaryEncoder.cs +++ b/src/ImageSharp/Formats/Pbm/BinaryEncoder.cs @@ -192,7 +192,8 @@ internal class BinaryEncoder for (int x = 0; x < width;) { int value = previousValue; - for (int i = 0; i < 8; i++) + int stopBit = Math.Min(8, width - x); + for (int i = 0; i < stopBit; i++) { if (rowSpan[x].PackedValue < 128) {