Skip to content

Commit

Permalink
udpardTxPop now accepts mutable item #sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
serges147 committed Nov 28, 2024
1 parent 01b14e7 commit 142590d
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 15 deletions.
9 changes: 2 additions & 7 deletions libudpard/udpard.c
Original file line number Diff line number Diff line change
Expand Up @@ -654,15 +654,10 @@ struct UdpardTxItem* udpardTxPeek(const struct UdpardTx* const self)
return out;
}

struct UdpardTxItem* udpardTxPop(struct UdpardTx* const self, const struct UdpardTxItem* const item)
struct UdpardTxItem* udpardTxPop(struct UdpardTx* const self, struct UdpardTxItem* const item)
{
struct UdpardTxItem* out = NULL;
if ((self != NULL) && (item != NULL))
{
// Intentional violation of MISRA: casting away const qualifier. This is considered safe because the API
// contract dictates that the pointer shall point to a mutable entity in RAM previously allocated by the
// memory manager. It is difficult to avoid this cast in this context.
out = (struct UdpardTxItem*) item; // NOSONAR casting away const qualifier.
// Paragraph 6.7.2.1.15 of the C standard says:
// A pointer to a structure object, suitably converted, points to its initial member, and vice versa.
// Note that the highest-priority frame is always a leaf node in the AVL tree, which means that it is very
Expand All @@ -671,7 +666,7 @@ struct UdpardTxItem* udpardTxPop(struct UdpardTx* const self, const struct Udpar
UDPARD_ASSERT(self->queue_size > 0U);
self->queue_size--;
}
return out;
return item;
}

void udpardTxFree(const struct UdpardTxMemoryResources memory, struct UdpardTxItem* const item)
Expand Down
2 changes: 1 addition & 1 deletion libudpard/udpard.h
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ struct UdpardTxItem* udpardTxPeek(const struct UdpardTx* const self);
/// If any of the arguments are NULL, the function has no effect and returns NULL.
///
/// The time complexity is logarithmic of the queue size. This function does not invoke the dynamic memory manager.
struct UdpardTxItem* udpardTxPop(struct UdpardTx* const self, const struct UdpardTxItem* const item);
struct UdpardTxItem* udpardTxPop(struct UdpardTx* const self, struct UdpardTxItem* const item);

/// This is a simple helper that frees the memory allocated for the item and its payload,
/// using the correct sizes and memory resources.
Expand Down
4 changes: 2 additions & 2 deletions tests/src/test_e2e.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void testPubSub()
TEST_ASSERT_EQUAL(0, alloc_rx_session.allocated_fragments);
TEST_ASSERT_EQUAL(0, alloc_rx_fragment.allocated_fragments);
TEST_ASSERT_EQUAL(0, alloc_rx_payload.allocated_fragments);
const UdpardTxItem* tx_item = udpardTxPeek(&tx);
UdpardTxItem* tx_item = udpardTxPeek(&tx);
TEST_ASSERT_NOT_NULL(tx_item);
TEST_ASSERT_EQUAL(sub.at(1).udp_ip_endpoint.ip_address, tx_item->destination.ip_address);
TEST_ASSERT_NULL(tx_item->next_in_transfer);
Expand Down Expand Up @@ -478,7 +478,7 @@ void testRPC()
TEST_ASSERT_EQUAL(0, alloc_rx_session.allocated_fragments);
TEST_ASSERT_EQUAL(0, alloc_rx_fragment.allocated_fragments);
TEST_ASSERT_EQUAL(0, alloc_rx_payload.allocated_fragments);
const UdpardTxItem* tx_item = udpardTxPeek(&tx);
UdpardTxItem* tx_item = udpardTxPeek(&tx);
TEST_ASSERT_NOT_NULL(tx_item);
TEST_ASSERT_EQUAL(udp_ip_endpoint.ip_address, tx_item->destination.ip_address);
TEST_ASSERT_NULL(tx_item->next_in_transfer);
Expand Down
4 changes: 2 additions & 2 deletions tests/src/test_intrusive_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ static void testPushPeekPopFree(void)
alloc.allocated_bytes);
TEST_ASSERT_EQUAL(3, tx.queue_size);

const UdpardTxItem* frame = udpardTxPeek(&tx);
UdpardTxItem* frame = udpardTxPeek(&tx);
TEST_ASSERT_NOT_EQUAL(NULL, frame);
TEST_ASSERT_NOT_EQUAL(NULL, frame->next_in_transfer);
TEST_ASSERT_EQUAL(1234567890U, frame->deadline_usec);
Expand Down Expand Up @@ -722,7 +722,7 @@ static void testPushPrioritization(void)
NULL));
TEST_ASSERT_EQUAL(3 * 2ULL, alloc.allocated_fragments);
TEST_ASSERT_EQUAL(3, tx.queue_size);
const UdpardTxItem* frame = udpardTxPeek(&tx);
UdpardTxItem* frame = udpardTxPeek(&tx);
TEST_ASSERT_NOT_EQUAL(NULL, frame);
TEST_ASSERT_EQUAL(0xAAAAAAAA, frame->destination.ip_address);

Expand Down
6 changes: 3 additions & 3 deletions tests/src/test_tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void testPublish()
&user_transfer_referent));
TEST_ASSERT_EQUAL(1 * 2ULL, alloc.allocated_fragments);
TEST_ASSERT_EQUAL(1, tx.queue_size);
const auto* frame = udpardTxPeek(&tx);
auto* frame = udpardTxPeek(&tx);
std::cout << hexdump::hexdump(frame->datagram_payload.data, frame->datagram_payload.size) << "\n\n";
TEST_ASSERT_NOT_EQUAL(nullptr, frame);
TEST_ASSERT_EQUAL(nullptr, frame->next_in_transfer);
Expand Down Expand Up @@ -249,7 +249,7 @@ void testRequest()
&user_transfer_referent));
TEST_ASSERT_EQUAL(1 * 2ULL, alloc.allocated_fragments);
TEST_ASSERT_EQUAL(1, tx.queue_size);
const auto* frame = udpardTxPeek(&tx);
auto* frame = udpardTxPeek(&tx);
std::cout << hexdump::hexdump(frame->datagram_payload.data, frame->datagram_payload.size) << "\n\n";
TEST_ASSERT_NOT_EQUAL(nullptr, frame);
TEST_ASSERT_EQUAL(nullptr, frame->next_in_transfer);
Expand Down Expand Up @@ -401,7 +401,7 @@ void testRespond()
&user_transfer_referent));
TEST_ASSERT_EQUAL(1 * 2ULL, alloc.allocated_fragments);
TEST_ASSERT_EQUAL(1, tx.queue_size);
const auto* frame = udpardTxPeek(&tx);
auto* frame = udpardTxPeek(&tx);
std::cout << hexdump::hexdump(frame->datagram_payload.data, frame->datagram_payload.size) << "\n\n";
TEST_ASSERT_NOT_EQUAL(nullptr, frame);
TEST_ASSERT_EQUAL(nullptr, frame->next_in_transfer);
Expand Down

0 comments on commit 142590d

Please sign in to comment.