Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions libcanard/canard.c
Original file line number Diff line number Diff line change
Expand Up @@ -1382,9 +1382,11 @@ int8_t canardRxSubscribe(struct CanardInstance* const ins,
const CanardMicrosecond transfer_id_timeout_usec,
struct CanardRxSubscription* const out_subscription)
{
int8_t out = -CANARD_ERROR_INVALID_ARGUMENT;
const size_t tk = (size_t) transfer_kind;
if ((ins != NULL) && (out_subscription != NULL) && (tk < CANARD_NUM_TRANSFER_KINDS))
int8_t out = -CANARD_ERROR_INVALID_ARGUMENT;
const size_t tk = (size_t) transfer_kind;
const bool port_id_ok = ((transfer_kind == CanardTransferKindMessage) && (port_id <= CANARD_SUBJECT_ID_MAX)) ||
(port_id <= CANARD_SERVICE_ID_MAX);
if ((ins != NULL) && (out_subscription != NULL) && (tk < CANARD_NUM_TRANSFER_KINDS) && port_id_ok)
{
// Reset to the initial state. This is absolutely critical because the new payload size limit may be larger
// than the old value; if there are any payload buffers allocated, we may overrun them because they are shorter
Expand Down
4 changes: 4 additions & 0 deletions tests/test_public_rx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ TEST_CASE("RxSubscriptionErrors")
REQUIRE(-CANARD_ERROR_INVALID_ARGUMENT == canardRxSubscribe(&ins.getInstance(), kind.value, 0, 0, 0, &sub));
REQUIRE(-CANARD_ERROR_INVALID_ARGUMENT ==
canardRxSubscribe(&ins.getInstance(), CanardTransferKindMessage, 0, 0, 0, nullptr));
REQUIRE(-CANARD_ERROR_INVALID_ARGUMENT ==
canardRxSubscribe(&ins.getInstance(), CanardTransferKindMessage, 8192, 0, 0, &sub));
REQUIRE(-CANARD_ERROR_INVALID_ARGUMENT ==
canardRxSubscribe(&ins.getInstance(), CanardTransferKindResponse, 512, 0, 0, &sub));

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