From 176fe941b72e628503417d3b033167a46287a466 Mon Sep 17 00:00:00 2001 From: Brendan Devine Date: Sun, 10 Aug 2025 18:50:26 -0500 Subject: [PATCH] fix: prevent segfault from dereferencing std::max_element on an empty array --- shared/lib_battery_dispatch_automatic_fom.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/lib_battery_dispatch_automatic_fom.cpp b/shared/lib_battery_dispatch_automatic_fom.cpp index f51c7be6c..2576f42a0 100644 --- a/shared/lib_battery_dispatch_automatic_fom.cpp +++ b/shared/lib_battery_dispatch_automatic_fom.cpp @@ -281,7 +281,7 @@ void dispatch_automatic_front_of_meter_t::update_dispatch(size_t year, size_t ho } } pv_hours_on = revenueToPVChargeForecast.size() / _steps_per_hour; - revenueToPVChargeMax = pv_hours_on >= t_duration ? *std::max_element(std::begin(revenueToPVChargeForecast), std::end(revenueToPVChargeForecast)): 0; + revenueToPVChargeMax = (!revenueToPVChargeForecast.empty() && pv_hours_on >= t_duration) ? *std::max_element(std::begin(revenueToPVChargeForecast), std::end(revenueToPVChargeForecast)): 0; } /*! Economic benefit of charging from clipped PV in current time step to discharge sometime in the next X hours (clipped PV is free) ($/kWh) */