Skip to content

Commit

Permalink
DatabaseHandler: Fix empty check
Browse files Browse the repository at this point in the history
In `transaction_metervalues_insert ()`, we were accessing the
first element of a list, then checking whether or not it was
empty. This causes an exception since if this list *is* empty,
there's no element to access.

This commit shifts the code around so that the empty check occurs
before accessing any elements of the list.

Co-authored-by: Gianfranco Berardi <[email protected]>
Signed-off-by: Christopher Davis <[email protected]>
  • Loading branch information
christopher-davis-afs and gberardi-pillar committed Feb 26, 2024
1 parent 1725770 commit d9536ad
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/ocpp/v201/database_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,12 @@ int32_t DatabaseHandler::get_local_authorization_list_number_of_entries() {
}

bool DatabaseHandler::transaction_metervalues_insert(const std::string& transaction_id, const MeterValue& meter_value) {
if (meter_value.sampledValue.empty()) {
return false;
}

auto sampled_value_context = meter_value.sampledValue.at(0).context;
if (meter_value.sampledValue.empty() or !sampled_value_context.has_value()) {
if (!sampled_value_context.has_value()) {
return false;
}

Expand Down

0 comments on commit d9536ad

Please sign in to comment.