@@ -274,26 +274,21 @@ 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 = (uint32_t )((size_t )image -> alphaRowBytes * image -> height );
277+ const size_t alphaSize = (size_t )image -> alphaRowBytes * image -> height ;
278+ if (alphaSize > UINT32_MAX ) {
279+ goto cleanup ;
280+ }
281+ input_buffer -> n_filled_len = (uint32_t )alphaSize ;
278282
279283#if SVT_AV1_CHECK_VERSION (1 , 8 , 0 )
280284 // Simulate 4:2:0 UV planes. SVT-AV1 does not support 4:0:0 samples.
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 ) {
285+ const uint32_t uvWidth = (image -> width + y_shift ) >> y_shift ;
286+ const uint32_t uvRowBytes = uvWidth * bytesPerPixel ;
287+ const size_t uvSize = (size_t )uvRowBytes * uvHeight ;
288+ if (uvSize > UINT32_MAX / 2 ) {
289289 goto cleanup ;
290290 }
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 ) {
291+ if (uvSize * 2 > UINT32_MAX - input_buffer -> n_filled_len ) {
297292 goto cleanup ;
298293 }
299294 uvPlanes = avifAlloc (uvSize );
@@ -305,8 +300,8 @@ static avifResult svtCodecEncodeImage(avifCodec * codec,
305300 input_buffer -> n_filled_len += (uint32_t )uvSize ;
306301 input_picture_buffer -> cr = uvPlanes ;
307302 input_buffer -> n_filled_len += (uint32_t )uvSize ;
308- input_picture_buffer -> cb_stride = ( uint32_t ) uvWidth ;
309- input_picture_buffer -> cr_stride = ( uint32_t ) uvWidth ;
303+ input_picture_buffer -> cb_stride = uvWidth ;
304+ input_picture_buffer -> cr_stride = uvWidth ;
310305#else
311306 // This workaround was not needed before SVT-AV1 1.8.0.
312307 // See https://github.com/AOMediaCodec/libavif/issues/1992.
@@ -315,11 +310,23 @@ static avifResult svtCodecEncodeImage(avifCodec * codec,
315310 } else {
316311 input_picture_buffer -> y_stride = image -> yuvRowBytes [0 ] / bytesPerPixel ;
317312 input_picture_buffer -> luma = image -> yuvPlanes [0 ];
318- input_buffer -> n_filled_len = (uint32_t )((size_t )image -> yuvRowBytes [0 ] * image -> height );
313+ const size_t ySize = (size_t )image -> yuvRowBytes [0 ] * image -> height ;
314+ if (ySize > UINT32_MAX ) {
315+ goto cleanup ;
316+ }
317+ input_buffer -> n_filled_len = (uint32_t )ySize ;
319318 input_picture_buffer -> cb = image -> yuvPlanes [1 ];
320- input_buffer -> n_filled_len += (uint32_t )((size_t )image -> yuvRowBytes [1 ] * uvHeight );
319+ const size_t uSize = (size_t )image -> yuvRowBytes [1 ] * uvHeight ;
320+ if (uSize > UINT32_MAX - input_buffer -> n_filled_len ) {
321+ goto cleanup ;
322+ }
323+ input_buffer -> n_filled_len += (uint32_t )uSize ;
321324 input_picture_buffer -> cr = image -> yuvPlanes [2 ];
322- input_buffer -> n_filled_len += (uint32_t )((size_t )image -> yuvRowBytes [2 ] * uvHeight );
325+ const size_t vSize = (size_t )image -> yuvRowBytes [2 ] * uvHeight ;
326+ if (vSize > UINT32_MAX - input_buffer -> n_filled_len ) {
327+ goto cleanup ;
328+ }
329+ input_buffer -> n_filled_len += (uint32_t )vSize ;
323330 input_picture_buffer -> cb_stride = image -> yuvRowBytes [1 ] / bytesPerPixel ;
324331 input_picture_buffer -> cr_stride = image -> yuvRowBytes [2 ] / bytesPerPixel ;
325332 }
0 commit comments