Skip to content

Commit 60f7cba

Browse files
committed
Fix potential integer overflow in rowBytes multiplications
Cast the first operand to (size_t) before multiplying two uint32_t values involving rowBytes, alphaRowBytes, or yuvRowBytes to prevent unsigned integer wrap-around on large images.
1 parent 979ad61 commit 60f7cba

5 files changed

Lines changed: 73 additions & 73 deletions

File tree

src/codec_aom.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ static avifResult aomCodecEncodeImage(avifCodec * codec,
12091209
if (aomImageAllocated) {
12101210
const uint32_t bytesPerRow = ((image->depth > 8) ? 2 : 1) * image->width;
12111211
for (uint32_t j = 0; j < image->height; ++j) {
1212-
const uint8_t * srcAlphaRow = &image->alphaPlane[j * image->alphaRowBytes];
1212+
const uint8_t * srcAlphaRow = &image->alphaPlane[(size_t)j * image->alphaRowBytes];
12131213
uint8_t * dstAlphaRow = &aomImage.planes[0][j * aomImage.stride[0]];
12141214
memcpy(dstAlphaRow, srcAlphaRow, bytesPerRow);
12151215
}
@@ -1233,7 +1233,7 @@ static avifResult aomCodecEncodeImage(avifCodec * codec,
12331233
uint32_t bytesPerRow = bytesPerPixel * planeWidth;
12341234

12351235
for (uint32_t j = 0; j < planeHeight; ++j) {
1236-
const uint8_t * srcRow = &image->yuvPlanes[yuvPlane][j * image->yuvRowBytes[yuvPlane]];
1236+
const uint8_t * srcRow = &image->yuvPlanes[yuvPlane][(size_t)j * image->yuvRowBytes[yuvPlane]];
12371237
uint8_t * dstRow = &aomImage.planes[yuvPlane][j * aomImage.stride[yuvPlane]];
12381238
memcpy(dstRow, srcRow, bytesPerRow);
12391239
}

src/codec_avm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ static avifResult avmCodecEncodeImage(avifCodec * codec,
903903
if (avmImageAllocated) {
904904
const uint32_t bytesPerRow = ((image->depth > 8) ? 2 : 1) * image->width;
905905
for (uint32_t j = 0; j < image->height; ++j) {
906-
const uint8_t * srcAlphaRow = &image->alphaPlane[j * image->alphaRowBytes];
906+
const uint8_t * srcAlphaRow = &image->alphaPlane[(size_t)j * image->alphaRowBytes];
907907
uint8_t * dstAlphaRow = &avmImage.planes[0][j * avmImage.stride[0]];
908908
memcpy(dstAlphaRow, srcAlphaRow, bytesPerRow);
909909
}
@@ -927,7 +927,7 @@ static avifResult avmCodecEncodeImage(avifCodec * codec,
927927
uint32_t bytesPerRow = bytesPerPixel * planeWidth;
928928

929929
for (uint32_t j = 0; j < planeHeight; ++j) {
930-
const uint8_t * srcRow = &image->yuvPlanes[yuvPlane][j * image->yuvRowBytes[yuvPlane]];
930+
const uint8_t * srcRow = &image->yuvPlanes[yuvPlane][(size_t)j * image->yuvRowBytes[yuvPlane]];
931931
uint8_t * dstRow = &avmImage.planes[yuvPlane][j * avmImage.stride[yuvPlane]];
932932
memcpy(dstRow, srcRow, bytesPerRow);
933933
}

src/codec_svt.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,13 @@ static avifResult svtCodecEncodeImage(avifCodec * codec,
274274
if (alpha) {
275275
input_picture_buffer->y_stride = image->alphaRowBytes / bytesPerPixel;
276276
input_picture_buffer->luma = image->alphaPlane;
277-
input_buffer->n_filled_len = image->alphaRowBytes * image->height;
277+
input_buffer->n_filled_len = (size_t)image->alphaRowBytes * image->height;
278278

279279
#if SVT_AV1_CHECK_VERSION(1, 8, 0)
280280
// Simulate 4:2:0 UV planes. SVT-AV1 does not support 4:0:0 samples.
281281
const uint32_t uvWidth = (image->width + y_shift) >> y_shift;
282282
const uint32_t uvRowBytes = uvWidth * bytesPerPixel;
283-
const uint32_t uvSize = uvRowBytes * uvHeight;
283+
const size_t uvSize = (size_t)uvRowBytes * uvHeight;
284284
uvPlanes = avifAlloc(uvSize);
285285
if (uvPlanes == NULL) {
286286
goto cleanup;
@@ -300,11 +300,11 @@ static avifResult svtCodecEncodeImage(avifCodec * codec,
300300
} else {
301301
input_picture_buffer->y_stride = image->yuvRowBytes[0] / bytesPerPixel;
302302
input_picture_buffer->luma = image->yuvPlanes[0];
303-
input_buffer->n_filled_len = image->yuvRowBytes[0] * image->height;
303+
input_buffer->n_filled_len = (size_t)image->yuvRowBytes[0] * image->height;
304304
input_picture_buffer->cb = image->yuvPlanes[1];
305-
input_buffer->n_filled_len += image->yuvRowBytes[1] * uvHeight;
305+
input_buffer->n_filled_len += (size_t)image->yuvRowBytes[1] * uvHeight;
306306
input_picture_buffer->cr = image->yuvPlanes[2];
307-
input_buffer->n_filled_len += image->yuvRowBytes[2] * uvHeight;
307+
input_buffer->n_filled_len += (size_t)image->yuvRowBytes[2] * uvHeight;
308308
input_picture_buffer->cb_stride = image->yuvRowBytes[1] / bytesPerPixel;
309309
input_picture_buffer->cr_stride = image->yuvRowBytes[2] / bytesPerPixel;
310310
}

src/read.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6599,8 +6599,8 @@ static avifResult avifImageLimitedToFullAlpha(avifImage * image)
65996599

66006600
if (image->depth > 8) {
66016601
for (uint32_t j = 0; j < image->height; ++j) {
6602-
const uint8_t * srcRow = &alphaPlane[j * alphaRowBytes];
6603-
uint8_t * dstRow = &image->alphaPlane[j * image->alphaRowBytes];
6602+
const uint8_t * srcRow = &alphaPlane[(size_t)j * alphaRowBytes];
6603+
uint8_t * dstRow = &image->alphaPlane[(size_t)j * image->alphaRowBytes];
66046604
for (uint32_t i = 0; i < image->width; ++i) {
66056605
int srcAlpha = *((const uint16_t *)&srcRow[i * 2]);
66066606
int dstAlpha = avifLimitedToFullY(image->depth, srcAlpha);
@@ -6609,8 +6609,8 @@ static avifResult avifImageLimitedToFullAlpha(avifImage * image)
66096609
}
66106610
} else {
66116611
for (uint32_t j = 0; j < image->height; ++j) {
6612-
const uint8_t * srcRow = &alphaPlane[j * alphaRowBytes];
6613-
uint8_t * dstRow = &image->alphaPlane[j * image->alphaRowBytes];
6612+
const uint8_t * srcRow = &alphaPlane[(size_t)j * alphaRowBytes];
6613+
uint8_t * dstRow = &image->alphaPlane[(size_t)j * image->alphaRowBytes];
66146614
for (uint32_t i = 0; i < image->width; ++i) {
66156615
int srcAlpha = srcRow[i];
66166616
int dstAlpha = avifLimitedToFullY(image->depth, srcAlpha);

0 commit comments

Comments
 (0)