From 0199a1f3ffea1d82ff23c5931df74b459a17a5b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Mon, 20 Jul 2026 14:49:29 +0200 Subject: [PATCH] Bounds-check detection sensor trigger type (#11091) --- src/modules/DetectionSensorModule.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/modules/DetectionSensorModule.cpp b/src/modules/DetectionSensorModule.cpp index ca682b772..7e33c7a75 100644 --- a/src/modules/DetectionSensorModule.cpp +++ b/src/modules/DetectionSensorModule.cpp @@ -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; } \ No newline at end of file