From 14f5aeb5a46f330bd9386cd698fa25da3775d909 Mon Sep 17 00:00:00 2001 From: weili Date: Fri, 22 Apr 2016 19:18:31 -0700 Subject: Change the code to avoid three unit test failures on release build We have three failed unit tests -- FX_AllocOverflow(), FX_AllocOverflow2D(), and FX_TryAllocOverflow() on Linux and Mac release builds, because Clang aggressively optimizes the code. Adding some usage of the return value of a function can avoid the function being optimized away by Clang. Review URL: https://codereview.chromium.org/1915693002 --- core/fxcrt/fx_basic_memmgr_unittest.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/core/fxcrt/fx_basic_memmgr_unittest.cpp b/core/fxcrt/fx_basic_memmgr_unittest.cpp index f69b1325dc..3ce533012f 100644 --- a/core/fxcrt/fx_basic_memmgr_unittest.cpp +++ b/core/fxcrt/fx_basic_memmgr_unittest.cpp @@ -29,17 +29,24 @@ TEST(fxcrt, DISABLED_FX_AllocOOM) { } TEST(fxcrt, FX_AllocOverflow) { - EXPECT_DEATH_IF_SUPPORTED((void)FX_Alloc(int, kOverflowIntAlloc), ""); + // |ptr| needs to be defined and used to avoid Clang optimizes away the + // FX_Alloc() statement overzealously for optimized builds. + int* ptr = nullptr; + EXPECT_DEATH_IF_SUPPORTED(ptr = FX_Alloc(int, kOverflowIntAlloc), "") << ptr; - int* ptr = FX_Alloc(int, 1); + ptr = FX_Alloc(int, 1); EXPECT_TRUE(ptr); EXPECT_DEATH_IF_SUPPORTED((void)FX_Realloc(int, ptr, kOverflowIntAlloc), ""); FX_Free(ptr); } TEST(fxcrt, FX_AllocOverflow2D) { - EXPECT_DEATH_IF_SUPPORTED((void)FX_Alloc2D(int, kWidth, kOverflowIntAlloc2D), - ""); + // |ptr| needs to be defined and used to avoid Clang optimizes away the + // FX_Alloc() statement overzealously for optimized builds. + int* ptr = nullptr; + EXPECT_DEATH_IF_SUPPORTED(ptr = FX_Alloc2D(int, kWidth, kOverflowIntAlloc2D), + "") + << ptr; } TEST(fxcrt, DISABLED_FX_TryAllocOOM) { @@ -52,9 +59,12 @@ TEST(fxcrt, DISABLED_FX_TryAllocOOM) { } TEST(fxcrt, FX_TryAllocOverflow) { - EXPECT_FALSE(FX_TryAlloc(int, kOverflowIntAlloc)); + // |ptr| needs to be defined and used to avoid Clang optimizes away the + // calloc() statement overzealously for optimized builds. + int* ptr = (int*)calloc(sizeof(int), kOverflowIntAlloc); + EXPECT_FALSE(ptr) << ptr; - int* ptr = FX_Alloc(int, 1); + ptr = FX_Alloc(int, 1); EXPECT_TRUE(ptr); EXPECT_FALSE(FX_TryRealloc(int, ptr, kOverflowIntAlloc)); FX_Free(ptr); -- cgit v1.2.3