Skip to content

Conversation

edsiper
Copy link
Member

@edsiper edsiper commented Sep 21, 2025


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.

Summary by CodeRabbit

  • Bug Fixes
    • Corrects output size reporting when packing JSON with stateful/streaming parsing, ensuring buffer sizes are accurately reflected.
    • Improves handling of incremental reads to prevent incorrect or zero-length output size updates.
    • Enhances stability of the JSON packing path for the JSMN-based backend under stateful operation.

@edsiper edsiper added this to the Fluent Bit v4.1 milestone Sep 21, 2025
Copy link

coderabbitai bot commented Sep 21, 2025

Walkthrough

Adds variables to capture return value and state-derived output size when using the JSMN backend with an existing state, conditionally updating the caller-provided out_size only on success, and returning the captured status. Resets state_out_size before incremental read handling.

Changes

Cohort / File(s) Summary
JSON packing control flow
src/flb_pack_json.c
Replaces direct return from flb_pack_json_state with captured ret and local state_out_size; on success, assigns computed size to caller’s out_size; returns ret. Initializes and later resets state_out_size to 0.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Caller
  participant Pack as flb_pack_json.c
  participant State as flb_pack_json_state

  Caller->>Pack: flb_pack_json(json, len, out_buf, out_size, state)
  alt Backend = JSMN and state exists
    Pack->>State: invoke(json, len, out_buf, state_out_size)
    State-->>Pack: ret, state_out_size
    alt ret == 0
      Note over Pack: Update *out_size = state_out_size
    else ret != 0
      Note over Pack: Do not modify out_size
    end
    Pack-->>Caller: return ret
  else Other paths
    Pack-->>Caller: existing behavior
  end

  rect rgba(230, 246, 255, 0.5)
  Note over Pack: Reset state_out_size = 0 before incremental read handling
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I nudge the bytes with cautious cheer,
A hop, a check: is ret now clear?
If green, I tally sizes right,
If not, I keep my paws polite.
JSMN whispers, “All is set”—
I thump and safely pass the bet. 🐇

Pre-merge checks and finishing touches

❌ 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%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "pack_json: fix data type assignation" is concise, names the affected component (pack_json) and indicates a fix related to assignment/data type; this aligns with the changes in src/flb_pack_json.c that adjust how state_out_size and out_size are computed and assigned, so it accurately summarizes the main change.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch pack-json-data-type

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

@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: 0

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

30-33: Narrow variable scope and drop redundant zeroing.

ret and state_out_size are only used in the JSMN+state branch. Limit their scope and remove the extra assignment.

-    int ret;
     int backend;
-    int state_out_size = 0;
     struct flb_pack_state *state = NULL;
@@
-        if (state) {
-            state_out_size = 0;
+        if (state) {
+            int ret;
+            int state_out_size = 0;

Also applies to: 50-50

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1946ade and 15def32.

📒 Files selected for processing (1)
  • src/flb_pack_json.c (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/flb_pack_json.c (1)
src/flb_pack.c (1)
  • flb_pack_json_state (613-684)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (29)
  • GitHub Check: pr-windows-build / call-build-windows-package (Windows 64bit, x64, x64-windows-static, 3.31.6)
  • GitHub Check: pr-windows-build / call-build-windows-package (Windows 32bit, x86, x86-windows-static, 3.31.6)
  • GitHub Check: pr-windows-build / call-build-windows-package (Windows 64bit (Arm64), amd64_arm64, -DCMAKE_SYSTEM_NAME=Windows -DCMA...
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_COVERAGE=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SIMD=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SANITIZE_MEMORY=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SANITIZE_MEMORY=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SIMD=Off, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SIMD=Off, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SANITIZE_THREAD=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SANITIZE_THREAD=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_ARROW=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DSANITIZE_ADDRESS=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SIMD=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DSANITIZE_UNDEFINED=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DSANITIZE_UNDEFINED=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_JEMALLOC=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_JEMALLOC=Off, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_JEMALLOC=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SMALL=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SMALL=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DSANITIZE_ADDRESS=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_JEMALLOC=Off, 3.31.6, gcc, g++)
  • GitHub Check: pr-compile-system-libs (-DFLB_PREFER_SYSTEM_LIBS=On, 3.31.6, gcc, g++, ubuntu-22.04, clang-12)
  • GitHub Check: PR - fuzzing test
  • GitHub Check: pr-compile-system-libs (-DFLB_PREFER_SYSTEM_LIBS=On, 3.31.6, gcc, g++, ubuntu-24.04, clang-14)
  • GitHub Check: pr-compile-system-libs (-DFLB_PREFER_SYSTEM_LIBS=On, 3.31.6, clang, clang++, ubuntu-24.04, clang-14)
  • GitHub Check: pr-compile-system-libs (-DFLB_PREFER_SYSTEM_LIBS=On, 3.31.6, clang, clang++, ubuntu-22.04, clang-12)
  • GitHub Check: pr-compile-centos-7
🔇 Additional comments (1)
src/flb_pack_json.c (1)

57-64: Good fix: remove size_t/int pointer mismatch and gate out_size updates on success.

Capturing into a local int and assigning to size_t only on success avoids UB on 64‑bit and prevents stale values from leaking through. rg shows many callers still pass &out_size — verify those callers declare an int (not size_t). Run this to locate callers that may be passing a size_t address:

#!/bin/bash
set -euo pipefail
rg -nP '\bflb_pack_json_state\s*\(' -n --hidden | while IFS=: read -r file lineno _; do
  start=$((lineno-40)); [ $start -lt 1 ] && start=1
  echo "== $file:$lineno =="
  sed -n "${start},$((lineno+3))p" "$file"
  sed -n "${start},$((lineno))p" "$file" | rg -nP --color=never 'size_t\s+\w+' || true
done

@edsiper edsiper merged commit 386e373 into master Sep 21, 2025
55 of 56 checks passed
@edsiper edsiper deleted the pack-json-data-type branch September 21, 2025 22:33
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.

1 participant