LibGfx: Validate bit depths, and don't assert on invalid color type

The PNG spec says that each color type only allows certain bit depths,
so add explicit checks for that.
This commit is contained in:
Nico Weber 2020-11-29 14:37:15 -05:00 committed by Andreas Kling
parent fe999d6281
commit 34c5478f31
Notes: sideshowbarker 2024-07-19 01:10:41 +09:00

View file

@ -847,22 +847,32 @@ static bool process_IHDR(const ByteBuffer& data, PNGLoadingContext& context)
switch (context.color_type) {
case 0: // Each pixel is a grayscale sample.
if (context.bit_depth != 1 && context.bit_depth != 2 && context.bit_depth != 4 && context.bit_depth != 8 && context.bit_depth != 16)
return false;
context.channels = 1;
break;
case 4: // Each pixel is a grayscale sample, followed by an alpha sample.
if (context.bit_depth != 8 && context.bit_depth != 16)
return false;
context.channels = 2;
break;
case 2: // Each pixel is an RGB sample
if (context.bit_depth != 8 && context.bit_depth != 16)
return false;
context.channels = 3;
break;
case 3: // Each pixel is a palette index; a PLTE chunk must appear.
if (context.bit_depth != 1 && context.bit_depth != 2 && context.bit_depth != 4 && context.bit_depth != 8)
return false;
context.channels = 1;
break;
case 6: // Each pixel is an RGB sample, followed by an alpha sample.
if (context.bit_depth != 8 && context.bit_depth != 16)
return false;
context.channels = 4;
break;
default:
ASSERT_NOT_REACHED();
return false;
}
return true;
}