From 6fa526429246ed1a8b25151e3714e78fd66b53ef Mon Sep 17 00:00:00 2001 From: Frank Barchard Date: Fri, 14 Aug 2026 16:59:19 -0700 Subject: [PATCH] ARGBToRGB565Dither fix for unaligned dither matrix Copy dither4x4 (16 bytes) into an aligned local uint32_t dither4[4] array using memcpy and index using dither4[y & 3] to avoid unaligned 32-bit pointer dereference traps in fastbuild / UB sanitizers. Bug: None Test: LibYUVConvertTest.TestNoDither, LibYUVConvertTest.TestDither Change-Id: I4447cd2924b16739a9bce2792d57a550dedeb345 Reviewed-on: https://chromium-review.googlesource.com/c/libyuv/libyuv/+/8262354 Reviewed-by: richard winterton Commit-Queue: Frank Barchard Reviewed-by: Wan-Teh Chang --- README.chromium | 2 +- include/libyuv/version.h | 2 +- source/convert_argb.cc | 7 ++++--- source/convert_from_argb.cc | 7 ++++--- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/README.chromium b/README.chromium index 8b6708f23..ce06ea2ce 100644 --- a/README.chromium +++ b/README.chromium @@ -1,6 +1,6 @@ Name: libyuv URL: https://chromium.googlesource.com/libyuv/libyuv/ -Version: 1958 +Version: 1959 Revision: DEPS License: BSD-3-Clause License File: LICENSE diff --git a/include/libyuv/version.h b/include/libyuv/version.h index cd1d85c71..8e6470095 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 1958 +#define LIBYUV_VERSION 1959 #endif // INCLUDE_LIBYUV_VERSION_H_ diff --git a/source/convert_argb.cc b/source/convert_argb.cc index f20083257..8dad827ea 100644 --- a/source/convert_argb.cc +++ b/source/convert_argb.cc @@ -12,6 +12,7 @@ #include #include +#include // For memcpy. #include "libyuv/convert_from_argb.h" #include "libyuv/cpu_id.h" @@ -6314,6 +6315,7 @@ int I420ToRGB565Dither(const uint8_t* src_y, int width, int height) { int y; + uint32_t dither4[4]; void (*I422ToARGBRow)(const uint8_t* y_buf, const uint8_t* u_buf, const uint8_t* v_buf, uint8_t* rgb_buf, const struct YuvConstants* yuvconstants, int width) = @@ -6334,6 +6336,7 @@ int I420ToRGB565Dither(const uint8_t* src_y, if (!dither4x4) { dither4x4 = kDither565_4x4; } + memcpy(dither4, dither4x4, 16); #if defined(HAS_I422TOARGBROW_SSSE3) if (TestCpuFlag(kCpuHasSSSE3)) { I422ToARGBRow = I422ToARGBRow_Any_SSSE3; @@ -6450,9 +6453,7 @@ int I420ToRGB565Dither(const uint8_t* src_y, return 1; for (y = 0; y < height; ++y) { I422ToARGBRow(src_y, src_u, src_v, row_argb, &kYuvI601Constants, width); - ARGBToRGB565DitherRow(row_argb, dst_rgb565, - *(const uint32_t*)(dither4x4 + ((y & 3) << 2)), - width); + ARGBToRGB565DitherRow(row_argb, dst_rgb565, dither4[y & 3], width); dst_rgb565 += dst_stride_rgb565; src_y += src_stride_y; if (y & 1) { diff --git a/source/convert_from_argb.cc b/source/convert_from_argb.cc index a633085a7..11e54fcf1 100644 --- a/source/convert_from_argb.cc +++ b/source/convert_from_argb.cc @@ -11,6 +11,7 @@ #include "libyuv/convert_from_argb.h" #include +#include // For memcpy. #include "libyuv/basic_types.h" #include "libyuv/cpu_id.h" @@ -1582,6 +1583,7 @@ int ARGBToRGB565Dither(const uint8_t* src_argb, int width, int height) { int y; + uint32_t dither4[4]; void (*ARGBToRGB565DitherRow)(const uint8_t* src_argb, uint8_t* dst_rgb, uint32_t dither4, int width) = ARGBToRGB565DitherRow_C; @@ -1597,6 +1599,7 @@ int ARGBToRGB565Dither(const uint8_t* src_argb, if (!dither4x4) { dither4x4 = kDither565_4x4; } + memcpy(dither4, dither4x4, 16); #if defined(HAS_ARGBTORGB565DITHERROW_AVX2) if (TestCpuFlag(kCpuHasAVX2)) { @@ -1637,9 +1640,7 @@ int ARGBToRGB565Dither(const uint8_t* src_argb, #endif for (y = 0; y < height; ++y) { - ARGBToRGB565DitherRow(src_argb, dst_rgb565, - *(const uint32_t*)(dither4x4 + ((y & 3) << 2)), - width); + ARGBToRGB565DitherRow(src_argb, dst_rgb565, dither4[y & 3], width); src_argb += src_stride_argb; dst_rgb565 += dst_stride_rgb565; }