History

avcodec/pngdec: avoid hard failure on illegal sBIT chunks

If a malformed chunk like sBIT appears but otherwise the stream is still parseable, we should print a warning and skip it rather than failing with an error.

Leo Izen committed 1 year ago

Commit 3fca5871 parent 43be8d01 file · +10 −8Browse files

Unified · Split

libavcodec/pngdec.c+10−8
@@ -1073,6 +1073,7 @@ static int decode_sbit_chunk(AVCodecContext *avctx, PNGDecContext *s, · 1072 unmodified lines
10731073 {
10741074 int bits = 0;
10751075 int channels;
1076+ int remainder = bytestream2_get_bytes_left(gb);
10761077
10771078 if (!(s->hdr_state & PNG_IHDR)) {
10781079 av_log(avctx, AV_LOG_ERROR, "sBIT before IHDR\n");
@@ -1080,16 +1081,17 @@ static int decode_sbit_chunk(AVCodecContext *avctx, PNGDecContext *s, · 1 unmodified line
10801081 }
10811082
10821083 if (s->pic_state & PNG_IDAT) {
1083− av_log(avctx, AV_LOG_ERROR, "sBIT after IDAT\n");
1084− return AVERROR_INVALIDDATA;
1084+ av_log(avctx, AV_LOG_WARNING, "Ignoring illegal sBIT chunk after IDAT\n");
1085+ return 0;
10851086 }
10861087
10871088 channels = s->color_type & PNG_COLOR_MASK_PALETTE ? 3 : ff_png_get_nb_channels(s->color_type);
10881089
1089− if (bytestream2_get_bytes_left(gb) != channels) {
1090− av_log(avctx, AV_LOG_ERROR, "Invalid sBIT size: %d, expected: %d\n",
1091− bytestream2_get_bytes_left(gb), channels);
1092− return AVERROR_INVALIDDATA;
1090+ if (remainder != channels) {
1091+ av_log(avctx, AV_LOG_WARNING, "Invalid sBIT size: %d, expected: %d\n", remainder, channels);
1092+ /* not enough space left in chunk to read info */
1093+ if (remainder < channels)
1094+ return 0;
10931095 }
10941096
10951097 for (int i = 0; i < channels; i++) {
@@ -1098,8 +1100,8 @@ static int decode_sbit_chunk(AVCodecContext *avctx, PNGDecContext *s, · 2 unmodified lines
10981100 }
10991101
11001102 if (bits <= 0 || bits > (s->color_type & PNG_COLOR_MASK_PALETTE ? 8 : s->bit_depth)) {
1101− av_log(avctx, AV_LOG_ERROR, "Invalid significant bits: %d\n", bits);
1102− return AVERROR_INVALIDDATA;
1103+ av_log(avctx, AV_LOG_WARNING, "Invalid significant bits: %d\n", bits);
1104+ return 0;
11031105 }
11041106 s->significant_bits = bits;
11051107

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t