From d9035ef360bab4a9ed484398d40d463547d9e2e5 Mon Sep 17 00:00:00 2001 From: easy Date: Tue, 4 Aug 2020 17:05:57 +1000 Subject: [PATCH] asan: enable more asan and ubsan checks. (#224) * Don't call nullptr->f(). * Avoid signed overflow. --- .bazelrc | 8 ++++++-- api/test/nostd/shared_ptr_test.cc | 2 +- api/test/nostd/unique_ptr_test.cc | 5 ++++- sdk/test/metrics/counter_aggregator_test.cc | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/.bazelrc b/.bazelrc index e893a2e2fa..a10ad12060 100644 --- a/.bazelrc +++ b/.bazelrc @@ -5,9 +5,13 @@ build --copt -DGRPC_BAZEL_BUILD # --config=asan : Address Sanitizer. -common:asan --copt -fsanitize=address common:asan --copt -DADDRESS_SANITIZER -common:asan --linkopt -fsanitize=address +common:asan --copt -fsanitize=address,bool,float-cast-overflow,integer-divide-by-zero,null,return,returns-nonnull-attribute,shift-exponent,signed-integer-overflow,unreachable,vla-bound +common:asan --copt -fsanitize-address-use-after-scope +common:asan --copt -fno-sanitize-recover=all +common:asan --linkopt -fsanitize=address,bool,float-cast-overflow,integer-divide-by-zero,null,return,returns-nonnull-attribute,shift-exponent,signed-integer-overflow,unreachable,vla-bound +common:asan --linkopt -fsanitize-address-use-after-scope +common:asan --linkopt -fno-sanitize-recover=all common:asan --cc_output_directory_tag=asan # --config=tsan : Thread Sanitizer. diff --git a/api/test/nostd/shared_ptr_test.cc b/api/test/nostd/shared_ptr_test.cc index 7f18d3bdb5..46151d88c4 100644 --- a/api/test/nostd/shared_ptr_test.cc +++ b/api/test/nostd/shared_ptr_test.cc @@ -125,7 +125,7 @@ TEST(SharedPtrTest, PointerOperators) EXPECT_EQ(&*ptr1, value); EXPECT_EQ( - shared_ptr {}->f(), 123); + shared_ptr { new B }->f(), 123); } TEST(SharedPtrTest, Swap) diff --git a/api/test/nostd/unique_ptr_test.cc b/api/test/nostd/unique_ptr_test.cc index ee1ae17f4a..731c5f79c2 100644 --- a/api/test/nostd/unique_ptr_test.cc +++ b/api/test/nostd/unique_ptr_test.cc @@ -89,6 +89,9 @@ TEST(UniquePtrTest, StdUniquePtrConversionOperator) EXPECT_EQ(ptr1.get(), nullptr); EXPECT_EQ(ptr2.get(), value); + ptr2 = nullptr; + EXPECT_EQ(ptr2.get(), nullptr); + EXPECT_TRUE((std::is_assignable, unique_ptr &&>::value)); EXPECT_FALSE((std::is_assignable, unique_ptr &>::value)); } @@ -109,7 +112,7 @@ TEST(UniquePtrTest, PointerOperators) EXPECT_EQ(&*ptr1, value); EXPECT_EQ( - unique_ptr {}->f(), 123); + unique_ptr { new B }->f(), 123); } TEST(UniquePtrTest, Reset) diff --git a/sdk/test/metrics/counter_aggregator_test.cc b/sdk/test/metrics/counter_aggregator_test.cc index 1e2b1dce8c..09d146b15f 100644 --- a/sdk/test/metrics/counter_aggregator_test.cc +++ b/sdk/test/metrics/counter_aggregator_test.cc @@ -37,7 +37,7 @@ TEST(CounterAggregator, Update) int sum = 0; for (int i = 0; i < 100; i++) { - int tmp = std::rand(); + int tmp = std::rand() % 128; beta.update(tmp); sum += tmp; }