diff options
author | George Burgess IV <gbiv@google.com> | 2017-09-12 16:54:53 -0700 |
---|---|---|
committer | George Burgess IV <gbiv@google.com> | 2017-09-12 17:01:20 -0700 |
commit | 180e5e7021d17176bf2f5f110970e58cb19d6310 (patch) | |
tree | 2bfa06335390fe0ceb35a31bc05712ab9c790288 /libmemunreachable | |
parent | cfadedb1391f82a25bc0209aaa2e069875657776 (diff) | |
download | system_core-180e5e7021d17176bf2f5f110970e58cb19d6310.tar.gz system_core-180e5e7021d17176bf2f5f110970e58cb19d6310.tar.bz2 system_core-180e5e7021d17176bf2f5f110970e58cb19d6310.zip |
Fix static analyzer warnings
The static analyzer was complaining that we were potentially leaking
memory here (in `ASSERT_NE(ptr, nullptr)` after `new (char)`). This
wasn't correct, but it's also not possible for `new` to return nullptr
without std::nothrow.
In any case, swap to direct calls to `::operator new`, since it looks
like this test explicitly wants calls to `::operator new` to be emitted
(which the C++ standard doesn't guarantee for all `new` expressions).
Bug: 27101951
Test: mma; static analyzer warnings are gone. Also ran
memunreachable_test on marlin; no failures.
Change-Id: Ia740e41079f263040da978ba1ccc71c9c39f53fd
Diffstat (limited to 'libmemunreachable')
-rw-r--r-- | libmemunreachable/tests/DisableMalloc_test.cpp | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/libmemunreachable/tests/DisableMalloc_test.cpp b/libmemunreachable/tests/DisableMalloc_test.cpp index c630049fc..f4467190c 100644 --- a/libmemunreachable/tests/DisableMalloc_test.cpp +++ b/libmemunreachable/tests/DisableMalloc_test.cpp @@ -73,15 +73,18 @@ TEST_F(DisableMallocTest, deadlock_allocate) { TEST_F(DisableMallocTest, deadlock_new) { ASSERT_DEATH( { - char* ptr = new (char); + // C++ allows `new Foo` to be replaced with a stack allocation or merged + // with future `new Foo` expressions, provided certain conditions are + // met [expr.new/10]. None of this applies to `operator new(size_t)`. + void* ptr = ::operator new(1); ASSERT_NE(ptr, nullptr); - delete (ptr); + ::operator delete(ptr); { alarm(100ms); ScopedDisableMalloc disable_malloc; - char* ptr = new (std::nothrow)(char); + void* ptr = ::operator new(1); ASSERT_NE(ptr, nullptr); - delete (ptr); + ::operator delete(ptr); } }, ""); @@ -90,14 +93,12 @@ TEST_F(DisableMallocTest, deadlock_new) { TEST_F(DisableMallocTest, deadlock_delete) { ASSERT_DEATH( { - char* ptr = new (char); + void* ptr = ::operator new(1); ASSERT_NE(ptr, nullptr); { alarm(250ms); ScopedDisableMalloc disable_malloc; - delete (ptr); - // Force ptr usage or this code gets optimized away by the arm64 compiler. - ASSERT_NE(ptr, nullptr); + ::operator delete(ptr); } }, ""); |