Skip to content

list: fix back link in cfl_list_add_after() - #97

Open
edsiper wants to merge 2 commits into
masterfrom
list-fix-add-after-back-link
Open

edsiper wants to merge 2 commits into
masterfrom
list-fix-add-after-back-link

Conversation

@edsiper

@edsiper edsiper commented Oct 15, 2026

Copy link
Copy Markdown
Member

Problem

cfl_list_add_after() sets next->prev = prev instead of next->prev = _new. Forward iteration still works, but the back link of the entry after the inserted one stays on the anchor.

When the anchor is the tail, head->prev keeps pointing at the old tail. A later cfl_list_add() tail insert then links the new entry after the old tail and drops the entry that cfl_list_add_after() inserted. Reverse iteration and cfl_list_entry_last() are also wrong.

Fluent Bit hits this when restoring a removed kvlist pair after the last key, if a processor inserts at the tail afterwards. processor_labels also calls cfl_list_add_after().

Fix

Point next->prev at the inserted entry, matching cfl_list_add_before() and __cfl_list_add().

Tests

tests/list.c gets an add_after case: it inserts in the middle and after the tail, then checks the back links, a following cfl_list_add(), and forward and reverse iteration. The case fails without the fix and passes with it.

cmake -S . -B build -DCFL_TESTS=On && cmake --build build -j8
ctest --test-dir build -R cfl-test-list --output-on-failure   # fails before the fix, passes after
BUILD_DIR=build scripts/agent-verify.sh                       # 34/34 passed
valgrind --leak-check=full --error-exitcode=9 build/tests/cfl-test-list   # clean
git diff --check

Compatibility

No API or ABI change. Only the broken back link changes; callers that relied on forward iteration see the same order.

cfl_list_add_after() set next->prev to the anchor entry instead of the
inserted one. Forward iteration still worked, but backward links were
broken. When inserting after the tail, head->prev stayed on the old tail,
so a later cfl_list_add() tail insert dropped the new entry from the
list.

Signed-off-by: Eduardo Silva <eduardo@calyptia.com>
Insert in the middle and after the tail, then verify the back links,
a following tail insert, and forward and reverse iteration.

Signed-off-by: Eduardo Silva <eduardo@calyptia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant