Skip to content

Commit d264da2

Browse files
committed
gainmap: prevent integer overflow in plane allocation
1 parent 6064c35 commit d264da2

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/gainmap.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,11 +558,24 @@ avifResult avifRGBImageComputeGainMap(const avifRGBImage * baseRgbImage,
558558

559559
avifResult res = AVIF_RESULT_OK;
560560
// --- After this point, the function should exit with 'goto cleanup' to free allocated resources.
561+
// Overflow protection: 'width * height * sizeof(float)' uses signed int
562+
// multiplication which is undefined behavior on overflow in C. Compute the
563+
// allocation size in size_t with explicit overflow checks instead.
564+
if (baseRgbImage->width > SIZE_MAX / sizeof(float)) {
565+
res = AVIF_RESULT_INVALID_ARGUMENT;
566+
goto cleanup;
567+
}
568+
const size_t gainMapPlaneRowBytes = (size_t)baseRgbImage->width * sizeof(float);
569+
if (gainMapPlaneRowBytes != 0 && baseRgbImage->height > SIZE_MAX / gainMapPlaneRowBytes) {
570+
res = AVIF_RESULT_INVALID_ARGUMENT;
571+
goto cleanup;
572+
}
573+
const size_t gainMapPlaneSize = gainMapPlaneRowBytes * baseRgbImage->height;
561574

562575
const avifBool singleChannel = (gainMap->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV400);
563576
const int numGainMapChannels = singleChannel ? 1 : 3;
564577
for (int c = 0; c < numGainMapChannels; ++c) {
565-
gainMapF[c] = avifAlloc(width * height * sizeof(float));
578+
gainMapF[c] = avifAlloc(gainMapPlaneSize);
566579
if (gainMapF[c] == NULL) {
567580
res = AVIF_RESULT_OUT_OF_MEMORY;
568581
goto cleanup;

0 commit comments

Comments
 (0)