Skip to content

Commit ee2f164

Browse files
inputflinger_tests: Fix asan error about new-delete type mismatch
Cast the value returned by InputDispatcher::verifyInputEvent to the correct event type and call delete explicitly on the pointer. This fixes the `new-delete-type-mismatch` reported by asan with clang-r547379. We cannot rely on virtual destructor calls because InputEvent and subclasses don't have a virtual destructor. The proper fix is to either consider using std::variant in some way, or introduce an intermediate POD data structure that we will put the data into just prior to signing. Adding virtual functions to these classes is undesirable as the bytes in these objects are getting signed. Bug: 392703785 Test: run inputflinger_tests on the host with aosp/3419479 Change-Id: Ie78684539536c8b62acc8d540df57b2bbaac3649
1 parent 52b4e75 commit ee2f164

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

services/inputflinger/tests/InputDispatcher_test.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8132,6 +8132,17 @@ TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
81328132
ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
81338133
ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
81348134
ASSERT_EQ(0, verifiedKey.repeatCount);
8135+
8136+
// InputEvent and subclasses don't have a virtual destructor and only
8137+
// InputEvent's destructor gets called when `verified` goes out of scope,
8138+
// even if `verifyInputEvent` returns an object of a subclass. To fix this,
8139+
// we should either consider using std::variant in some way, or introduce an
8140+
// intermediate POD data structure that we will put the data into just prior
8141+
// to signing. Adding virtual functions to these classes is undesirable as
8142+
// the bytes in these objects are getting signed. As a temporary fix, cast
8143+
// the pointer to the correct class (which is statically known) before
8144+
// destruction.
8145+
delete (VerifiedKeyEvent*)verified.release();
81358146
}
81368147

81378148
TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
@@ -8179,6 +8190,10 @@ TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
81798190
EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
81808191
EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
81818192
EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
8193+
8194+
// Cast to the correct type before destruction. See explanation at the end
8195+
// of the VerifyInputEvent_KeyEvent test.
8196+
delete (VerifiedMotionEvent*)verified.release();
81828197
}
81838198

81848199
/**

0 commit comments

Comments
 (0)