Skip to content

Commit

Permalink
Add diagnostic messages if imageSizeLimit exceeded
Browse files Browse the repository at this point in the history
Add custom diagnostic messages if item size, track size, or grid
dimensions exceed imageSizeLimit.

Related to #263.
  • Loading branch information
wantehchang committed Aug 6, 2021
1 parent f7b7f57 commit bd1492e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -1631,10 +1631,14 @@ static avifBool avifParseImageGridBox(avifImageGrid * grid, const uint8_t * raw,
CHECK(avifROStreamReadU32(&s, &grid->outputWidth)); // unsigned int(FieldLength) output_width;
CHECK(avifROStreamReadU32(&s, &grid->outputHeight)); // unsigned int(FieldLength) output_height;
}
if ((grid->outputWidth == 0) || (grid->outputHeight == 0) || (grid->outputWidth > (imageSizeLimit / grid->outputHeight))) {
if ((grid->outputWidth == 0) || (grid->outputHeight == 0)) {
avifDiagnosticsPrintf(diag, "Grid box contains illegal dimensions: [%u x %u]", grid->outputWidth, grid->outputHeight);
return AVIF_FALSE;
}
if (grid->outputWidth > (imageSizeLimit / grid->outputHeight)) {
avifDiagnosticsPrintf(diag, "Grid box dimensions are too large: [%u x %u]", grid->outputWidth, grid->outputHeight);
return AVIF_FALSE;
}
return avifROStreamRemainingBytes(&s) == 0;
}

Expand Down Expand Up @@ -2365,10 +2369,14 @@ static avifBool avifParseTrackHeaderBox(avifTrack * track, const uint8_t * raw,
track->width = width >> 16;
track->height = height >> 16;

if ((track->width == 0) || (track->height == 0) || (track->width > (imageSizeLimit / track->height))) {
if ((track->width == 0) || (track->height == 0)) {
avifDiagnosticsPrintf(diag, "Track ID [%u] has an invalid size [%ux%u]", track->id, track->width, track->height);
return AVIF_FALSE;
}
if (track->width > (imageSizeLimit / track->height)) {
avifDiagnosticsPrintf(diag, "Track ID [%u] size is too large [%ux%u]", track->id, track->width, track->height);
return AVIF_FALSE;
}

// TODO: support scaling based on width/height track header info?

Expand Down Expand Up @@ -3079,10 +3087,14 @@ avifResult avifDecoderParse(avifDecoder * decoder)
item->width = ispeProp->u.ispe.width;
item->height = ispeProp->u.ispe.height;

if ((item->width == 0) || (item->height == 0) || (item->width > (decoder->imageSizeLimit / item->height))) {
if ((item->width == 0) || (item->height == 0)) {
avifDiagnosticsPrintf(data->diag, "Item ID [%u] has an invalid size [%ux%u]", item->id, item->width, item->height);
return AVIF_RESULT_BMFF_PARSE_FAILED;
}
if (item->width > (decoder->imageSizeLimit / item->height)) {
avifDiagnosticsPrintf(data->diag, "Item ID [%u] size is too large [%ux%u]", item->id, item->width, item->height);
return AVIF_RESULT_BMFF_PARSE_FAILED;
}
} else {
avifDiagnosticsPrintf(data->diag, "Item ID [%u] is missing a mandatory ispe property", item->id);
return AVIF_RESULT_BMFF_PARSE_FAILED;
Expand Down
6 changes: 5 additions & 1 deletion src/scale.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ avifBool avifImageScale(avifImage * image, uint32_t dstWidth, uint32_t dstHeight
return AVIF_TRUE;
}

if ((dstWidth == 0) || (dstHeight == 0) || (dstWidth > (imageSizeLimit / dstHeight))) {
if ((dstWidth == 0) || (dstHeight == 0)) {
avifDiagnosticsPrintf(diag, "avifImageScale requested invalid dst dimensions [%ux%u]", dstWidth, dstHeight);
return AVIF_FALSE;
}
if (dstWidth > (imageSizeLimit / dstHeight)) {
avifDiagnosticsPrintf(diag, "avifImageScale requested dst dimensions that are too large [%ux%u]", dstWidth, dstHeight);
return AVIF_FALSE;
}

uint8_t * srcYUVPlanes[AVIF_PLANE_COUNT_YUV];
uint32_t srcYUVRowBytes[AVIF_PLANE_COUNT_YUV];
Expand Down

0 comments on commit bd1492e

Please sign in to comment.