Skip to content

kv: add flb_kv_get_all_key_values function for mk_list#11777

Open
iypetrov wants to merge 4 commits intofluent:masterfrom
iypetrov:add-option-to-get-all-items-from-flb-kv-list
Open

kv: add flb_kv_get_all_key_values function for mk_list#11777
iypetrov wants to merge 4 commits intofluent:masterfrom
iypetrov:add-option-to-get-all-items-from-flb-kv-list

Conversation

@iypetrov
Copy link
Copy Markdown

@iypetrov iypetrov commented May 5, 2026

Add flb_kv_get_all_key_values function for mk_list

Addresses #11776 (1/3 sub-task)


Enter [N/A] in the box, if an item is not applicable to your change.

Testing
Before we can approve your change; please submit the following in a comment:

  • Example configuration file for the change
  • Debug log output from testing the change
  • Attached Valgrind output that shows no leaks or memory corruption was found

If this is a change to packaging of containers or native binaries then please confirm it works for all targets.

  • Run local packaging test showing all targets (including any new ones) build.
  • Set ok-package-test label to test for all targets (requires maintainer to do).

Documentation

  • Documentation required for this feature

Backporting

  • Backport to latest stable release.

Fluent Bit is licensed under Apache 2.0, by submitting this pull request I understand that this code will be released under the terms of that license.


<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

* **New Features**
  * Introduced a new public API function that retrieves multiple key-value pairs in bulk from existing lists. The function includes proper validation for null inputs and handles memory allocation appropriately.

* **Tests**
  * Added comprehensive test coverage for the bulk key-value retrieval functionality, validating correct retrieval of pair data and appropriate behavior with empty lists.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 5, 2026

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Added a public bulk-retrieval API flb_kv_get_all_key_values(struct mk_list *list) with implementation that returns an allocated struct flb_kv_pair ** array or NULL for NULL/empty lists or allocation failures. Added unit test covering empty and populated-list behavior.

Changes

KV Bulk Retrieval

Layer / File(s) Summary
API Declaration
include/fluent-bit/flb_kv.h
Added prototype struct flb_kv_pair **flb_kv_get_all_key_values(struct mk_list *list);.
Core Implementation
src/flb_kv.c
Implemented flb_kv_get_all_key_values() — returns NULL for NULL/empty list, allocates array sized by mk_list_size(list), iterates list to populate array, returns NULL if allocation fails.
Tests
tests/internal/kv.c
Added test_kv_get_all_key_values() that asserts NULL for empty list, inserts three pairs, verifies returned array is non-NULL and entries match insertion order, frees resources; test registered in TEST_LIST.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • fujimotos

Poem

🐇 I hopped through lists with nimble paws,
I gathered keys without a pause.
One call returns them, neat and tight,
An ordered array — what a sight!
Tests thumped their feet and gave applause.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: adding a new function flb_kv_get_all_key_values to the kv module for working with mk_list structures.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 15a81e8953

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/flb_kv.c Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
include/fluent-bit/flb_kv.h (1)

49-49: ⚡ Quick win

Clarify memory ownership/lifetime for flb_kv_get_all_key_values().

The declaration does not define who owns the returned array/elements or whether entries remain valid after flb_kv_release()/list mutation. Please document the contract (and preferably add a matching destroy helper) to prevent leaks/UAF in callers.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@include/fluent-bit/flb_kv.h` at line 49, Document the ownership/lifetime
contract for flb_kv_get_all_key_values: specify whether the returned struct
flb_kv_pair ** array and its flb_kv_pair elements are heap-allocated and owned
by the caller, whether they remain valid after calling flb_kv_release() or
mutating the source mk_list, and whether the array is NULL-terminated or
accompanied by a count; then add a matching destroy helper (e.g.,
flb_kv_free_key_values or flb_kv_destroy_key_values) declaration and describe
its required use so callers know how to free the array and avoid leaks/UAFs —
reference the existing flb_kv_get_all_key_values and flb_kv_release symbols so
reviewers can locate changes.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/flb_kv.c`:
- Around line 181-191: The array arr is allocated as pointers to struct
flb_kv_pair but the code writes to arr[i]->key/val without allocating each
pointed-to struct; update the mk_list_foreach loop (and related logic that
returns arr) to allocate each element before use (e.g., arr[i] = flb_calloc(1,
sizeof(struct flb_kv_pair))) and then set arr[i]->key and arr[i]->val from the
mk_list_foreach entry (struct flb_kv *kv). On allocation failure, free any
previously allocated arr[j] and arr itself, call flb_errno(), and return NULL to
avoid leaks or crashes. Ensure i increments and the final returned array length
semantics remain unchanged.

---

