Skip to content

Commit 385a940

Browse files
committed
If string is too long, skip bytes at the end instead of throwing an exception.
1 parent f78f234 commit 385a940

File tree

1 file changed

+16
-8
lines changed
  • src/ImageSharp/Formats/Tiff/Compression/Decompressors

1 file changed

+16
-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 (offset + 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 too 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
}

0 commit comments

Comments
 (0)