Skip to content

Commit

Permalink
Fix crush single message inside long message (#190)
Browse files Browse the repository at this point in the history
* Add test for single message inside long message

* Fix crush single message inside long message

Co-authored-by: p.kuskov <p.kuskov@geoscan.aero>
  • Loading branch information
a1ien and p.kuskov authored Mar 20, 2022
1 parent c9e482e commit b7e2be5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion canard.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,12 @@ int16_t canardHandleRxFrame(CanardInstance* ins, const CanardCANFrame* frame, ui
const bool first_frame = IS_START_OF_TRANSFER(tail_byte);
const bool not_previous_tid =
computeTransferIDForwardDistance((uint8_t) rx_state->transfer_id, TRANSFER_ID_FROM_TAIL_BYTE(tail_byte)) > 1;
const bool incomplete_frame = rx_state->buffer_blocks != NULL;

const bool need_restart =
(not_initialized) ||
(tid_timed_out) ||
(first_frame && not_previous_tid);
(first_frame && (not_previous_tid || incomplete_frame));

if (need_restart)
{
Expand Down
34 changes: 34 additions & 0 deletions tests/test_rxerr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,37 @@ TEST_CASE("canardHandleRxFrame OOM handling, Correctness")
err = canardHandleRxFrame(&canard, &frame, 1);
REQUIRE(-CANARD_ERROR_OUT_OF_MEMORY == err);
}

TEST_CASE("canardHandleRxFrame unusual single frame, Correctness")
{
uint8_t canard_memory_pool[1024];
CanardInstance canard;
CanardCANFrame frame;
int16_t err;

g_should_accept = true;

//Open canard to accept all transfers with a node ID of 20
canardInit(&canard, canard_memory_pool, sizeof(canard_memory_pool),
onTransferReceived, shouldAcceptTransfer, &canard);
canardSetLocalNodeID(&canard, 20);

frame.data[7] = CONSTRUCT_TAIL_BYTE(1, 0, 0, 1);
frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
frame.data_len = 8; //Data length MUST be full packet
err = canardHandleRxFrame(&canard, &frame, 1);
REQUIRE(CANARD_OK == err);


frame.data[7] = CONSTRUCT_TAIL_BYTE(0, 0, 1, 1);
frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
frame.data_len = 8; //Data length MUST be full packet
err = canardHandleRxFrame(&canard, &frame, 1);
REQUIRE(CANARD_OK == err);

frame.data[1] = CONSTRUCT_TAIL_BYTE(1, 1, 1, 1);
frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
frame.data_len = 2; //Data length MUST be full packet
err = canardHandleRxFrame(&canard, &frame, 1);
REQUIRE(CANARD_OK == err);
}

0 comments on commit b7e2be5

Please sign in to comment.