drivers: CAN: Limit the DLC to 8

This commit limits the data length code to eight.
DLC > 8 returns a newly introduced CAN_TX_EINVAL error code.

Signed-off-by: Alexander Wachter <alexander.wachter@student.tugraz.at>
This commit is contained in:
Alexander Wachter 2019-11-06 10:03:20 +01:00 committed by Kumar Gala
commit d558fd055a
6 changed files with 40 additions and 2 deletions

View file

@ -56,6 +56,11 @@ int can_loopback_send(struct device *dev, const struct zcan_frame *frame,
"standard" : "extended" "standard" : "extended"
, frame->rtr == CAN_DATAFRAME ? "no" : "yes"); , frame->rtr == CAN_DATAFRAME ? "no" : "yes");
if (frame->dlc > CAN_MAX_DLC) {
LOG_ERR("DLC of %d exceeds maximum (%d)", frame->dlc, CAN_MAX_DLC);
return CAN_TX_EINVAL;
}
if (!data->loopback) { if (!data->loopback) {
return 0; return 0;

View file

@ -388,6 +388,11 @@ static int mcp2515_send(struct device *dev, const struct zcan_frame *msg,
u8_t nnn; u8_t nnn;
u8_t tx_frame[MCP2515_FRAME_LEN]; u8_t tx_frame[MCP2515_FRAME_LEN];
if (msg->dlc > CAN_MAX_DLC) {
LOG_ERR("DLC of %d exceeds maximum (%d)", msg->dlc, CAN_MAX_DLC);
return CAN_TX_EINVAL;
}
if (k_sem_take(&dev_data->tx_sem, timeout) != 0) { if (k_sem_take(&dev_data->tx_sem, timeout) != 0) {
return CAN_TIMEOUT; return CAN_TIMEOUT;
} }

View file

@ -296,6 +296,11 @@ static int mcux_flexcan_send(struct device *dev, const struct zcan_frame *msg,
status_t status; status_t status;
int alloc; int alloc;
if (msg->dlc > CAN_MAX_DLC) {
LOG_ERR("DLC of %d exceeds maximum (%d)", msg->dlc, CAN_MAX_DLC);
return CAN_TX_EINVAL;
}
while (true) { while (true) {
alloc = mcux_get_tx_alloc(data); alloc = mcux_get_tx_alloc(data);
if (alloc >= 0) { if (alloc >= 0) {

View file

@ -548,7 +548,11 @@ int can_stm32_send(struct device *dev, const struct zcan_frame *msg,
, msg->rtr == CAN_DATAFRAME ? "no" : "yes"); , msg->rtr == CAN_DATAFRAME ? "no" : "yes");
__ASSERT(msg->dlc == 0U || msg->data != NULL, "Dataptr is null"); __ASSERT(msg->dlc == 0U || msg->data != NULL, "Dataptr is null");
__ASSERT(msg->dlc <= CAN_MAX_DLC, "DLC > 8");
if (msg->dlc > CAN_MAX_DLC) {
LOG_ERR("DLC of %d exceeds maximum (%d)", msg->dlc, CAN_MAX_DLC);
return CAN_TX_EINVAL;
}
if (can->ESR & CAN_ESR_BOFF) { if (can->ESR & CAN_ESR_BOFF) {
return CAN_TX_BUS_OFF; return CAN_TX_BUS_OFF;

View file

@ -47,6 +47,9 @@ extern "C" {
/** unexpected error */ /** unexpected error */
#define CAN_TX_UNKNOWN (-5) #define CAN_TX_UNKNOWN (-5)
/** invalid parameter */
#define CAN_TX_EINVAL (-22)
/** attach_* failed because there is no unused filter left*/ /** attach_* failed because there is no unused filter left*/
#define CAN_NO_FREE_FILTER (-1) #define CAN_NO_FREE_FILTER (-1)

View file

@ -593,6 +593,21 @@ static void test_send_receive_wrong_id(void)
can_detach(can_dev, filter_id); can_detach(can_dev, filter_id);
} }
/*
* Check if a call with dlc > CAN_MAX_DLC returns CAN_TX_EINVAL
*/
static void test_send_invalid_dlc(void)
{
struct zcan_frame frame;
int ret;
frame.dlc = CAN_MAX_DLC + 1;
ret = can_send(can_dev, &frame, TEST_SEND_TIMEOUT, tx_std_isr, NULL);
zassert_equal(ret, CAN_TX_EINVAL,
"ret [%d] not equal to %d", ret, CAN_TX_EINVAL);
}
void test_main(void) void test_main(void)
{ {
k_sem_init(&rx_isr_sem, 0, 1); k_sem_init(&rx_isr_sem, 0, 1);
@ -612,6 +627,7 @@ void test_main(void)
ztest_unit_test(test_send_receive_std_masked), ztest_unit_test(test_send_receive_std_masked),
ztest_unit_test(test_send_receive_ext_masked), ztest_unit_test(test_send_receive_ext_masked),
ztest_unit_test(test_send_receive_buffer), ztest_unit_test(test_send_receive_buffer),
ztest_unit_test(test_send_receive_wrong_id)); ztest_unit_test(test_send_receive_wrong_id),
ztest_unit_test(test_send_invalid_dlc));
ztest_run_test_suite(can_driver); ztest_run_test_suite(can_driver);
} }