Skip to content

Commit

Permalink
Fixed CanardRxTransfer::allocated_size for anonymous frame
Browse files Browse the repository at this point in the history
  • Loading branch information
serges147 committed Nov 15, 2024
1 parent b857b90 commit 14e373a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion libcanard/canard.c
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ CANARD_PRIVATE int8_t rxAcceptFrame(CanardInstance* const ins,
out_transfer->timestamp_usec = frame->timestamp_usec;
out_transfer->payload_size = payload_size;
out_transfer->payload = payload;
out_transfer->allocated_size = subscription->extent;
out_transfer->allocated_size = payload_size;
// Clang-Tidy raises an error recommending the use of memcpy_s() instead.
// We ignore it because the safe functions are poorly supported; reliance on them may limit the portability.
(void) memcpy(payload, frame->payload, payload_size); // NOLINT
Expand Down
3 changes: 2 additions & 1 deletion libcanard/canard.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ typedef struct CanardRxTransfer
void* payload;

/// Size of the allocated payload buffer in bytes.
/// Normally equal to the extent specified in the subscription, but may be zero if the payload pointer is NULL.
/// Normally equal to the extent specified in the subscription, but could be less (equal to `payload_size`)
/// in case of single frame transfer, or even zero if the payload pointer is NULL.
size_t allocated_size;
} CanardRxTransfer;

Expand Down
4 changes: 2 additions & 2 deletions tests/test_public_rx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ TEST_CASE("RxAnonymous")
REQUIRE(transfer.metadata.port_id == 0b0110011001100);
REQUIRE(transfer.metadata.remote_node_id == CANARD_NODE_ID_UNSET);
REQUIRE(transfer.metadata.transfer_id == 0);
REQUIRE(transfer.payload_size == 6); // NOT truncated.
REQUIRE(transfer.allocated_size == 16);
REQUIRE(transfer.payload_size == 6); // NOT truncated.
REQUIRE(transfer.allocated_size == 6); // Less than extent b/c it's anonymous single frame transfer.
REQUIRE(0 == std::memcmp(transfer.payload, "\x01\x02\x03\x04\x05\x06", 0));
REQUIRE(ins.getAllocator().getNumAllocatedFragments() == 1); // The PAYLOAD BUFFER only! No session for anons.
REQUIRE(ins.getAllocator().getTotalAllocatedAmount() == 6); // Smaller allocation.
Expand Down

0 comments on commit 14e373a

Please sign in to comment.