Prevent use of invalidated iterator
If the underlying container changes, such as by deleting an element from
it, all of the outstanding iterators get invalidated.
Currently in EventHub, we are using a for loop to go through the entire
map, and deleting the map contents from inside this loop.
To avoid this, do not store the iterators explicitly.
Bug: 163171599
Test: atest inputflinger_tests:EventHubTest#InputEvent_TimestampIsMonotonic
Test: atest inputflinger_tests
Change-Id: I3af0721d54cc03668a68848f6f690a2d523e809b
diff --git a/services/inputflinger/Android.bp b/services/inputflinger/Android.bp
index 28cce47..a0de607 100644
--- a/services/inputflinger/Android.bp
+++ b/services/inputflinger/Android.bp
@@ -25,6 +25,9 @@
"-Wshadow-field-in-constructor-modified",
"-Wshadow-uncaptured-local",
],
+ sanitize: {
+ misc_undefined: ["bounds"],
+ },
}
/////////////////////////////////////////////////
diff --git a/services/inputflinger/benchmarks/InputDispatcher_benchmarks.cpp b/services/inputflinger/benchmarks/InputDispatcher_benchmarks.cpp
index b31980b..f99fffe 100644
--- a/services/inputflinger/benchmarks/InputDispatcher_benchmarks.cpp
+++ b/services/inputflinger/benchmarks/InputDispatcher_benchmarks.cpp
@@ -103,7 +103,7 @@
class FakeInputReceiver {
public:
void consumeEvent() {
- uint32_t consumeSeq;
+ uint32_t consumeSeq = 0;
InputEvent* event;
std::chrono::time_point start = std::chrono::steady_clock::now();
diff --git a/services/inputflinger/reader/EventHub.cpp b/services/inputflinger/reader/EventHub.cpp
index cde977f..76b9419 100644
--- a/services/inputflinger/reader/EventHub.cpp
+++ b/services/inputflinger/reader/EventHub.cpp
@@ -187,7 +187,6 @@
EventHub::Device::~Device() {
close();
- delete configuration;
}
void EventHub::Device::close() {
@@ -284,11 +283,14 @@
if (configurationFile.empty()) {
ALOGD("No input device configuration file found for device '%s'.", identifier.name.c_str());
} else {
- status_t status = PropertyMap::load(String8(configurationFile.c_str()), &configuration);
+ PropertyMap* propertyMap;
+ status_t status = PropertyMap::load(String8(configurationFile.c_str()), &propertyMap);
if (status) {
ALOGE("Error loading input device configuration file for device '%s'. "
"Using default configuration.",
identifier.name.c_str());
+ } else {
+ configuration = std::unique_ptr<PropertyMap>(propertyMap);
}
}
}
@@ -305,7 +307,7 @@
}
status_t EventHub::Device::loadKeyMapLocked() {
- return keyMap.load(identifier, configuration);
+ return keyMap.load(identifier, configuration.get());
}
bool EventHub::Device::isExternalDeviceLocked() {
@@ -415,8 +417,6 @@
: mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD),
mNextDeviceId(1),
mControllerNumbers(),
- mOpeningDevices(0),
- mClosingDevices(0),
mNeedToSendFinishedDeviceScan(false),
mNeedToReopenDevices(false),
mNeedToScanDevices(true),
@@ -471,8 +471,6 @@
EventHub::~EventHub(void) {
closeAllDevicesLocked();
- mClosingDevices.clear();
-
::close(mEpollFd);
::close(mINotifyFd);
::close(mWakeReadPipeFd);
@@ -1487,7 +1485,8 @@
if (device->classes.test(InputDeviceClass::KEYBOARD)) {
// Register the keyboard as a built-in keyboard if it is eligible.
if (!keyMapStatus && mBuiltInKeyboardId == NO_BUILT_IN_KEYBOARD &&
- isEligibleBuiltInKeyboard(device->identifier, device->configuration, &device->keyMap)) {
+ isEligibleBuiltInKeyboard(device->identifier, device->configuration.get(),
+ &device->keyMap)) {
mBuiltInKeyboardId = device->id;
}
@@ -1711,8 +1710,8 @@
void EventHub::closeAllDevicesLocked() {
mUnattachedVideoDevices.clear();
- for (const auto& [id, device] : mDevices) {
- closeDeviceLocked(*device);
+ while (!mDevices.empty()) {
+ closeDeviceLocked(*(mDevices.begin()->second));
}
}
@@ -1733,11 +1732,10 @@
}
releaseControllerNumberLocked(device.controllerNumber);
+ device.controllerNumber = 0;
device.close();
-
- // Move device to mClosingDevices
mClosingDevices.push_back(std::move(mDevices[device.id]));
- // Erase device from mDevices
+
mDevices.erase(device.id);
}
diff --git a/services/inputflinger/reader/include/EventHub.h b/services/inputflinger/reader/include/EventHub.h
index 80e80cb..ff12d98 100644
--- a/services/inputflinger/reader/include/EventHub.h
+++ b/services/inputflinger/reader/include/EventHub.h
@@ -417,7 +417,7 @@
BitArray<INPUT_PROP_MAX> propBitmask;
std::string configurationFile;
- PropertyMap* configuration;
+ std::unique_ptr<PropertyMap> configuration;
std::unique_ptr<VirtualKeyMap> virtualKeyMap;
KeyMap keyMap;