From 903c91cc2e3c831748d6f6168b7a8bb98f6ab9ec Mon Sep 17 00:00:00 2001 From: Frank Barchard Date: Tue, 2 Feb 2016 14:32:12 -0800 Subject: [PATCH] fix for ubsan on unittest.h fastrand() internal math of the fastrand function uses a multiply and add that overflows a signed int. This triggers a ubsan failure: ../../unit_test/../unit_test/unit_test.h:60:33: runtime error: signed integer overflow: 56248274 * 214013 cannot be represented in type 'int' This change casts the intermediate math to unsigned int to avoid the overflow. For more info on ubsan, see http://dev.chromium.org/developers/testing/undefinedbehaviorsanitizer TESTED=Passing compilation using: GYP_DEFINES="ubsan=1" GYP_DEFINES="ubsan_vptr=1" R=harryjin@google.com, pbos@webrtc.org BUG=libyuv:563 Review URL: https://codereview.chromium.org/1662453003 . --- README.chromium | 2 +- include/libyuv/version.h | 2 +- unit_test/unit_test.cc | 12 ++++++------ unit_test/unit_test.h | 6 +++--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/README.chromium b/README.chromium index 0064bc65f..0db329dc2 100644 --- a/README.chromium +++ b/README.chromium @@ -1,6 +1,6 @@ Name: libyuv URL: http://code.google.com/p/libyuv/ -Version: 1570 +Version: 1571 License: BSD License File: LICENSE diff --git a/include/libyuv/version.h b/include/libyuv/version.h index 796fc55e2..62d50e333 100644 --- a/include/libyuv/version.h +++ b/include/libyuv/version.h @@ -11,6 +11,6 @@ #ifndef INCLUDE_LIBYUV_VERSION_H_ // NOLINT #define INCLUDE_LIBYUV_VERSION_H_ -#define LIBYUV_VERSION 1570 +#define LIBYUV_VERSION 1571 #endif // INCLUDE_LIBYUV_VERSION_H_ NOLINT diff --git a/unit_test/unit_test.cc b/unit_test/unit_test.cc index c3fec6b39..c98c285cb 100644 --- a/unit_test/unit_test.cc +++ b/unit_test/unit_test.cc @@ -20,7 +20,7 @@ // TODO(fbarchard): Add command line parsing to pass this as option. #define BENCHMARK_ITERATIONS 1 -int fastrand_seed = 0xfb; +unsigned int fastrand_seed = 0xfb; DEFINE_int32(libyuv_width, 0, "width of test image."); DEFINE_int32(libyuv_height, 0, "height of test image."); @@ -125,7 +125,7 @@ LibYUVColorTest::LibYUVColorTest() : benchmark_cpu_info_ = atoi(cpu_flags); // NOLINT } if (FLAGS_libyuv_cpu_info) { - benchmark_cpu_info_ = FLAGS_libyuv_cpu_info; + benchmark_cpu_info_ = FLAGS_libyuv_cpu_info; } benchmark_pixels_div256_ = static_cast(( static_cast(Abs(benchmark_width_)) * @@ -177,7 +177,7 @@ LibYUVScaleTest::LibYUVScaleTest() : benchmark_cpu_info_ = atoi(cpu_flags); // NOLINT } if (FLAGS_libyuv_cpu_info) { - benchmark_cpu_info_ = FLAGS_libyuv_cpu_info; + benchmark_cpu_info_ = FLAGS_libyuv_cpu_info; } benchmark_pixels_div256_ = static_cast(( static_cast(Abs(benchmark_width_)) * @@ -229,7 +229,7 @@ LibYUVRotateTest::LibYUVRotateTest() : benchmark_cpu_info_ = atoi(cpu_flags); // NOLINT } if (FLAGS_libyuv_cpu_info) { - benchmark_cpu_info_ = FLAGS_libyuv_cpu_info; + benchmark_cpu_info_ = FLAGS_libyuv_cpu_info; } benchmark_pixels_div256_ = static_cast(( static_cast(Abs(benchmark_width_)) * @@ -281,7 +281,7 @@ LibYUVPlanarTest::LibYUVPlanarTest() : benchmark_cpu_info_ = atoi(cpu_flags); // NOLINT } if (FLAGS_libyuv_cpu_info) { - benchmark_cpu_info_ = FLAGS_libyuv_cpu_info; + benchmark_cpu_info_ = FLAGS_libyuv_cpu_info; } benchmark_pixels_div256_ = static_cast(( static_cast(Abs(benchmark_width_)) * @@ -333,7 +333,7 @@ LibYUVBaseTest::LibYUVBaseTest() : benchmark_cpu_info_ = atoi(cpu_flags); // NOLINT } if (FLAGS_libyuv_cpu_info) { - benchmark_cpu_info_ = FLAGS_libyuv_cpu_info; + benchmark_cpu_info_ = FLAGS_libyuv_cpu_info; } benchmark_pixels_div256_ = static_cast(( static_cast(Abs(benchmark_width_)) * diff --git a/unit_test/unit_test.h b/unit_test/unit_test.h index f816ec711..009ff62ab 100644 --- a/unit_test/unit_test.h +++ b/unit_test/unit_test.h @@ -55,10 +55,10 @@ static inline double get_time() { } #endif -extern int fastrand_seed; +extern unsigned int fastrand_seed; inline int fastrand() { - fastrand_seed = fastrand_seed * 214013 + 2531011; - return (fastrand_seed >> 16) & 0xffff; + fastrand_seed = fastrand_seed * 214013u + 2531011u; + return static_cast((fastrand_seed >> 16) & 0xffff); } static inline void MemRandomize(uint8* dst, int64 len) {