From 0d609346a52baa3451c11171c03f74618b18e001 Mon Sep 17 00:00:00 2001 From: SpaceOceanCorp Date: Tue, 10 Nov 2026 12:15:14 -0600 Subject: [PATCH] Fix #535, keep the leading-edge chunk when the chunk list is full A full chunk list dropped a new chunk unless it was larger than the smallest tracked chunk. The first PDU after each new gap is a single PDU, so it and every later contiguous PDU went untracked and were NAKed again. Keep a chunk that lies past every tracked chunk, evicting the smallest. --- fsw/src/cf_chunk.c | 8 +++++++- unit-test/cf_chunk_tests.c | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/fsw/src/cf_chunk.c b/fsw/src/cf_chunk.c index ffd3172b..3e33af51 100644 --- a/fsw/src/cf_chunk.c +++ b/fsw/src/cf_chunk.c @@ -263,7 +263,13 @@ void CF_Chunks_Insert(CF_ChunkList_t *chunks, CF_ChunkIdx_t i, const CF_Chunk_t { smallest_i = CF_Chunks_FindSmallestSize(chunks); smallest_c = &chunks->chunks[smallest_i]; - if (smallest_c->size < chunk->size) + + /* + * A chunk past every tracked chunk is the leading edge of a sequential + * transfer, and later data extends it. Keep it at the cost of the smallest + * tracked chunk; dropping it would leave all later data untracked. + */ + if ((smallest_c->size < chunk->size) || (i == chunks->count)) { CF_Chunks_EraseChunk(chunks, smallest_i); CF_Chunks_InsertChunk(chunks, CF_Chunks_FindInsertPosition(chunks, chunk), chunk); diff --git a/unit-test/cf_chunk_tests.c b/unit-test/cf_chunk_tests.c index 4a7b0a71..661698f3 100644 --- a/unit-test/cf_chunk_tests.c +++ b/unit-test/cf_chunk_tests.c @@ -163,6 +163,46 @@ void Test_CF_Chunk_CreateAddReset(void) UtAssert_UINT32_EQ(clist.count, 3); } +/* + * Cover a full list receiving a chunk past every tracked chunk that is smaller than + * the smallest tracked chunk (the first PDU after a new gap in a sequential transfer): + * it must replace the smallest chunk, and later contiguous data must extend it. + * Also confirm a small chunk inside the tracked range is still dropped. + */ +void Test_CF_Chunk_AddFullKeepsLeadingEdge(void) +{ + CF_ChunkList_t clist; + CF_Chunk_t chunks[3]; + + UtAssert_VOIDCALL(CF_ChunkListInit(&clist, sizeof(chunks) / sizeof(chunks[0]), chunks)); + UtAssert_VOIDCALL(CF_ChunkListAdd(&clist, 0, 100)); + UtAssert_VOIDCALL(CF_ChunkListAdd(&clist, 200, 50)); + UtAssert_VOIDCALL(CF_ChunkListAdd(&clist, 300, 100)); + UtAssert_UINT32_EQ(clist.count, 3); + + /* Small chunk inside the tracked range: dropped, list unchanged */ + UtAssert_VOIDCALL(CF_ChunkListAdd(&clist, 150, 10)); + UtAssert_UINT32_EQ(clist.count, 3); + UtAssert_UINT32_EQ(clist.chunks[1].offset, 200); + UtAssert_UINT32_EQ(clist.chunks[1].size, 50); + + /* Small chunk past every tracked chunk: replaces the smallest (200, 50) */ + UtAssert_VOIDCALL(CF_ChunkListAdd(&clist, 500, 10)); + UtAssert_UINT32_EQ(clist.count, 3); + UtAssert_UINT32_EQ(clist.chunks[0].offset, 0); + UtAssert_UINT32_EQ(clist.chunks[0].size, 100); + UtAssert_UINT32_EQ(clist.chunks[1].offset, 300); + UtAssert_UINT32_EQ(clist.chunks[1].size, 100); + UtAssert_UINT32_EQ(clist.chunks[2].offset, 500); + UtAssert_UINT32_EQ(clist.chunks[2].size, 10); + + /* Contiguous data extends the leading edge */ + UtAssert_VOIDCALL(CF_ChunkListAdd(&clist, 510, 10)); + UtAssert_UINT32_EQ(clist.count, 3); + UtAssert_UINT32_EQ(clist.chunks[2].offset, 500); + UtAssert_UINT32_EQ(clist.chunks[2].size, 20); +} + /* Cover add of an empty range, which covers no data and must not be stored */ void Test_CF_Chunk_AddEmpty(void) { @@ -384,6 +424,7 @@ void UtTest_Setup(void) { /* Full coverage with just this section of tests */ TEST_CF_ADD(Test_CF_Chunk_CreateAddReset); + TEST_CF_ADD(Test_CF_Chunk_AddFullKeepsLeadingEdge); TEST_CF_ADD(Test_CF_Chunk_AddEmpty); TEST_CF_ADD(Test_CF_Chunk_Combine); TEST_CF_ADD(Test_CF_Chunk_GetRmFirst);