From b4ccc01cdd478e9c6cf11866ac9c06a7392b5b08 Mon Sep 17 00:00:00 2001 From: Caner Karaca Date: Thu, 8 Oct 2026 00:22:13 +0300 Subject: [PATCH] fix(sirens): safely parse siren states whether defined as array or object --- src/features/sirens.cpp | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/features/sirens.cpp b/src/features/sirens.cpp index 76c02ea8..1d119d83 100755 --- a/src/features/sirens.cpp +++ b/src/features/sirens.cpp @@ -551,21 +551,41 @@ VehicleSirenData::VehicleSirenData(const nlohmann::json &json) } } - for (nlohmann::json::const_iterator stateObject = states.begin(); stateObject != states.end(); ++stateObject) + if (states.is_object()) { - VehicleSirenState *state = new VehicleSirenState(stateObject.key(), stateObject.value()); - - if (!state->Validate) + for (nlohmann::json::const_iterator stateObject = states.begin(); stateObject != states.end(); ++stateObject) { - LOG_VERBOSE("Failed to set up state {}, could not configure object from manifest!", stateObject.key()); + VehicleSirenState *state = new VehicleSirenState(stateObject.key(), stateObject.value()); - continue; + if (!state->Validate) + { + LOG_VERBOSE("Failed to set up state {}, could not configure object from manifest!", stateObject.key()); + delete state; + continue; + } + + States.push_back(state); } + } + else if (states.is_array()) + { + int idx = 0; + for (nlohmann::json::const_iterator stateObject = states.begin(); stateObject != states.end(); ++stateObject, ++idx) + { + VehicleSirenState *state = new VehicleSirenState(std::to_string(idx), stateObject.value()); - States.push_back(state); + if (!state->Validate) + { + LOG_VERBOSE("Failed to set up state {}, could not configure object from manifest!", idx); + delete state; + continue; + } + + States.push_back(state); + } } - Validate = true; + Validate = !States.empty(); }; void Sirens::Parse(const nlohmann::json &data, int model)