Fix a segmentation fault in NoStaleEvents tests
This CL fixes an issue where a sensor may not generate any events
and the processing code calls .front() on an empty std::vector.
Bug: 291779133
Test: Presubmits
Change-Id: Ibb118f239ce9ea42fc25d1d05ba6bd46a60231f0
diff --git a/sensors/aidl/vts/VtsAidlHalSensorsTargetTest.cpp b/sensors/aidl/vts/VtsAidlHalSensorsTargetTest.cpp
index 618acbb..456aee7 100644
--- a/sensors/aidl/vts/VtsAidlHalSensorsTargetTest.cpp
+++ b/sensors/aidl/vts/VtsAidlHalSensorsTargetTest.cpp
@@ -921,9 +921,15 @@
continue;
}
+ // Skip sensors with no events
+ const std::vector<Event> events = callback.getEvents(sensor.sensorHandle);
+ if (events.empty()) {
+ continue;
+ }
+
// Ensure that the first event received is not stale by ensuring that its timestamp is
// sufficiently different from the previous event
- const Event newEvent = callback.getEvents(sensor.sensorHandle).front();
+ const Event newEvent = events.front();
std::chrono::milliseconds delta =
duration_cast<std::chrono::milliseconds>(std::chrono::nanoseconds(
newEvent.timestamp - lastEventTimestampMap[sensor.sensorHandle]));
diff --git a/sensors/common/vts/2_X/VtsHalSensorsV2_XTargetTest.h b/sensors/common/vts/2_X/VtsHalSensorsV2_XTargetTest.h
index aa6e881..b381580 100644
--- a/sensors/common/vts/2_X/VtsHalSensorsV2_XTargetTest.h
+++ b/sensors/common/vts/2_X/VtsHalSensorsV2_XTargetTest.h
@@ -806,9 +806,15 @@
continue;
}
+ // Skip sensors with no events
+ const std::vector<EventType> events = callback.getEvents(sensor.sensorHandle);
+ if (events.empty()) {
+ continue;
+ }
+
// Ensure that the first event received is not stale by ensuring that its timestamp is
// sufficiently different from the previous event
- const EventType newEvent = callback.getEvents(sensor.sensorHandle).front();
+ const EventType newEvent = events.front();
milliseconds delta = duration_cast<milliseconds>(
nanoseconds(newEvent.timestamp - lastEventTimestampMap[sensor.sensorHandle]));
milliseconds sensorMinDelay = duration_cast<milliseconds>(microseconds(sensor.minDelay));