From c76bc32d959eb6bb3f0d1b9d70eaf18ffcaf909d Mon Sep 17 00:00:00 2001 From: Chih-Hung Hsieh Date: Thu, 1 Dec 2022 17:52:50 -0800 Subject: [PATCH] Fix "Attempt to delete released memory" warning. EXPECT_NONFATAL_FAILURE(statement, ...) expands to do { ... statement ... } while (::testing::internal::AlwaysFalse()); Clang analyzer cannot see into AlwaysFalse(), and gives a false warning assuming that the do loop can be done twice. Use a unique_ptr::reset to call delete, because reset can be called multiple times.. Bug: 259999410 Test: make tidy-system-core-fastboot_subset Change-Id: Ie536f76635dd55f4e47b80eed6498fd1193b88ee --- fastboot/socket_test.cpp | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/fastboot/socket_test.cpp b/fastboot/socket_test.cpp index 373abc35e..74ff377d3 100644 --- a/fastboot/socket_test.cpp +++ b/fastboot/socket_test.cpp @@ -293,23 +293,23 @@ TEST(SocketMockTest, TestSendSuccess) { } TEST(SocketMockTest, TestSendFailure) { - SocketMock* mock = new SocketMock; + std::unique_ptr mock(new SocketMock); mock->ExpectSendFailure("foo"); - EXPECT_FALSE(SendString(mock, "foo")); + EXPECT_FALSE(SendString(mock.get(), "foo")); - EXPECT_NONFATAL_FAILURE(SendString(mock, "foo"), "no message was expected"); + EXPECT_NONFATAL_FAILURE(SendString(mock.get(), "foo"), "no message was expected"); mock->ExpectSend("foo"); - EXPECT_NONFATAL_FAILURE(SendString(mock, "bar"), "expected foo, but got bar"); - EXPECT_TRUE(SendString(mock, "foo")); + EXPECT_NONFATAL_FAILURE(SendString(mock.get(), "bar"), "expected foo, but got bar"); + EXPECT_TRUE(SendString(mock.get(), "foo")); mock->AddReceive("foo"); - EXPECT_NONFATAL_FAILURE(SendString(mock, "foo"), "called out-of-order"); - EXPECT_TRUE(ReceiveString(mock, "foo")); + EXPECT_NONFATAL_FAILURE(SendString(mock.get(), "foo"), "called out-of-order"); + EXPECT_TRUE(ReceiveString(mock.get(), "foo")); mock->ExpectSend("foo"); - EXPECT_NONFATAL_FAILURE(delete mock, "1 event(s) were not handled"); + EXPECT_NONFATAL_FAILURE(mock.reset(), "1 event(s) were not handled"); } TEST(SocketMockTest, TestReceiveSuccess) { @@ -331,33 +331,33 @@ TEST(SocketMockTest, TestReceiveSuccess) { } TEST(SocketMockTest, TestReceiveFailure) { - SocketMock* mock = new SocketMock; + std::unique_ptr mock(new SocketMock); mock->AddReceiveFailure(); - EXPECT_FALSE(ReceiveString(mock, "foo")); + EXPECT_FALSE(ReceiveString(mock.get(), "foo")); EXPECT_FALSE(mock->ReceiveTimedOut()); mock->AddReceiveTimeout(); - EXPECT_FALSE(ReceiveString(mock, "foo")); + EXPECT_FALSE(ReceiveString(mock.get(), "foo")); EXPECT_TRUE(mock->ReceiveTimedOut()); mock->AddReceive("foo"); mock->AddReceiveFailure(); - EXPECT_FALSE(ReceiveString(mock, "foobar")); + EXPECT_FALSE(ReceiveString(mock.get(), "foobar")); - EXPECT_NONFATAL_FAILURE(ReceiveString(mock, "foo"), "no message was ready"); + EXPECT_NONFATAL_FAILURE(ReceiveString(mock.get(), "foo"), "no message was ready"); mock->ExpectSend("foo"); - EXPECT_NONFATAL_FAILURE(ReceiveString(mock, "foo"), "called out-of-order"); - EXPECT_TRUE(SendString(mock, "foo")); + EXPECT_NONFATAL_FAILURE(ReceiveString(mock.get(), "foo"), "called out-of-order"); + EXPECT_TRUE(SendString(mock.get(), "foo")); char c; mock->AddReceive("foo"); EXPECT_NONFATAL_FAILURE(mock->Receive(&c, 1, 0), "not enough bytes (1) for foo"); - EXPECT_TRUE(ReceiveString(mock, "foo")); + EXPECT_TRUE(ReceiveString(mock.get(), "foo")); mock->AddReceive("foo"); - EXPECT_NONFATAL_FAILURE(delete mock, "1 event(s) were not handled"); + EXPECT_NONFATAL_FAILURE(mock.reset(), "1 event(s) were not handled"); } TEST(SocketMockTest, TestAcceptSuccess) { @@ -372,14 +372,14 @@ TEST(SocketMockTest, TestAcceptSuccess) { } TEST(SocketMockTest, TestAcceptFailure) { - SocketMock* mock = new SocketMock; + std::unique_ptr mock(new SocketMock); EXPECT_NONFATAL_FAILURE(mock->Accept(), "no socket was ready"); mock->ExpectSend("foo"); EXPECT_NONFATAL_FAILURE(mock->Accept(), "called out-of-order"); - EXPECT_TRUE(SendString(mock, "foo")); + EXPECT_TRUE(SendString(mock.get(), "foo")); mock->AddAccept(nullptr); - EXPECT_NONFATAL_FAILURE(delete mock, "1 event(s) were not handled"); + EXPECT_NONFATAL_FAILURE(mock.reset(), "1 event(s) were not handled"); }