From c98edcc8dcdba15beaf866a4a5ea8cccb2865cbe Mon Sep 17 00:00:00 2001 From: Wan-Teh Chang Date: Fri, 29 May 2026 11:29:01 -0700 Subject: [PATCH] Don't coalesce rows if width*height would overflow Audit all occurrences of "width *= height;" in the libyuv source code. Make sure height > 0 and (ptrdiff_t)width * height <= INT_MAX before executing width *= height. Bug: chromium:517339758 Change-Id: I143a41c66492a6e4c48b6aa2a1c4a2ae974ceeb1 Reviewed-on: https://chromium-review.googlesource.com/c/libyuv/libyuv/+/7883816 Commit-Queue: Wan-Teh Chang Reviewed-by: Frank Barchard --- README.chromium | 2 +- include/libyuv/version.h | 2 +- source/compare.cc | 12 ++- source/convert.cc | 11 ++- source/convert_argb.cc | 65 +++++++++----- source/convert_from.cc | 8 +- source/convert_from_argb.cc | 35 +++++--- source/planar_functions.cc | 172 ++++++++++++++++++++++++------------ 8 files changed, 207 insertions(+), 100 deletions(-) diff --git a/README.chromium b/README.chromium index c1f416458..ae8e037eb 100644 --- a/README.chromium +++ b/README.chromium @@ -1,6 +1,6 @@ Name: libyuv URL: https://chromium.googlesource.com/libyuv/libyuv/ -Version: 1942 +Version: 1943 Revision: DEPS License: BSD-3-Clause License File: LICENSE diff --git a/include/libyuv/version.h b/include/libyuv/version.h index ab2420cd5..d739e7ea6 100644 --- a/include/libyuv/version.h +++ b/include/libyuv/version.h @@ -11,6 +11,6 @@ #ifndef INCLUDE_LIBYUV_VERSION_H_ #define INCLUDE_LIBYUV_VERSION_H_ -#define LIBYUV_VERSION 1942 +#define LIBYUV_VERSION 1943 #endif // INCLUDE_LIBYUV_VERSION_H_ diff --git a/source/compare.cc b/source/compare.cc index e85cc6d07..10023301c 100644 --- a/source/compare.cc +++ b/source/compare.cc @@ -11,6 +11,7 @@ #include "libyuv/compare.h" #include +#include #include #ifdef _OPENMP #include @@ -106,8 +107,11 @@ uint32_t ARGBDetect(const uint8_t* argb, uint32_t fourcc = 0; int h; + if (!argb || width <= 0 || height <= 0) { + return fourcc; + } // Coalesce rows. - if (stride_argb == width * 4) { + if (stride_argb == width * 4 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; stride_argb = 0; @@ -245,8 +249,12 @@ uint64_t ComputeSumSquareErrorPlane(const uint8_t* src_a, int height) { uint64_t sse = 0; int h; + if (!src_a || !src_b || width <= 0 || height <= 0) { + return sse; + } // Coalesce rows. - if (stride_a == width && stride_b == width) { + if (stride_a == width && stride_b == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; stride_a = stride_b = 0; diff --git a/source/convert.cc b/source/convert.cc index 4a713658b..4f5cca500 100644 --- a/source/convert.cc +++ b/source/convert.cc @@ -10,6 +10,8 @@ #include "libyuv/convert.h" +#include + #include "libyuv/basic_types.h" #include "libyuv/cpu_id.h" #include "libyuv/planar_functions.h" @@ -1435,7 +1437,8 @@ int NV12ToI420(const uint8_t* src_y, src_stride_uv = -src_stride_uv; } // Coalesce rows. - if (src_stride_y == width && dst_stride_y == width) { + if (src_stride_y == width && dst_stride_y == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_y = dst_stride_y = 0; @@ -4476,7 +4479,8 @@ int RGB24ToJ400(const uint8_t* src_rgb24, src_stride_rgb24 = -src_stride_rgb24; } // Coalesce rows. - if (src_stride_rgb24 == width * 3 && dst_stride_yj == width) { + if (src_stride_rgb24 == width * 3 && dst_stride_yj == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_rgb24 = dst_stride_yj = 0; @@ -4641,7 +4645,8 @@ int RAWToJ400(const uint8_t* src_raw, src_stride_raw = -src_stride_raw; } // Coalesce rows. - if (src_stride_raw == width * 3 && dst_stride_yj == width) { + if (src_stride_raw == width * 3 && dst_stride_yj == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_raw = dst_stride_yj = 0; diff --git a/source/convert_argb.cc b/source/convert_argb.cc index 82f10966a..1d73d1e43 100644 --- a/source/convert_argb.cc +++ b/source/convert_argb.cc @@ -11,6 +11,7 @@ #include "libyuv/convert_argb.h" #include +#include #include "libyuv/convert_from_argb.h" #include "libyuv/cpu_id.h" @@ -327,7 +328,8 @@ int I422ToARGBMatrix(const uint8_t* src_y, } // Coalesce rows. if (src_stride_y == width && src_stride_u * 2 == width && - src_stride_v * 2 == width && dst_stride_argb == width * 4) { + src_stride_v * 2 == width && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_y = src_stride_u = src_stride_v = dst_stride_argb = 0; @@ -581,7 +583,7 @@ int I444ToARGBMatrix(const uint8_t* src_y, } // Coalesce rows. if (src_stride_y == width && src_stride_u == width && src_stride_v == width && - dst_stride_argb == width * 4) { + dst_stride_argb == width * 4 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_y = src_stride_u = src_stride_v = dst_stride_argb = 0; @@ -818,7 +820,7 @@ int I444ToRGB24Matrix(const uint8_t* src_y, } // Coalesce rows. if (src_stride_y == width && src_stride_u == width && src_stride_v == width && - dst_stride_rgb24 == width * 3) { + dst_stride_rgb24 == width * 3 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_y = src_stride_u = src_stride_v = dst_stride_rgb24 = 0; @@ -3181,7 +3183,8 @@ int I400ToARGBMatrix(const uint8_t* src_y, dst_stride_argb = -dst_stride_argb; } // Coalesce rows. - if (src_stride_y == width && dst_stride_argb == width * 4) { + if (src_stride_y == width && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_y = dst_stride_argb = 0; @@ -3275,7 +3278,8 @@ int J400ToARGB(const uint8_t* src_y, src_stride_y = -src_stride_y; } // Coalesce rows. - if (src_stride_y == width && dst_stride_argb == width * 4) { + if (src_stride_y == width && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_y = dst_stride_argb = 0; @@ -3449,7 +3453,8 @@ int ARGBToBGRA(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_bgra == width * 4) { + if (src_stride_argb == width * 4 && dst_stride_bgra == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_bgra = 0; @@ -3490,7 +3495,8 @@ int ARGBToABGR(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_abgr == width * 4) { + if (src_stride_argb == width * 4 && dst_stride_abgr == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_abgr = 0; @@ -3543,7 +3549,8 @@ int RGBAToARGB(const uint8_t* src_rgba, src_stride_rgba = -src_stride_rgba; } // Coalesce rows. - if (src_stride_rgba == width * 4 && dst_stride_argb == width * 4) { + if (src_stride_rgba == width * 4 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_rgba = dst_stride_argb = 0; @@ -3584,7 +3591,8 @@ int AR64ToAB64(const uint16_t* src_ar64, src_stride_ar64 = -src_stride_ar64; } // Coalesce rows. - if (src_stride_ar64 == width * 4 && dst_stride_ab64 == width * 4) { + if (src_stride_ar64 == width * 4 && dst_stride_ab64 == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_ar64 = dst_stride_ab64 = 0; @@ -3626,7 +3634,8 @@ int RGB24ToARGB(const uint8_t* src_rgb24, src_stride_rgb24 = -src_stride_rgb24; } // Coalesce rows. - if (src_stride_rgb24 == width * 3 && dst_stride_argb == width * 4) { + if (src_stride_rgb24 == width * 3 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_rgb24 = dst_stride_argb = 0; @@ -3718,7 +3727,8 @@ int RAWToARGB(const uint8_t* src_raw, src_stride_raw = -src_stride_raw; } // Coalesce rows. - if (src_stride_raw == width * 3 && dst_stride_argb == width * 4) { + if (src_stride_raw == width * 3 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_raw = dst_stride_argb = 0; @@ -3811,7 +3821,8 @@ int RAWToRGBA(const uint8_t* src_raw, src_stride_raw = -src_stride_raw; } // Coalesce rows. - if (src_stride_raw == width * 3 && dst_stride_rgba == width * 4) { + if (src_stride_raw == width * 3 && dst_stride_rgba == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_raw = dst_stride_rgba = 0; @@ -3872,7 +3883,8 @@ int RGB565ToARGB(const uint8_t* src_rgb565, src_stride_rgb565 = -src_stride_rgb565; } // Coalesce rows. - if (src_stride_rgb565 == width * 2 && dst_stride_argb == width * 4) { + if (src_stride_rgb565 == width * 2 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_rgb565 = dst_stride_argb = 0; @@ -3947,7 +3959,8 @@ int ARGB1555ToARGB(const uint8_t* src_argb1555, src_stride_argb1555 = -src_stride_argb1555; } // Coalesce rows. - if (src_stride_argb1555 == width * 2 && dst_stride_argb == width * 4) { + if (src_stride_argb1555 == width * 2 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb1555 = dst_stride_argb = 0; @@ -4027,7 +4040,8 @@ int ARGB4444ToARGB(const uint8_t* src_argb4444, src_stride_argb4444 = -src_stride_argb4444; } // Coalesce rows. - if (src_stride_argb4444 == width * 2 && dst_stride_argb == width * 4) { + if (src_stride_argb4444 == width * 2 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb4444 = dst_stride_argb = 0; @@ -4100,7 +4114,8 @@ int AR30ToARGB(const uint8_t* src_ar30, src_stride_ar30 = -src_stride_ar30; } // Coalesce rows. - if (src_stride_ar30 == width * 4 && dst_stride_argb == width * 4) { + if (src_stride_ar30 == width * 4 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_ar30 = dst_stride_argb = 0; @@ -4132,7 +4147,8 @@ int AR30ToABGR(const uint8_t* src_ar30, src_stride_ar30 = -src_stride_ar30; } // Coalesce rows. - if (src_stride_ar30 == width * 4 && dst_stride_abgr == width * 4) { + if (src_stride_ar30 == width * 4 && dst_stride_abgr == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_ar30 = dst_stride_abgr = 0; @@ -4164,7 +4180,8 @@ int AR30ToAB30(const uint8_t* src_ar30, src_stride_ar30 = -src_stride_ar30; } // Coalesce rows. - if (src_stride_ar30 == width * 4 && dst_stride_ab30 == width * 4) { + if (src_stride_ar30 == width * 4 && dst_stride_ab30 == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_ar30 = dst_stride_ab30 = 0; @@ -4198,7 +4215,8 @@ int AR64ToARGB(const uint16_t* src_ar64, src_stride_ar64 = -src_stride_ar64; } // Coalesce rows. - if (src_stride_ar64 == width * 4 && dst_stride_argb == width * 4) { + if (src_stride_ar64 == width * 4 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_ar64 = dst_stride_argb = 0; @@ -4262,7 +4280,8 @@ int AB64ToARGB(const uint16_t* src_ab64, src_stride_ab64 = -src_stride_ab64; } // Coalesce rows. - if (src_stride_ab64 == width * 4 && dst_stride_argb == width * 4) { + if (src_stride_ab64 == width * 4 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_ab64 = dst_stride_argb = 0; @@ -4837,7 +4856,8 @@ int YUY2ToARGBMatrix(const uint8_t* src_yuy2, src_stride_yuy2 = -src_stride_yuy2; } // Coalesce rows. - if (src_stride_yuy2 == width * 2 && dst_stride_argb == width * 4) { + if (src_stride_yuy2 == width * 2 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_yuy2 = dst_stride_argb = 0; @@ -4927,7 +4947,8 @@ int UYVYToARGBMatrix(const uint8_t* src_uyvy, src_stride_uyvy = -src_stride_uyvy; } // Coalesce rows. - if (src_stride_uyvy == width * 2 && dst_stride_argb == width * 4) { + if (src_stride_uyvy == width * 2 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_uyvy = dst_stride_argb = 0; diff --git a/source/convert_from.cc b/source/convert_from.cc index 4a3e1c436..aa5de62f5 100644 --- a/source/convert_from.cc +++ b/source/convert_from.cc @@ -10,6 +10,8 @@ #include "libyuv/convert_from.h" +#include + #include "libyuv/basic_types.h" #include "libyuv/convert.h" // For I420Copy #include "libyuv/cpu_id.h" @@ -357,7 +359,8 @@ int I422ToYUY2(const uint8_t* src_y, } // Coalesce rows. if (src_stride_y == width && src_stride_u * 2 == width && - src_stride_v * 2 == width && dst_stride_yuy2 == width * 2) { + src_stride_v * 2 == width && dst_stride_yuy2 == width * 2 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_y = src_stride_u = src_stride_v = dst_stride_yuy2 = 0; @@ -503,7 +506,8 @@ int I422ToUYVY(const uint8_t* src_y, } // Coalesce rows. if (src_stride_y == width && src_stride_u * 2 == width && - src_stride_v * 2 == width && dst_stride_uyvy == width * 2) { + src_stride_v * 2 == width && dst_stride_uyvy == width * 2 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_y = src_stride_u = src_stride_v = dst_stride_uyvy = 0; diff --git a/source/convert_from_argb.cc b/source/convert_from_argb.cc index 965ce0b9f..612821996 100644 --- a/source/convert_from_argb.cc +++ b/source/convert_from_argb.cc @@ -10,6 +10,8 @@ #include "libyuv/convert_from_argb.h" +#include + #include "libyuv/basic_types.h" #include "libyuv/cpu_id.h" #include "libyuv/planar_functions.h" @@ -1180,7 +1182,8 @@ int ARGBToRGBA(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_rgba == width * 4) { + if (src_stride_argb == width * 4 && dst_stride_rgba == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_rgba = 0; @@ -1221,7 +1224,8 @@ int ARGBToRGB24(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_rgb24 == width * 3) { + if (src_stride_argb == width * 4 && dst_stride_rgb24 == width * 3 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_rgb24 = 0; @@ -1313,7 +1317,8 @@ int ARGBToRAW(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_raw == width * 3) { + if (src_stride_argb == width * 4 && dst_stride_raw == width * 3 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_raw = 0; @@ -1483,7 +1488,8 @@ int ARGBToRGB565(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_rgb565 == width * 2) { + if (src_stride_argb == width * 4 && dst_stride_rgb565 == width * 2 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_rgb565 = 0; @@ -1563,7 +1569,8 @@ int ARGBToARGB1555(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_argb1555 == width * 2) { + if (src_stride_argb == width * 4 && dst_stride_argb1555 == width * 2 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_argb1555 = 0; @@ -1637,7 +1644,8 @@ int ARGBToARGB4444(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_argb4444 == width * 2) { + if (src_stride_argb == width * 4 && dst_stride_argb4444 == width * 2 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_argb4444 = 0; @@ -1711,7 +1719,8 @@ int ABGRToAR30(const uint8_t* src_abgr, src_stride_abgr = -src_stride_abgr; } // Coalesce rows. - if (src_stride_abgr == width * 4 && dst_stride_ar30 == width * 4) { + if (src_stride_abgr == width * 4 && dst_stride_ar30 == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_abgr = dst_stride_ar30 = 0; @@ -1768,7 +1777,8 @@ int ARGBToAR30(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_ar30 == width * 4) { + if (src_stride_argb == width * 4 && dst_stride_ar30 == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_ar30 = 0; @@ -1888,7 +1898,8 @@ int RGBAToJ400(const uint8_t* src_rgba, src_stride_rgba = -src_stride_rgba; } // Coalesce rows. - if (src_stride_rgba == width * 4 && dst_stride_yj == width) { + if (src_stride_rgba == width * 4 && dst_stride_yj == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_rgba = dst_stride_yj = 0; @@ -2030,7 +2041,8 @@ int ARGBToAR64(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_ar64 == width * 4) { + if (src_stride_argb == width * 4 && dst_stride_ar64 == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_ar64 = 0; @@ -2094,7 +2106,8 @@ int ARGBToAB64(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_ab64 == width * 4) { + if (src_stride_argb == width * 4 && dst_stride_ab64 == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_ab64 = 0; diff --git a/source/planar_functions.cc b/source/planar_functions.cc index 016ea24e0..02757f3b0 100644 --- a/source/planar_functions.cc +++ b/source/planar_functions.cc @@ -12,6 +12,7 @@ #include "libyuv/planar_functions.h" #include +#include #include // for memset() #include "libyuv/cpu_id.h" @@ -43,7 +44,8 @@ void CopyPlane(const uint8_t* src_y, dst_stride_y = -dst_stride_y; } // Coalesce rows. - if (src_stride_y == width && dst_stride_y == width) { + if (src_stride_y == width && dst_stride_y == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_y = dst_stride_y = 0; @@ -131,7 +133,8 @@ void Convert16To8Plane(const uint16_t* src_y, dst_stride_y = -dst_stride_y; } // Coalesce rows. - if (src_stride_y == width && dst_stride_y == width) { + if (src_stride_y == width && dst_stride_y == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_y = dst_stride_y = 0; @@ -205,7 +208,8 @@ void Convert8To16Plane(const uint8_t* src_y, dst_stride_y = -dst_stride_y; } // Coalesce rows. - if (src_stride_y == width && dst_stride_y == width) { + if (src_stride_y == width && dst_stride_y == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_y = dst_stride_y = 0; @@ -272,7 +276,8 @@ void Convert8To8Plane(const uint8_t* src_y, dst_stride_y = -dst_stride_y; } // Coalesce rows. - if (src_stride_y == width && dst_stride_y == width) { + if (src_stride_y == width && dst_stride_y == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_y = dst_stride_y = 0; @@ -603,7 +608,7 @@ void SplitUVPlane(const uint8_t* src_uv, } // Coalesce rows. if (src_stride_uv == width * 2 && dst_stride_u == width && - dst_stride_v == width) { + dst_stride_v == width && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_uv = dst_stride_u = dst_stride_v = 0; @@ -678,7 +683,7 @@ void MergeUVPlane(const uint8_t* src_u, } // Coalesce rows. if (src_stride_u == width && src_stride_v == width && - dst_stride_uv == width * 2) { + dst_stride_uv == width * 2 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_u = src_stride_v = dst_stride_uv = 0; @@ -772,7 +777,7 @@ void SplitUVPlane_16(const uint16_t* src_uv, } // Coalesce rows. if (src_stride_uv == width * 2 && dst_stride_u == width && - dst_stride_v == width) { + dst_stride_v == width && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_uv = dst_stride_u = dst_stride_v = 0; @@ -830,7 +835,7 @@ void MergeUVPlane_16(const uint16_t* src_u, } // Coalesce rows. if (src_stride_u == width && src_stride_v == width && - dst_stride_uv == width * 2) { + dst_stride_uv == width * 2 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_u = src_stride_v = dst_stride_uv = 0; @@ -889,7 +894,8 @@ void ConvertToMSBPlane_16(const uint16_t* src_y, dst_stride_y = -dst_stride_y; } // Coalesce rows. - if (src_stride_y == width && dst_stride_y == width) { + if (src_stride_y == width && dst_stride_y == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_y = dst_stride_y = 0; @@ -947,7 +953,8 @@ void ConvertToLSBPlane_16(const uint16_t* src_y, dst_stride_y = -dst_stride_y; } // Coalesce rows. - if (src_stride_y == width && dst_stride_y == width) { + if (src_stride_y == width && dst_stride_y == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_y = dst_stride_y = 0; @@ -1003,7 +1010,8 @@ void SwapUVPlane(const uint8_t* src_uv, src_stride_uv = -src_stride_uv; } // Coalesce rows. - if (src_stride_uv == width * 2 && dst_stride_vu == width * 2) { + if (src_stride_uv == width * 2 && dst_stride_vu == width * 2 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_uv = dst_stride_vu = 0; @@ -1364,7 +1372,8 @@ void SplitRGBPlane(const uint8_t* src_rgb, } // Coalesce rows. if (src_stride_rgb == width * 3 && dst_stride_r == width && - dst_stride_g == width && dst_stride_b == width) { + dst_stride_g == width && dst_stride_b == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_rgb = dst_stride_r = dst_stride_g = dst_stride_b = 0; @@ -1444,7 +1453,7 @@ void MergeRGBPlane(const uint8_t* src_r, } // Coalesce rows. if (src_stride_r == width && src_stride_g == width && src_stride_b == width && - dst_stride_rgb == width * 3) { + dst_stride_rgb == width * 3 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_r = src_stride_g = src_stride_b = dst_stride_rgb = 0; @@ -1499,13 +1508,14 @@ static void SplitARGBPlaneAlpha(const uint8_t* src_argb, uint8_t* dst_b, uint8_t* dst_a, int width) = SplitARGBRow_C; - assert(height > 0); + assert(height >= 0); if (width <= 0 || height == 0) { return; } if (src_stride_argb == width * 4 && dst_stride_r == width && - dst_stride_g == width && dst_stride_b == width && dst_stride_a == width) { + dst_stride_g == width && dst_stride_b == width && dst_stride_a == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_r = dst_stride_g = dst_stride_b = @@ -1574,13 +1584,14 @@ static void SplitARGBPlaneOpaque(const uint8_t* src_argb, int y; void (*SplitXRGBRow)(const uint8_t* src_rgb, uint8_t* dst_r, uint8_t* dst_g, uint8_t* dst_b, int width) = SplitXRGBRow_C; - assert(height > 0); + assert(height >= 0); if (width <= 0 || height == 0) { return; } if (src_stride_argb == width * 4 && dst_stride_r == width && - dst_stride_g == width && dst_stride_b == width) { + dst_stride_g == width && dst_stride_b == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_r = dst_stride_g = dst_stride_b = 0; @@ -1687,13 +1698,14 @@ static void MergeARGBPlaneAlpha(const uint8_t* src_r, const uint8_t* src_b, const uint8_t* src_a, uint8_t* dst_argb, int width) = MergeARGBRow_C; - assert(height > 0); + assert(height >= 0); if (width <= 0 || height == 0) { return; } if (src_stride_r == width && src_stride_g == width && src_stride_b == width && - src_stride_a == width && dst_stride_argb == width * 4) { + src_stride_a == width && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_r = src_stride_g = src_stride_b = src_stride_a = @@ -1755,13 +1767,13 @@ static void MergeARGBPlaneOpaque(const uint8_t* src_r, const uint8_t* src_b, uint8_t* dst_argb, int width) = MergeXRGBRow_C; - assert(height > 0); + assert(height >= 0); if (width <= 0 || height == 0) { return; } if (src_stride_r == width && src_stride_g == width && src_stride_b == width && - dst_stride_argb == width * 4) { + dst_stride_argb == width * 4 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_r = src_stride_g = src_stride_b = dst_stride_argb = 0; @@ -1854,6 +1866,9 @@ void MergeXR30Plane(const uint16_t* src_r, const uint16_t* src_b, uint8_t* dst_ar30, int depth, int width) = MergeXR30Row_C; + if (width <= 0 || height == 0) { + return; + } // Negative height means invert the image. if (height < 0) { height = -height; @@ -1862,7 +1877,7 @@ void MergeXR30Plane(const uint16_t* src_r, } // Coalesce rows. if (src_stride_r == width && src_stride_g == width && src_stride_b == width && - dst_stride_ar30 == width * 4) { + dst_stride_ar30 == width * 4 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_r = src_stride_g = src_stride_b = dst_stride_ar30 = 0; @@ -1920,8 +1935,14 @@ static void MergeAR64PlaneAlpha(const uint16_t* src_r, uint16_t* dst_argb, int depth, int width) = MergeAR64Row_C; + assert(height >= 0); + + if (width <= 0 || height == 0) { + return; + } if (src_stride_r == width && src_stride_g == width && src_stride_b == width && - src_stride_a == width && dst_stride_ar64 == width * 4) { + src_stride_a == width && dst_stride_ar64 == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_r = src_stride_g = src_stride_b = src_stride_a = @@ -1971,9 +1992,14 @@ static void MergeAR64PlaneOpaque(const uint16_t* src_r, const uint16_t* src_b, uint16_t* dst_argb, int depth, int width) = MergeXR64Row_C; + assert(height >= 0); + + if (width <= 0 || height == 0) { + return; + } // Coalesce rows. if (src_stride_r == width && src_stride_g == width && src_stride_b == width && - dst_stride_ar64 == width * 4) { + dst_stride_ar64 == width * 4 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_r = src_stride_g = src_stride_b = dst_stride_ar64 = 0; @@ -2056,8 +2082,14 @@ static void MergeARGB16To8PlaneAlpha(const uint16_t* src_r, uint8_t* dst_argb, int depth, int width) = MergeARGB16To8Row_C; + assert(height >= 0); + + if (width <= 0 || height == 0) { + return; + } if (src_stride_r == width && src_stride_g == width && src_stride_b == width && - src_stride_a == width && dst_stride_argb == width * 4) { + src_stride_a == width && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_r = src_stride_g = src_stride_b = src_stride_a = @@ -2107,9 +2139,14 @@ static void MergeARGB16To8PlaneOpaque(const uint16_t* src_r, const uint16_t* src_b, uint8_t* dst_argb, int depth, int width) = MergeXRGB16To8Row_C; + assert(height >= 0); + + if (width <= 0 || height == 0) { + return; + } // Coalesce rows. if (src_stride_r == width && src_stride_g == width && src_stride_b == width && - dst_stride_argb == width * 4) { + dst_stride_argb == width * 4 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_r = src_stride_g = src_stride_b = dst_stride_argb = 0; @@ -2201,7 +2238,7 @@ int YUY2ToI422(const uint8_t* src_yuy2, // Coalesce rows. if (src_stride_yuy2 == width * 2 && dst_stride_y == width && dst_stride_u * 2 == width && dst_stride_v * 2 == width && - width * height <= 32768) { + (ptrdiff_t)width * height <= 32768) { width *= height; height = 1; src_stride_yuy2 = dst_stride_y = dst_stride_u = dst_stride_v = 0; @@ -2297,7 +2334,7 @@ int UYVYToI422(const uint8_t* src_uyvy, // Coalesce rows. if (src_stride_uyvy == width * 2 && dst_stride_y == width && dst_stride_u * 2 == width && dst_stride_v * 2 == width && - width * height <= 32768) { + (ptrdiff_t)width * height <= 32768) { width *= height; height = 1; src_stride_uyvy = dst_stride_y = dst_stride_u = dst_stride_v = 0; @@ -2385,7 +2422,8 @@ int YUY2ToY(const uint8_t* src_yuy2, src_stride_yuy2 = -src_stride_yuy2; } // Coalesce rows. - if (src_stride_yuy2 == width * 2 && dst_stride_y == width) { + if (src_stride_yuy2 == width * 2 && dst_stride_y == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_yuy2 = dst_stride_y = 0; @@ -2444,7 +2482,8 @@ int UYVYToY(const uint8_t* src_uyvy, src_stride_uyvy = -src_stride_uyvy; } // Coalesce rows. - if (src_stride_uyvy == width * 2 && dst_stride_y == width) { + if (src_stride_uyvy == width * 2 && dst_stride_y == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_uyvy = dst_stride_y = 0; @@ -2864,7 +2903,7 @@ int ARGBBlend(const uint8_t* src_argb0, } // Coalesce rows. if (src_stride_argb0 == width * 4 && src_stride_argb1 == width * 4 && - dst_stride_argb == width * 4) { + dst_stride_argb == width * 4 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; @@ -2926,7 +2965,8 @@ int BlendPlane(const uint8_t* src_y0, // Coalesce rows for Y plane. if (src_stride_y0 == width && src_stride_y1 == width && - alpha_stride == width && dst_stride_y == width) { + alpha_stride == width && dst_stride_y == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_y0 = src_stride_y1 = alpha_stride = dst_stride_y = 0; @@ -3126,7 +3166,7 @@ int ARGBMultiply(const uint8_t* src_argb0, } // Coalesce rows. if (src_stride_argb0 == width * 4 && src_stride_argb1 == width * 4 && - dst_stride_argb == width * 4) { + dst_stride_argb == width * 4 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; @@ -3211,7 +3251,7 @@ int ARGBAdd(const uint8_t* src_argb0, } // Coalesce rows. if (src_stride_argb0 == width * 4 && src_stride_argb1 == width * 4 && - dst_stride_argb == width * 4) { + dst_stride_argb == width * 4 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; @@ -3296,7 +3336,7 @@ int ARGBSubtract(const uint8_t* src_argb0, } // Coalesce rows. if (src_stride_argb0 == width * 4 && src_stride_argb1 == width * 4 && - dst_stride_argb == width * 4) { + dst_stride_argb == width * 4 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; @@ -3373,7 +3413,8 @@ int RAWToRGB24(const uint8_t* src_raw, src_stride_raw = -src_stride_raw; } // Coalesce rows. - if (src_stride_raw == width * 3 && dst_stride_rgb24 == width * 3) { + if (src_stride_raw == width * 3 && dst_stride_rgb24 == width * 3 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_raw = dst_stride_rgb24 = 0; @@ -3440,7 +3481,7 @@ void SetPlane(uint8_t* dst_y, dst_stride_y = -dst_stride_y; } // Coalesce rows. - if (dst_stride_y == width) { + if (dst_stride_y == width && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; dst_stride_y = 0; @@ -3537,7 +3578,7 @@ int ARGBRect(uint8_t* dst_argb, } dst_argb += dst_y * dst_stride_argb + dst_x * 4; // Coalesce rows. - if (dst_stride_argb == width * 4) { + if (dst_stride_argb == width * 4 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; dst_stride_argb = 0; @@ -3605,7 +3646,8 @@ int ARGBAttenuate(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { + if (src_stride_argb == width * 4 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_argb = 0; @@ -3684,7 +3726,8 @@ int ARGBUnattenuate(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { + if (src_stride_argb == width * 4 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_argb = 0; @@ -3735,7 +3778,8 @@ int ARGBGrayTo(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { + if (src_stride_argb == width * 4 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_argb = 0; @@ -3790,7 +3834,7 @@ int ARGBGray(uint8_t* dst_argb, return -1; } // Coalesce rows. - if (dst_stride_argb == width * 4) { + if (dst_stride_argb == width * 4 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; dst_stride_argb = 0; @@ -3843,7 +3887,7 @@ int ARGBSepia(uint8_t* dst_argb, return -1; } // Coalesce rows. - if (dst_stride_argb == width * 4) { + if (dst_stride_argb == width * 4 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; dst_stride_argb = 0; @@ -3904,7 +3948,8 @@ int ARGBColorMatrix(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { + if (src_stride_argb == width * 4 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_argb = 0; @@ -3993,7 +4038,7 @@ int ARGBColorTable(uint8_t* dst_argb, return -1; } // Coalesce rows. - if (dst_stride_argb == width * 4) { + if (dst_stride_argb == width * 4 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; dst_stride_argb = 0; @@ -4029,7 +4074,7 @@ int RGBColorTable(uint8_t* dst_argb, return -1; } // Coalesce rows. - if (dst_stride_argb == width * 4) { + if (dst_stride_argb == width * 4 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; dst_stride_argb = 0; @@ -4074,7 +4119,7 @@ int ARGBQuantize(uint8_t* dst_argb, return -1; } // Coalesce rows. - if (dst_stride_argb == width * 4) { + if (dst_stride_argb == width * 4 && (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; dst_stride_argb = 0; @@ -4268,7 +4313,8 @@ int ARGBShade(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { + if (src_stride_argb == width * 4 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_argb = 0; @@ -4327,7 +4373,8 @@ int InterpolatePlane(const uint8_t* src0, dst_stride = -dst_stride; } // Coalesce rows. - if (src_stride0 == width && src_stride1 == width && dst_stride == width) { + if (src_stride0 == width && src_stride1 == width && dst_stride == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride0 = src_stride1 = dst_stride = 0; @@ -4401,7 +4448,8 @@ int InterpolatePlane_16(const uint16_t* src0, dst_stride = -dst_stride; } // Coalesce rows. - if (src_stride0 == width && src_stride1 == width && dst_stride == width) { + if (src_stride0 == width && src_stride1 == width && dst_stride == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride0 = src_stride1 = dst_stride = 0; @@ -4531,7 +4579,8 @@ int ARGBShuffle(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { + if (src_stride_argb == width * 4 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_argb = 0; @@ -4607,7 +4656,8 @@ int AR64Shuffle(const uint16_t* src_ar64, src_stride_ar64 = -src_stride_ar64; } // Coalesce rows. - if (src_stride_ar64 == width * 4 && dst_stride_ar64 == width * 4) { + if (src_stride_ar64 == width * 4 && dst_stride_ar64 == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_ar64 = dst_stride_ar64 = 0; @@ -5014,7 +5064,8 @@ int ARGBPolynomial(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { + if (src_stride_argb == width * 4 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_argb = 0; @@ -5064,7 +5115,8 @@ int HalfFloatPlane(const uint16_t* src_y, src_stride_y = -src_stride_y; } // Coalesce rows. - if (src_stride_y == width && dst_stride_y == width) { + if (src_stride_y == width && dst_stride_y == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_y = dst_stride_y = 0; @@ -5168,7 +5220,8 @@ int ARGBLumaColorTable(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { + if (src_stride_argb == width * 4 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_argb = 0; @@ -5208,7 +5261,8 @@ int ARGBCopyAlpha(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { + if (src_stride_argb == width * 4 && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_argb = 0; @@ -5256,7 +5310,8 @@ int ARGBExtractAlpha(const uint8_t* src_argb, src_stride_argb = -src_stride_argb; } // Coalesce rows. - if (src_stride_argb == width * 4 && dst_stride_a == width) { + if (src_stride_argb == width * 4 && dst_stride_a == width && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_argb = dst_stride_a = 0; @@ -5322,7 +5377,8 @@ int ARGBCopyYToAlpha(const uint8_t* src_y, src_stride_y = -src_stride_y; } // Coalesce rows. - if (src_stride_y == width && dst_stride_argb == width * 4) { + if (src_stride_y == width && dst_stride_argb == width * 4 && + (ptrdiff_t)width * height <= INT_MAX) { width *= height; height = 1; src_stride_y = dst_stride_argb = 0;