Bounds-check detection sensor trigger type (#11091)

This commit is contained in:
Thomas Göttgens
2026-07-20 14:49:29 +02:00
committed by GitHub
co-authored by GitHub
parent 5cf346311c
commit 0199a1f3ff
+12 -3
View File
@@ -46,6 +46,16 @@ const static DetectionSensorTriggerHandler handlers[_meshtastic_ModuleConfig_Det
[meshtastic_ModuleConfig_DetectionSensorConfig_TriggerType_EITHER_EDGE_ACTIVE_HIGH] = detection_trigger_either_edge,
};
// The configured trigger type arrives as an unvalidated protobuf enum, so a value outside the
// generated range would index past the handler table. Fall back to the schema default instead.
static meshtastic_ModuleConfig_DetectionSensorConfig_TriggerType configuredTriggerType()
{
const uint32_t configured = (uint32_t)moduleConfig.detection_sensor.detection_trigger_type;
if (configured > (uint32_t)_meshtastic_ModuleConfig_DetectionSensorConfig_TriggerType_MAX)
return _meshtastic_ModuleConfig_DetectionSensorConfig_TriggerType_MIN;
return (meshtastic_ModuleConfig_DetectionSensorConfig_TriggerType)configured;
}
int32_t DetectionSensorModule::runOnce()
{
/*
@@ -89,8 +99,7 @@ int32_t DetectionSensorModule::runOnce()
if (!Throttle::isWithinTimespanMs(lastSentToMesh,
Default::getConfiguredOrDefaultMs(moduleConfig.detection_sensor.minimum_broadcast_secs))) {
bool isDetected = hasDetectionEvent();
DetectionSensorTriggerVerdict verdict =
handlers[moduleConfig.detection_sensor.detection_trigger_type](wasDetected, isDetected);
DetectionSensorTriggerVerdict verdict = handlers[configuredTriggerType()](wasDetected, isDetected);
wasDetected = isDetected;
switch (verdict) {
case DetectionSensorVerdictDetected:
@@ -160,5 +169,5 @@ bool DetectionSensorModule::hasDetectionEvent()
{
bool currentState = digitalRead(moduleConfig.detection_sensor.monitor_pin);
// LOG_DEBUG("Detection Sensor Module: Current state: %i", currentState);
return (moduleConfig.detection_sensor.detection_trigger_type & 1) ? currentState : !currentState;
return (configuredTriggerType() & 1) ? currentState : !currentState;
}