Skip to content

Commit

Permalink
fix(vt-client): prevent crashes in multiple places
Browse files Browse the repository at this point in the history
Also changed place of switch-case break points to align with rest of repo
  • Loading branch information
GwnDaan committed Jan 22, 2024
1 parent 689a544 commit c579cf5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
35 changes: 22 additions & 13 deletions isobus/src/isobus_virtual_terminal_client_state_tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,35 +70,44 @@ namespace isobus
{
if (message.has_valid_source_control_function() &&
message.is_destination(client) &&
message.is_parameter_group_number(CANLibParameterGroupNumber::VirtualTerminalToECU))
message.is_parameter_group_number(CANLibParameterGroupNumber::VirtualTerminalToECU) &&
message.get_data_length() >= 1)
{
std::uint8_t function = message.get_uint8_at(0);
switch (function)
{
case static_cast<std::uint8_t>(VirtualTerminalClient::Function::ChangeNumericValueCommand):
{
auto errorCode = message.get_uint8_at(3);
if (errorCode == 0)
if (message.get_data_length() == 8)
{
std::uint16_t objectId = message.get_uint16_at(1);
if (numericValueStates.find(objectId) != numericValueStates.end())
auto errorCode = message.get_uint8_at(3);
if (errorCode == 0)
{
std::uint32_t value = message.get_uint32_at(4);
numericValueStates[objectId] = value;
std::uint16_t objectId = message.get_uint16_at(1);
if (numericValueStates.find(objectId) != numericValueStates.end())
{
std::uint32_t value = message.get_uint32_at(4);
numericValueStates[objectId] = value;
}
}
}
break;
}
break;

case static_cast<std::uint8_t>(VirtualTerminalClient::Function::VTChangeNumericValueMessage):
{
std::uint16_t objectId = message.get_uint16_at(1);
if (numericValueStates.find(objectId) != numericValueStates.end())
if (message.get_data_length() == 8)
{
std::uint32_t value = message.get_uint32_at(4);
numericValueStates[objectId] = value;
std::uint16_t objectId = message.get_uint16_at(1);
if (numericValueStates.find(objectId) != numericValueStates.end())
{
std::uint32_t value = message.get_uint32_at(4);
numericValueStates[objectId] = value;
}
}
break;
}
break;

default:
break;
}
Expand Down
12 changes: 11 additions & 1 deletion isobus/src/isobus_virtual_terminal_client_update_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,25 @@
namespace isobus
{
VirtualTerminalClientUpdateHelper::VirtualTerminalClientUpdateHelper(std::shared_ptr<VirtualTerminalClient> client) :
VirtualTerminalClientStateTracker(client->get_internal_control_function()),
VirtualTerminalClientStateTracker(nullptr == client ? nullptr : client->get_internal_control_function()),
client(client)
{
if (nullptr == client)
{
CANStackLogger::error("[VTStateHelper] constructor: client is nullptr");
return;
}
numericValueChangeEventHandle = client->add_vt_change_numeric_value_event_listener(
std::bind(&VirtualTerminalClientUpdateHelper::process_numeric_value_change_event, this, std::placeholders::_1));
}

bool VirtualTerminalClientUpdateHelper::set_numeric_value(std::uint16_t object_id, std::uint32_t value)
{
if (nullptr == client)
{
CANStackLogger::error("[VTStateHelper] set_numeric_value: client is nullptr");
return false;
}
if (numericValueStates.find(object_id) == numericValueStates.end())
{
CANStackLogger::warn("[VTStateHelper] set_numeric_value: objectId %lu not tracked", object_id);
Expand Down

0 comments on commit c579cf5

Please sign in to comment.