Skip to content

Commit 2af8beb

Browse files
committed
catch null pointers in handlers and static casting of std::strings
1 parent 3907783 commit 2af8beb

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

handlers.h

+13-15
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "custom_types.h"
77
#include "helper_functions.h"
88

9-
SCSAPI_VOID handle_frame_start(const scs_event_t UNUSED(event), const void* const event_info, const scs_context_t context) {
9+
SCSAPI_VOID handle_frame_start(const scs_event_t UNUSED(event), const void* const UNUSED(event_info), const scs_context_t context) {
1010
telemetry_state* const telemetry = static_cast<telemetry_state*>(context);
1111

1212
std::stringstream state;
@@ -41,7 +41,7 @@ SCSAPI_VOID handle_pause_start(const scs_event_t event, const void* const UNUSED
4141
SCSAPI_VOID handle_position(const scs_string_t name, const scs_u32_t index, const scs_value_t* const value, const scs_context_t context) {
4242
telemetry_state* const telemetry = static_cast<telemetry_state*>(context);
4343

44-
if (!value) {
44+
if (value == NULL) {
4545
telemetry->position.avalible = false;
4646
return;
4747
}
@@ -53,23 +53,26 @@ SCSAPI_VOID handle_position(const scs_string_t name, const scs_u32_t index, cons
5353
point2d p{ (double)telemetry->position.x, (double)telemetry->position.z };
5454
point2d closest_city = telemetry->cities->nearest(p);
5555

56-
telemetry->position.closestCity = closest_city.name;
57-
telemetry->position.country = closest_city.country;
56+
telemetry->position.closestCity = static_cast<std::string>(closest_city.name);
57+
telemetry->position.country = static_cast<std::string>(closest_city.country);
5858
}
5959

6060
SCSAPI_VOID handle_speed(const scs_string_t name, const scs_u32_t index, const scs_value_t* const value, const scs_context_t context) {
61+
if (value == NULL) return;
6162
telemetry_state* const telemetry = static_cast<telemetry_state*>(context);
6263

6364
telemetry->speed = value->value_float.value;
6465
}
6566

6667
SCSAPI_VOID handle_distance(const scs_string_t name, const scs_u32_t index, const scs_value_t* const value, const scs_context_t context) {
68+
if (value == NULL) return;
6769
telemetry_state* const telemetry = static_cast<telemetry_state*>(context);
6870

6971
telemetry->distance = value->value_float.value;
7072
}
7173

7274
SCSAPI_VOID handle_trailer_connect(const scs_string_t name, const scs_u32_t index, const scs_value_t* const value, const scs_context_t context) {
75+
if (value == NULL) return;
7376
telemetry_state* const telemetry = static_cast<telemetry_state*>(context);
7477

7578
if (value) {
@@ -82,19 +85,17 @@ SCSAPI_VOID handle_trailer_connect(const scs_string_t name, const scs_u32_t inde
8285

8386
SCSAPI_VOID handle_configuration(const scs_event_t event, const void* const event_info, const scs_context_t context)
8487
{
88+
if (event_info == NULL) return;
8589
telemetry_state* const telemetry = static_cast<telemetry_state*>(context);
8690
const struct scs_telemetry_configuration_t* const info = static_cast<const scs_telemetry_configuration_t*>(event_info);
8791

8892
std::string id = info->id;
8993

9094
if (id.compare(SCS_TELEMETRY_CONFIG_job) == 0) {
91-
for (size_t i = 0; i < sizeof(info->attributes) / sizeof(scs_named_value_t); i++)
92-
{
93-
const scs_named_value_t* current = &info->attributes[i];
95+
for (const scs_named_value_t* current = info->attributes; current->name; ++current) {
9496
const std::string name = current->name;
95-
9697
if (name.compare(SCS_TELEMETRY_CONFIG_ATTRIBUTE_cargo) == 0) {
97-
telemetry->cargo.name = current->value.value_string.value;
98+
telemetry->cargo.name = static_cast<std::string>(current->value.value_string.value);
9899
}
99100
else if (name.compare(SCS_TELEMETRY_CONFIG_ATTRIBUTE_cargo_mass) == 0) {
100101
telemetry->cargo.weight = current->value.value_float.value;
@@ -105,16 +106,13 @@ SCSAPI_VOID handle_configuration(const scs_event_t event, const void* const even
105106
}
106107
}
107108
else if (id.compare(SCS_TELEMETRY_CONFIG_truck) == 0) {
108-
for (size_t i = 0; i < sizeof(info->attributes) / sizeof(scs_named_value_t); i++)
109-
{
110-
const scs_named_value_t* current = &info->attributes[i];
109+
for (const scs_named_value_t* current = info->attributes; current->name; ++current) {
111110
const std::string name = current->name;
112-
113111
if (name.compare(SCS_TELEMETRY_CONFIG_ATTRIBUTE_brand) == 0) {
114-
telemetry->truck.brand = current->value.value_string.value;
112+
telemetry->truck.brand = static_cast<std::string>(current->value.value_string.value);
115113
}
116114
else if (name.compare(SCS_TELEMETRY_CONFIG_ATTRIBUTE_name) == 0) {
117-
telemetry->truck.name = current->value.value_string.value;
115+
telemetry->truck.name = static_cast<std::string>(current->value.value_string.value);
118116
}
119117
}
120118
}

0 commit comments

Comments
 (0)