Skip to content

Commit 4b3afc1

Browse files
Fix #2801 and secure allocation
1 parent 63cae5c commit 4b3afc1

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

src/ImageSharp/Formats/Webp/AlphaDecoder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public void Decode()
183183
else
184184
{
185185
this.LosslessDecoder.DecodeImageData(this.Vp8LDec, this.Vp8LDec.Pixels.Memory.Span);
186-
this.ExtractAlphaRows(this.Vp8LDec);
186+
this.ExtractAlphaRows(this.Vp8LDec, this.Width);
187187
}
188188
}
189189

@@ -257,14 +257,15 @@ public void ExtractPalettedAlphaRows(int lastRow)
257257
/// Once the image-stream is decoded into ARGB color values, the transparency information will be extracted from the green channel of the ARGB quadruplet.
258258
/// </summary>
259259
/// <param name="dec">The VP8L decoder.</param>
260-
private void ExtractAlphaRows(Vp8LDecoder dec)
260+
/// <param name="width">The image width.</param>
261+
private void ExtractAlphaRows(Vp8LDecoder dec, int width)
261262
{
262263
int numRowsToProcess = dec.Height;
263-
int width = dec.Width;
264264
Span<uint> input = dec.Pixels.Memory.Span;
265265
Span<byte> output = this.Alpha.Memory.Span;
266266

267267
// Extract alpha (which is stored in the green plane).
268+
// the final width (!= dec->width_)
268269
int pixelCount = width * numRowsToProcess;
269270
WebpLosslessDecoder.ApplyInverseTransforms(dec, input, this.memoryAllocator);
270271
ExtractGreen(input, output, pixelCount);

src/ImageSharp/Formats/Webp/Lossless/LosslessUtils.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,11 @@ private static void SubtractGreenFromBlueAndRedScalar(Span<uint> pixelData)
269269
/// </summary>
270270
/// <param name="transform">The transform data contains color table size and the entries in the color table.</param>
271271
/// <param name="pixelData">The pixel data to apply the reverse transform on.</param>
272-
public static void ColorIndexInverseTransform(Vp8LTransform transform, Span<uint> pixelData)
272+
/// <param name="outputSpan">The resulting pixel data with the reversed transformation data.</param>
273+
public static void ColorIndexInverseTransform(
274+
Vp8LTransform transform,
275+
Span<uint> pixelData,
276+
Span<uint> outputSpan)
273277
{
274278
int bitsPerPixel = 8 >> transform.Bits;
275279
int width = transform.XSize;
@@ -282,7 +286,6 @@ public static void ColorIndexInverseTransform(Vp8LTransform transform, Span<uint
282286
int countMask = pixelsPerByte - 1;
283287
int bitMask = (1 << bitsPerPixel) - 1;
284288

285-
uint[] decodedPixelData = new uint[width * height];
286289
int pixelDataPos = 0;
287290
for (int y = 0; y < height; y++)
288291
{
@@ -298,12 +301,12 @@ public static void ColorIndexInverseTransform(Vp8LTransform transform, Span<uint
298301
packedPixels = GetArgbIndex(pixelData[pixelDataPos++]);
299302
}
300303

301-
decodedPixelData[decodedPixels++] = colorMap[(int)(packedPixels & bitMask)];
304+
outputSpan[decodedPixels++] = colorMap[(int)(packedPixels & bitMask)];
302305
packedPixels >>= bitsPerPixel;
303306
}
304307
}
305308

306-
decodedPixelData.AsSpan().CopyTo(pixelData);
309+
outputSpan.CopyTo(pixelData);
307310
}
308311
else
309312
{

src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ public static void ApplyInverseTransforms(Vp8LDecoder decoder, Span<uint> pixelD
684684
List<Vp8LTransform> transforms = decoder.Transforms;
685685
for (int i = transforms.Count - 1; i >= 0; i--)
686686
{
687+
// TODO: Review these 1D allocations. They could conceivably exceed limits.
687688
Vp8LTransform transform = transforms[i];
688689
switch (transform.TransformType)
689690
{
@@ -701,7 +702,11 @@ public static void ApplyInverseTransforms(Vp8LDecoder decoder, Span<uint> pixelD
701702
LosslessUtils.ColorSpaceInverseTransform(transform, pixelData);
702703
break;
703704
case Vp8LTransformType.ColorIndexingTransform:
704-
LosslessUtils.ColorIndexInverseTransform(transform, pixelData);
705+
using (IMemoryOwner<uint> output = memoryAllocator.Allocate<uint>(transform.XSize * transform.YSize, AllocationOptions.Clean))
706+
{
707+
LosslessUtils.ColorIndexInverseTransform(transform, pixelData, output.GetSpan());
708+
}
709+
705710
break;
706711
}
707712
}

tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,11 +524,11 @@ public void WebpDecoder_CanDecode_Issue2801<TPixel>(TestImageProvider<TPixel> pr
524524
{
525525
WebpEncoder encoder = new()
526526
{
527+
Quality = 100
527528
};
528529

529530
using Image<TPixel> image = provider.GetImage();
530-
image.DebugSave(provider);
531-
image.VerifyEncoder(provider, "webp", string.Empty, encoder);
531+
image.VerifyEncoder(provider, "webp", string.Empty, encoder, ImageComparer.TolerantPercentage(0.0994F));
532532
}
533533

534534
public static void RunEncodeLossy_WithPeakImage()

0 commit comments

Comments
 (0)