Skip to content

Commit 5bf8fa2

Browse files
committed
prevent potential size_t overflow in UV plane size calculations
1 parent 60f7cba commit 5bf8fa2

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

src/codec_svt.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,24 @@ static avifResult svtCodecEncodeImage(avifCodec * codec,
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.
281-
const uint32_t uvWidth = (image->width + y_shift) >> y_shift;
282-
const uint32_t uvRowBytes = uvWidth * bytesPerPixel;
283-
const size_t uvSize = (size_t)uvRowBytes * uvHeight;
281+
const size_t uvWidth = ((size_t)image->width + y_shift) >> y_shift;
282+
283+
// Use size_t to avoid 32-bit overflow
284+
const size_t uvRowBytes = (size_t)uvWidth * (size_t)bytesPerPixel;
285+
286+
// Verify multiplication overflow
287+
if (uvWidth != 0 &&
288+
uvRowBytes / (size_t)uvWidth != (size_t)bytesPerPixel) {
289+
goto cleanup;
290+
}
291+
292+
const size_t uvSize = uvRowBytes * (size_t)uvHeight;
293+
294+
// Verify second multiplication overflow
295+
if (uvHeight != 0 &&
296+
uvSize / (size_t)uvHeight != uvRowBytes) {
297+
goto cleanup;
298+
}
284299
uvPlanes = avifAlloc(uvSize);
285300
if (uvPlanes == NULL) {
286301
goto cleanup;

src/reformat.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1854,7 +1854,8 @@ void avifGetRGBAPixel(const avifRGBImage * src, uint32_t x, uint32_t y, const av
18541854
assert(!src->isFloat || src->depth == 16);
18551855
assert(src->format != AVIF_RGB_FORMAT_RGB_565 || src->depth == 8);
18561856

1857-
const uint8_t * const srcPixel = &src->pixels[(size_t)y * src->rowBytes + x * info->pixelBytes];
1857+
const size_t offset = (size_t)y * (size_t)src->rowBytes + (size_t)x * (size_t)info->pixelBytes;
1858+
const uint8_t * const srcPixel = &src->pixels[offset];
18581859
if (info->channelBytes > 1) {
18591860
uint16_t r = *((const uint16_t *)(&srcPixel[info->offsetBytesR]));
18601861
uint16_t g = *((const uint16_t *)(&srcPixel[info->offsetBytesG]));

0 commit comments

Comments
 (0)