Nitpick comments:
In `@include/fluent-bit/flb_kv.h`:
- Line 49: Document the ownership/lifetime contract for
flb_kv_get_all_key_values: specify whether the returned struct flb_kv_pair **
array and its flb_kv_pair elements are heap-allocated and owned by the caller,
whether they remain valid after calling flb_kv_release() or mutating the source
mk_list, and whether the array is NULL-terminated or accompanied by a count;
then add a matching destroy helper (e.g., flb_kv_free_key_values or
flb_kv_destroy_key_values) declaration and describe its required use so callers
know how to free the array and avoid leaks/UAFs — reference the existing
flb_kv_get_all_key_values and flb_kv_release symbols so reviewers can locate
changes.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 96fbec84-c045-48d9-a91b-cd0344e1cc7b

📥 Commits

Reviewing files that changed from the base of the PR and between 86ec64a and 15a81e8.

📒 Files selected for processing (3)
  • include/fluent-bit/flb_kv.h
  • src/flb_kv.c
  • tests/internal/kv.c

Comment thread src/flb_kv.c Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/flb_kv.c (1)

164-194: ⚡ Quick win

No count out-parameter or NULL sentinel leaves callers unable to bound the returned array.

The function returns a heap-allocated struct flb_kv ** whose length is not communicated to callers. They must independently call mk_list_size(list) again — a second traversal — just to iterate the result safely. The idiomatic fix in C is either:

  • Option A (preferred): Add a NULL sentinel by allocating count + 1 slots and leaving the last entry NULL (costs one extra pointer).
  • Option B: Add a size_t *out_count out-parameter.

Additionally, since arr[i] entries are direct pointers into list-owned flb_kv nodes, callers must flb_free(arr) only — freeing individual elements would corrupt the list. This contract should either be enforced structurally (e.g., a const-qualified element type) or documented in a comment.

✏️ Option A: NULL-terminate the array
-    arr = flb_calloc(count, sizeof(struct flb_kv *));
+    arr = flb_calloc(count + 1, sizeof(struct flb_kv *));
     if (!arr) {
         flb_errno();
         return NULL;
     }

     mk_list_foreach(head, list) {
         kv = mk_list_entry(head, struct flb_kv, _head);
         arr[i] = kv;
         i++;
     }
+    arr[count] = NULL;  /* sentinel */

     return arr;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/flb_kv.c` around lines 164 - 194, flb_kv_get_all_key_values currently
returns an unbounded array, forcing callers to re-check mk_list_size; change the
allocation to reserve one extra slot (use flb_calloc(count + 1, sizeof(struct
flb_kv *))) and set arr[count] = NULL so the returned array is NULL-terminated
(Option A), update the mk_list_foreach loop to fill arr[0..count-1] as before,
and add a brief comment above the function stating that the array elements are
direct pointers into the list and callers must only flb_free(arr) (not free
individual entries).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/flb_kv.c`:
- Line 181: The allocation uses the wrong type: change the flb_calloc call that
assigns to arr from using sizeof(struct flb_kv_pair *) to sizeof(struct flb_kv
*) so the allocated array element size matches the actual pointer type stored
(refer to the variable arr and the struct flb_kv type in this file); update the
sizeof expression accordingly wherever flb_kv_pair is still referenced.

---

Nitpick comments:
In `@src/flb_kv.c`:
- Around line 164-194: flb_kv_get_all_key_values currently returns an unbounded
array, forcing callers to re-check mk_list_size; change the allocation to
reserve one extra slot (use flb_calloc(count + 1, sizeof(struct flb_kv *))) and
set arr[count] = NULL so the returned array is NULL-terminated (Option A),
update the mk_list_foreach loop to fill arr[0..count-1] as before, and add a
brief comment above the function stating that the array elements are direct
pointers into the list and callers must only flb_free(arr) (not free individual
entries).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1aad2dd7-d4a9-4911-a92d-5f1f0ee4b084

📥 Commits

Reviewing files that changed from the base of the PR and between 15a81e8 and 3acf589.

📒 Files selected for processing (3)
  • include/fluent-bit/flb_kv.h
  • src/flb_kv.c
  • tests/internal/kv.c
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/internal/kv.c

Comment thread src/flb_kv.c Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/flb_kv.c (1)

164-194: ⚡ Quick win

Add a NULL sentinel to make the returned array self-describing.

The function returns an array of count elements, but the signature (struct flb_kv **) gives callers no way to determine its length. Callers must independently call mk_list_size(list) before they can safely iterate the array, which is fragile and non-obvious. A NULL sentinel is the idiomatic fix in C for this pattern.

♻️ Proposed fix — allocate count+1 and NULL-terminate
-    arr = flb_calloc(count, sizeof(struct flb_kv *));
+    arr = flb_calloc(count + 1, sizeof(struct flb_kv *));
     if (!arr) {
         flb_errno();
         return NULL;
     }

     mk_list_foreach(head, list) {
         kv = mk_list_entry(head, struct flb_kv, _head);
         arr[i] = kv;
         i++;
     }
