Skip to content

Commit a421943

Browse files
authored
check port id range (#251)
closes #221
1 parent 0fd1d3a commit a421943

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

libcanard/canard.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,9 +1382,11 @@ int8_t canardRxSubscribe(struct CanardInstance* const ins,
13821382
const CanardMicrosecond transfer_id_timeout_usec,
13831383
struct CanardRxSubscription* const out_subscription)
13841384
{
1385-
int8_t out = -CANARD_ERROR_INVALID_ARGUMENT;
1386-
const size_t tk = (size_t) transfer_kind;
1387-
if ((ins != NULL) && (out_subscription != NULL) && (tk < CANARD_NUM_TRANSFER_KINDS))
1385+
int8_t out = -CANARD_ERROR_INVALID_ARGUMENT;
1386+
const size_t tk = (size_t) transfer_kind;
1387+
const bool port_id_ok = ((transfer_kind == CanardTransferKindMessage) && (port_id <= CANARD_SUBJECT_ID_MAX)) ||
1388+
(port_id <= CANARD_SERVICE_ID_MAX);
1389+
if ((ins != NULL) && (out_subscription != NULL) && (tk < CANARD_NUM_TRANSFER_KINDS) && port_id_ok)
13881390
{
13891391
// Reset to the initial state. This is absolutely critical because the new payload size limit may be larger
13901392
// than the old value; if there are any payload buffers allocated, we may overrun them because they are shorter

tests/test_public_rx.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ TEST_CASE("RxSubscriptionErrors")
369369
REQUIRE(-CANARD_ERROR_INVALID_ARGUMENT == canardRxSubscribe(&ins.getInstance(), kind.value, 0, 0, 0, &sub));
370370
REQUIRE(-CANARD_ERROR_INVALID_ARGUMENT ==
371371
canardRxSubscribe(&ins.getInstance(), CanardTransferKindMessage, 0, 0, 0, nullptr));
372+
REQUIRE(-CANARD_ERROR_INVALID_ARGUMENT ==
373+
canardRxSubscribe(&ins.getInstance(), CanardTransferKindMessage, 8192, 0, 0, &sub));
374+
REQUIRE(-CANARD_ERROR_INVALID_ARGUMENT ==
375+
canardRxSubscribe(&ins.getInstance(), CanardTransferKindResponse, 512, 0, 0, &sub));
372376

373377
REQUIRE(-CANARD_ERROR_INVALID_ARGUMENT == canardRxUnsubscribe(nullptr, CanardTransferKindMessage, 0));
374378
REQUIRE(-CANARD_ERROR_INVALID_ARGUMENT == canardRxUnsubscribe(&ins.getInstance(), kind.value, 0));

0 commit comments

Comments
 (0)