Skip to content

Commit

Permalink
heap: initialize buffers to 0 before use and check for NULL
Browse files Browse the repository at this point in the history
When memory from heap is used to store struct,
it is needed to initialize it with zero to avoid random values on
on elements not set explicitelly.
KRKNWK-18597

Signed-off-by: Robert Gałat <robert.galat@nordicsemi.no>
  • Loading branch information
RobertGalatNordic committed Mar 7, 2024
1 parent add8b64 commit 32f9a96
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 22 deletions.
6 changes: 5 additions & 1 deletion samples/sid_end_device/src/cli/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ static void on_sidewalk_status_changed(const struct sid_status *status, void *co
int err = 0;
uint32_t new_link_mask = status->detail.link_status_mask;
struct sid_status *new_status = sid_hal_malloc(sizeof(struct sid_status));
memcpy(new_status, status, sizeof(struct sid_status));
if (!new_status) {
LOG_ERR("Failed to allocate memory for new status value");
} else {
memcpy(new_status, status, sizeof(struct sid_status));
}
sidewalk_event_send(SID_EVENT_NEW_STATUS, new_status);

switch (status->state) {
Expand Down
10 changes: 10 additions & 0 deletions samples/sid_end_device/src/cli/app_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ static int cmd_sid_option(cli_event_t event, enum sid_option option, void *data,
if (!p_opt) {
return -ENOMEM;
}
memset(p_opt, 0x0, sizeof(*p_opt));
p_opt->option = option;
p_opt->data_len = len;
if (data) {
Expand Down Expand Up @@ -489,6 +490,10 @@ int cmd_sid_send(const struct shell *shell, int32_t argc, const char **argv)
}

sidewalk_msg_t *send = sid_hal_malloc(sizeof(sidewalk_msg_t));
if (!send) {
return -ENOMEM;
}
memset(send, 0x0, sizeof(*send));
memcpy(&send->msg, &msg, sizeof(struct sid_msg));
memcpy(&send->desc, &desc, sizeof(struct sid_msg_desc));

Expand Down Expand Up @@ -875,6 +880,11 @@ int cmd_sid_option_gc(const struct shell *shell, int32_t argc, const char **argv
return -EINVAL;
}
uint32_t *p_link_mask = sid_hal_malloc(sizeof(uint32_t));
if (!p_link_mask) {
return -ENOMEM;
}

memset(p_link_mask, 0x0, sizeof(*p_link_mask));
cli_parse_link_mask_opt((uint8_t)link_type, p_link_mask);

int err = cmd_sid_option_get_input_data(SID_OPTION_GET_LINK_POLICY_AUTO_CONNECT_PARAMS,
Expand Down
2 changes: 2 additions & 0 deletions samples/sid_end_device/src/file_transfer.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ static void on_transfer_request(const struct sid_bulk_data_transfer_request *con
transfer_response->reject_reason = SID_BULK_DATA_TRANSFER_REJECT_REASON_NO_SPACE;
return;
}
memset(ptr, 0x0, sizeof(transfer_request->minimum_scratch_buffer_size));

// accept all requests if only we have avaliable memory for scratch buffer
buffer_repo[repo_index].memory_slab_for_transfer = ptr;
Expand All @@ -82,6 +83,7 @@ static void on_data_received(const struct sid_bulk_data_transfer_desc *const des
LOG_ERR("Failed to allocate memory for received data descriptor");
return;
}
memset(args, 0x0, sizeof(*args));
args->desc = *desc;
args->buffer = (struct sid_bulk_data_transfer_buffer *)buffer;
args->context = context;
Expand Down
27 changes: 25 additions & 2 deletions samples/sid_end_device/src/hello/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,18 @@ static void on_sidewalk_msg_received(const struct sid_msg_desc *msg_desc, const
if (msg_desc->type == SID_MSG_TYPE_GET || msg_desc->type == SID_MSG_TYPE_SET) {
LOG_INF("Send echo message");
sidewalk_msg_t *echo = sid_hal_malloc(sizeof(sidewalk_msg_t));

if (!echo) {
LOG_ERR("Failed to allocate event context for echo message");
return;
}
memset(echo, 0x0, sizeof(*echo));
echo->msg.size = msg->size;
echo->msg.data = sid_hal_malloc(echo->msg.size);
if (!echo->msg.data) {
LOG_ERR("Failed to allocate memory for message echo data");
sid_hal_free(echo);
return;
}
memcpy(echo->msg.data, msg->data, echo->msg.size);

echo->desc.type = (msg_desc->type == SID_MSG_TYPE_GET) ? SID_MSG_TYPE_RESPONSE :
Expand Down Expand Up @@ -113,7 +122,11 @@ static void on_sidewalk_status_changed(const struct sid_status *status, void *co
int err = 0;
uint32_t new_link_mask = status->detail.link_status_mask;
struct sid_status *new_status = sid_hal_malloc(sizeof(struct sid_status));
memcpy(new_status, status, sizeof(struct sid_status));
if (!new_status) {
LOG_ERR("Failed to allocate memory for new status value");
} else {
memcpy(new_status, status, sizeof(struct sid_status));
}
sidewalk_event_send(SID_EVENT_NEW_STATUS, new_status);

switch (status->state) {
Expand Down Expand Up @@ -170,9 +183,19 @@ static void app_button_handler(uint32_t event)
LOG_INF("Send hello message");
const char payload[] = "hello";
sidewalk_msg_t *hello = sid_hal_malloc(sizeof(sidewalk_msg_t));
if (!hello) {
LOG_ERR("Failed to alloc memory for message context");
return;
}
memset(hello, 0x0, sizeof(*hello));

hello->msg.size = sizeof(payload);
hello->msg.data = sid_hal_malloc(hello->msg.size);
if (!hello->msg.data) {
sid_hal_free(hello);
LOG_ERR("Failed to allocate memory for message data");
return;
}
memcpy(hello->msg.data, payload, hello->msg.size);

hello->desc.type = SID_MSG_TYPE_NOTIFY;
Expand Down
6 changes: 5 additions & 1 deletion samples/sid_end_device/src/sensor_monitoring/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ static void on_sidewalk_factory_reset(void *context)
static void on_sidewalk_status_changed(const struct sid_status *status, void *context)
{
struct sid_status *new_status = sid_hal_malloc(sizeof(struct sid_status));
memcpy(new_status, status, sizeof(struct sid_status));
if (!new_status) {
LOG_ERR("Failed to allocate memory for new status value");
} else {
memcpy(new_status, status, sizeof(struct sid_status));
}
sidewalk_event_send(SID_EVENT_NEW_STATUS, new_status);

int err = 0;
Expand Down
10 changes: 10 additions & 0 deletions samples/sid_end_device/src/sensor_monitoring/app_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,18 @@ static int app_tx_demo_msg_send(struct sid_parse_state *state, uint8_t *buffer,

// Send sidewalk message
sidewalk_msg_t *sid_msg = sid_hal_malloc(sizeof(sidewalk_msg_t));
if (!sid_msg) {
LOG_ERR("Failed to alloc memory for message context");
return -ENOMEM;
}
memset(sid_msg, 0x0, sizeof(*sid_msg));
sid_msg->msg.size = state->offset;
sid_msg->msg.data = sid_hal_malloc(sid_msg->msg.size);
if (!sid_msg->msg.data) {
sid_hal_free(sid_msg);
LOG_ERR("Failed to allocate memory for message data");
return -ENOMEM;
}
memcpy(sid_msg->msg.data, msg_buffer, sid_msg->msg.size);
memcpy(&sid_msg->desc, sid_desc, sizeof(struct sid_msg_desc));

Expand Down
33 changes: 15 additions & 18 deletions samples/sid_end_device/src/sidewalk.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,22 @@ static void state_sidewalk_run(void *o)
sid_error_t e = SID_ERROR_NONE;

switch (sm->event.id) {
case SID_EVENT_SIDEWALK:
case SID_EVENT_SIDEWALK: {
e = sid_process(sm->sid->handle);
if (e) {
LOG_ERR("sid process err %d", (int)e);
}
break;
case SID_EVENT_FACTORY_RESET:
} break;
case SID_EVENT_FACTORY_RESET: {
#ifdef CONFIG_SID_END_DEVICE_PERSISTENT_LINK_MASK
(void)settings_utils_link_mask_set(0);
#endif /* CONFIG_SIDEWALK_PERSISTENT_LINK_MASK */
e = sid_set_factory_reset(sm->sid->handle);
if (e) {
LOG_ERR("sid factory reset err %d", (int)e);
}
break;
case SID_EVENT_LINK_SWITCH:
} break;
case SID_EVENT_LINK_SWITCH: {
static uint32_t new_link_mask = DEFAULT_LM;

switch (sm->sid->config.link_mask) {
Expand Down Expand Up @@ -250,8 +250,8 @@ static void state_sidewalk_run(void *o)
}
}
#endif /* CONFIG_SID_END_DEVICE_AUTO_CONN_REQ */
break;
case SID_EVENT_NORDIC_DFU:
} break;
case SID_EVENT_NORDIC_DFU: {
#ifdef CONFIG_SIDEWALK_FILE_TRANSFER
app_file_transfer_demo_deinit(sm->sid->handle);
#endif
Expand All @@ -260,8 +260,8 @@ static void state_sidewalk_run(void *o)
LOG_ERR("sid deinit err %d", (int)e);
}
smf_set_state(SMF_CTX(sm), &sid_states[STATE_DFU]);
break;
case SID_EVENT_NEW_STATUS:
} break;
case SID_EVENT_NEW_STATUS: {
struct sid_status *p_status = (struct sid_status *)sm->event.ctx;
if (!p_status) {
LOG_ERR("sid new status is NULL");
Expand All @@ -270,9 +270,8 @@ static void state_sidewalk_run(void *o)

memcpy(&sm->sid->last_status, p_status, sizeof(struct sid_status));
sid_hal_free(p_status);

break;
case SID_EVENT_SEND_MSG:
} break;
case SID_EVENT_SEND_MSG: {
sidewalk_msg_t *p_msg = (sidewalk_msg_t *)sm->event.ctx;
if (!p_msg) {
LOG_ERR("sid send msg is NULL");
Expand All @@ -286,9 +285,8 @@ static void state_sidewalk_run(void *o)
LOG_DBG("sid send (type: %d, id: %u)", (int)p_msg->desc.type, p_msg->desc.id);
sid_hal_free(p_msg->msg.data);
sid_hal_free(p_msg);

break;
case SID_EVENT_CONNECT:
} break;
case SID_EVENT_CONNECT: {
if (!(sm->sid->config.link_mask & SID_LINK_TYPE_1)) {
LOG_ERR("Can not request connection - BLE not enabled");
return;
Expand All @@ -297,7 +295,7 @@ static void state_sidewalk_run(void *o)
if (e) {
LOG_ERR("sid conn req err %d", (int)e);
}
break;
} break;
case SID_EVENT_FILE_TRANSFER: {
#ifdef CONFIG_SIDEWALK_FILE_TRANSFER
struct data_received_args *args = (struct data_received_args *)sm->event.ctx;
Expand Down Expand Up @@ -366,10 +364,9 @@ static void state_dfu_entry(void *o)
static void state_dfu_run(void *o)
{
sm_t *sm = (sm_t *)o;

int err = -ENOTSUP;
switch (sm->event.id) {
case SID_EVENT_NORDIC_DFU:
int err = -ENOTSUP;
#if defined(CONFIG_SIDEWALK_DFU_SERVICE_BLE)
err = nordic_dfu_ble_stop();
#endif
Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/sid_dut_shell/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,7 @@ void test_sid_set_option_ml(struct test_sid_set_option_params params)

void test_sid_set_option_gc(struct test_sid_set_option_params params)
{
sidewalk_parameters_set(params_sid_ok);
int ret = cmd_sid_option_gc(NULL, params.argc, params.argv);

verify_sid_option_asserts(params, ret);
Expand Down

0 comments on commit 32f9a96

Please sign in to comment.