+    arr[count] = NULL;

     return arr;

With this change callers can iterate until arr[n] == NULL without needing a separate mk_list_size() call, and the declaration in flb_kv.h need not change.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/flb_kv.c` around lines 164 - 194, The returned array from
flb_kv_get_all_key_values is not NULL-terminated, forcing callers to separately
call mk_list_size; fix this by allocating one extra slot and NULL-terminating
the array: use flb_calloc(count + 1, sizeof(struct flb_kv *)) instead of
flb_calloc(count, ...), remove the early return that yields NULL for count==0
(or return an allocated array with arr[0]=NULL), populate arr[0..count-1] as now
and set arr[count] = NULL before returning; update uses of arr accordingly but
do not change the function signature or flb_kv.h declaration.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/flb_kv.c`:
- Around line 164-194: The returned array from flb_kv_get_all_key_values is not
NULL-terminated, forcing callers to separately call mk_list_size; fix this by
allocating one extra slot and NULL-terminating the array: use flb_calloc(count +
1, sizeof(struct flb_kv *)) instead of flb_calloc(count, ...), remove the early
return that yields NULL for count==0 (or return an allocated array with
arr[0]=NULL), populate arr[0..count-1] as now and set arr[count] = NULL before
returning; update uses of arr accordingly but do not change the function
signature or flb_kv.h declaration.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0a78fac0-1e6d-4786-aa91-670991a58252

📥 Commits

Reviewing files that changed from the base of the PR and between 3acf589 and 290a52d.

📒 Files selected for processing (1)
  • src/flb_kv.c

@iypetrov
Copy link
Copy Markdown
Author

iypetrov commented May 5, 2026

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 290a52d251

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread tests/internal/kv.c
@iypetrov iypetrov changed the title Add flb_kv_get_all_key_values function for mk_list kv: add flb_kv_get_all_key_values function for mk_list May 6, 2026
@cosmo0920
Copy link
Copy Markdown
Contributor

cosmo0920 commented May 7, 2026

Firstly, we need to address the following commit linter errors:

ref: https://github.com/fluent/fluent-bit/actions/runs/25450708887/job/74789123392?pr=11777#step:6:13

❌ Commit e87d8a0ffa failed:
Subject prefix 'fix:' does not match files changed.
Expected one of: kv:, tests:

❌ Commit 290a52d251 failed:
Subject prefix 'fix:' does not match files changed.
Expected one of: kv:

❌ Commit 3acf58973c failed:
Commit subject too long (>80 chars): 'feat: remove new struct flb_kv_pair and use the established flb_kv so not to have to deal with extra allocations'

❌ Commit 15a81e8953 failed:
Commit subject too long (>80 chars): 'feat: add flb_kv_get_all_key_values function, so to be able with a single call to get the items from the mk_list kv'


Commit prefix validation failed.
Error: Process completed with exit code 1.

iypetrov added 3 commits May 7, 2026 14:33
…mk_list)

Signed-off-by: Ilia Petrov <ilia.yavorov.petrov@gmail.com>
Signed-off-by: Ilia Petrov <ilia.yavorov.petrov@gmail.com>
Signed-off-by: Ilia Petrov <ilia.yavorov.petrov@gmail.com>
@iypetrov iypetrov force-pushed the add-option-to-get-all-items-from-flb-kv-list branch from e87d8a0 to 8d4fe4a Compare May 7, 2026 11:35
Signed-off-by: Ilia Petrov <ilia.yavorov.petrov@gmail.com>
@iypetrov iypetrov force-pushed the add-option-to-get-all-items-from-flb-kv-list branch from 8d4fe4a to e1603f8 Compare May 7, 2026 11:37
@iypetrov
Copy link
Copy Markdown
Author

iypetrov commented May 7, 2026

Firstly, we need to address the following commit linter errors:

ref: https://github.com/fluent/fluent-bit/actions/runs/25450708887/job/74789123392?pr=11777#step:6:13

❌ Commit e87d8a0ffa failed:
Subject prefix 'fix:' does not match files changed.
Expected one of: kv:, tests:

❌ Commit 290a52d251 failed:
Subject prefix 'fix:' does not match files changed.
Expected one of: kv:

❌ Commit 3acf58973c failed:
Commit subject too long (>80 chars): 'feat: remove new struct flb_kv_pair and use the established flb_kv so not to have to deal with extra allocations'

❌ Commit 15a81e8953 failed:
Commit subject too long (>80 chars): 'feat: add flb_kv_get_all_key_values function, so to be able with a single call to get the items from the mk_list kv'


Commit prefix validation failed.
Error: Process completed with exit code 1.

Thanks for the remark, commit linter errors are already fixed:

❯ python .github/scripts/commit_prefix_check.py
✅ Commit prefix validation passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants