Skip to content

Commit

Permalink
drivers: can: zephyr: Prevent potential null pointer dereference
Browse files Browse the repository at this point in the history
Rearranged the assignment of the interface name to occur after the null check on
the device pointer.

Although `device->name` wasn't necessarily accessed before the null check, it
could have been in certain scenarios, which posed a risk of null pointer
dereference.

The check is now performed first to ensure safe access.

Signed-off-by: Gaetan Perrot <[email protected]>
  • Loading branch information
moonlight83340 committed Oct 7, 2024
1 parent cc71d1a commit 191400b
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/drivers/can/can_zephyr.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,15 @@ int csp_can_open_and_add_interface(const struct device * device, const char * if
int ret;
k_tid_t rx_tid;
can_context_t * ctx = NULL;
const char * name = ifname ? ifname : device->name;
const char * name;

if (device == NULL) {
ret = CSP_ERR_INVAL;
goto end;
}

name = ifname ? ifname : device->name;

if (rx_thread_idx >= CONFIG_CSP_CAN_RX_THREAD_NUM) {
LOG_ERR("[%s] No more RX thread can be created. (MAX: %d) Please check CONFIG_CSP_CAN_RX_THREAD_NUM.",
name, CONFIG_CSP_CAN_RX_THREAD_NUM);
Expand Down

0 comments on commit 191400b

Please sign in to comment.