Skip to content

Commit 73c4a1a

Browse files
Merge pull request #2986 from brianberns/main
When decoding a compressed TIFF, truncate a too-long string instead of throwing an exception
2 parents 3948fa8 + 336b6a2 commit 73c4a1a

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

src/ImageSharp/Formats/Tiff/Compression/Decompressors/LzwString.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,33 @@ public int WriteTo(Span<byte> buffer, int offset)
6969
return 0;
7070
}
7171

72-
if (this.Length == 1)
72+
int available = buffer.Length - offset;
73+
if (available <= 0)
7374
{
74-
buffer[offset] = this.value;
75-
return 1;
75+
return 0;
76+
}
77+
78+
int numToWrite = this.Length;
79+
if (numToWrite > available)
80+
{
81+
numToWrite = available;
7682
}
7783

7884
LzwString e = this;
79-
int endIdx = this.Length - 1;
80-
if (endIdx >= buffer.Length)
85+
86+
// if string is too long, skip bytes at the end
87+
int toSkip = this.Length - numToWrite;
88+
for (int i = 0; i < toSkip; i++)
8189
{
82-
TiffThrowHelper.ThrowImageFormatException("Error reading lzw compressed stream. Either pixel buffer to write to is to small or code length is invalid!");
90+
e = e.previous;
8391
}
8492

85-
for (int i = endIdx; i >= 0; i--)
93+
for (int i = numToWrite - 1; i >= 0; i--)
8694
{
8795
buffer[offset + i] = e.value;
8896
e = e.previous;
8997
}
9098

91-
return this.Length;
99+
return numToWrite;
92100
}
93101
}

tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,4 +833,9 @@ public void TiffDecoder_Decode_Resize<TPixel>(TestImageProvider<TPixel> provider
833833
[WithFile(ExtraSamplesUnspecified, PixelTypes.Rgba32)]
834834
public void TiffDecoder_CanDecode_ExtraSamplesUnspecified<TPixel>(TestImageProvider<TPixel> provider)
835835
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);
836+
837+
[Theory]
838+
[WithFile(Issue2983, PixelTypes.Rgba32)]
839+
public void TiffDecoder_CanDecode_Issue2983<TPixel>(TestImageProvider<TPixel> provider)
840+
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);
836841
}

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,7 @@ public static class Tiff
11641164
public const string IptcData = "Tiff/iptc.tiff";
11651165

11661166
public const string Issue2909 = "Tiff/Issues/Issue2909.tiff";
1167+
public const string Issue2983 = "Tiff/Issues/Issue2983.tiff";
11671168

11681169
public static readonly string[] Multiframes = [MultiframeDeflateWithPreview, MultiframeLzwPredictor /*, MultiFrameDifferentSize, MultiframeDifferentSizeTiled, MultiFrameDifferentVariants,*/
11691170
];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:7bfe1d8660d11111cdf2674aedc43c1362dc8c2ecfab9b74b43a06c7c195863e
3+
size 13311100

0 commit comments

Comments
 (0)