Skip to content

GridOptimizedCharge: Missing bounds check in calculateAvailEnergy causes IndexOutOfBoundsException #3530

@cvabc

Description

@cvabc

Summary

Controller.Ess.GridOptimizedCharge throws IndexOutOfBoundsException when prediction data is insufficient. The code lacks input validation before accessing prediction arrays.

Error

[_cycle] WARN [ms.edge.core.cycle.CycleWorker] Error in Controller [ctrlGridOptimizedCharge0]. IndexOutOfBoundsException: toIndex = 5

Root Cause

In DelayCharge.calculateAvailEnergy() (line 562), subList() is called without checking array bounds:

if (endIndex > 0) {
    // No check: endIndex could exceed array length
    List<Integer> productionList = Arrays.asList(quarterHourlyProduction).subList(1, endIndex);

Additionally, line 579 accesses [0] without checking if array is empty:

var currentProduction = quarterHourlyProduction[0] == null ? 0 : quarterHourlyProduction[0];

Comparison: calculateTargetMinute() correctly includes bounds checking at line 509:

for (var i = 0; i < min(96 - predictionStartQuarterHourIndex, quarterHourlyProduction.length); i++)

Suggested Fix

Add bounds checking in calculateAvailEnergy():

if (quarterHourlyProduction.length == 0 || quarterHourlyConsumption.length == 0) {
    return 0; // or handle gracefully
}

int safeEndIndex = Math.min(endIndex, Math.min(quarterHourlyProduction.length, quarterHourlyConsumption.length));
if (safeEndIndex > 1) {
    List<Integer> productionList = Arrays.asList(quarterHourlyProduction).subList(1, safeEndIndex);
    // ...
}

Impact

Controller enters FAULT state every cycle when prediction data is insufficient, even though the core charging calculation (remainingCapacity / remainingTime) does not require prediction data.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions