Skip to content

Commit 2ed2838

Browse files
Ensure objects remain valid after calling policy am: 7d7ac48 am: 8d8ffe0
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/native/+/31313194 Change-Id: I62b8288022080f25406e21dc97168e2294ec049a Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2 parents 03c24bd + 8d8ffe0 commit 2ed2838

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

services/inputflinger/dispatcher/InputDispatcher.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5745,9 +5745,7 @@ void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime,
57455745

57465746
bool restartEvent;
57475747
if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) {
5748-
KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry));
5749-
restartEvent =
5750-
afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled);
5748+
restartEvent = afterKeyEventLockedInterruptable(connection, dispatchEntry, handled);
57515749
} else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) {
57525750
MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry));
57535751
restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry,
@@ -5975,7 +5973,17 @@ void InputDispatcher::processConnectionResponsiveLocked(const Connection& connec
59755973

59765974
bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection,
59775975
DispatchEntry* dispatchEntry,
5978-
KeyEntry& keyEntry, bool handled) {
5976+
bool handled) {
5977+
// The dispatchEntry is currently valid, but it might point to a deleted object after we release
5978+
// the lock. For simplicity, make copies of the data of interest here and assume that
5979+
// 'dispatchEntry' is not valid after this section.
5980+
// Hold a strong reference to the EventEntry to ensure it's valid for the duration of this
5981+
// function, even if the DispatchEntry gets destroyed and releases its share of the ownership.
5982+
std::shared_ptr<EventEntry> eventEntry = dispatchEntry->eventEntry;
5983+
const bool hasForegroundTarget = dispatchEntry->hasForegroundTarget();
5984+
KeyEntry& keyEntry = static_cast<KeyEntry&>(*(eventEntry));
5985+
// To prevent misuse, ensure dispatchEntry is no longer valid.
5986+
dispatchEntry = nullptr;
59795987
if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) {
59805988
if (!handled) {
59815989
// Report the key as unhandled, since the fallback was not handled.
@@ -5992,7 +6000,7 @@ bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& con
59926000
connection->inputState.removeFallbackKey(originalKeyCode);
59936001
}
59946002

5995-
if (handled || !dispatchEntry->hasForegroundTarget()) {
6003+
if (handled || !hasForegroundTarget) {
59966004
// If the application handles the original key for which we previously
59976005
// generated a fallback or if the window is not a foreground window,
59986006
// then cancel the associated fallback key, if any.

services/inputflinger/dispatcher/InputDispatcher.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ class InputDispatcher : public android::InputDispatcherInterface {
666666
void updateLastAnrStateLocked(const std::string& windowLabel, const std::string& reason)
667667
REQUIRES(mLock);
668668
bool afterKeyEventLockedInterruptable(const sp<Connection>& connection,
669-
DispatchEntry* dispatchEntry, KeyEntry& keyEntry,
669+
DispatchEntry* dispatchEntry,
670670
bool handled) REQUIRES(mLock);
671671
bool afterMotionEventLockedInterruptable(const sp<Connection>& connection,
672672
DispatchEntry* dispatchEntry, MotionEntry& motionEntry,

0 commit comments

Comments
 (0)