Skip to content

Conversation

erikrk
Copy link

@erikrk erikrk commented Sep 1, 2025

  • use strtoul for priority since it is unsigned
  • check for overflow correctly by comparing against ULONG_MAX
  • fail if no digits were consumed or next character is not comma
  • calculate tv.tv_usec correctly in case tv.tv_sec 32-bit
  • remove redundant errno = 0 added in 3254b9a
  • remove erroneous time.h added with 72d9dc8

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.

Summary by CodeRabbit

  • Bug Fixes
    • More robust parsing of priority, sequence, and timestamp fields; rejects malformed lines and ensures delimiters are present to prevent misreads.
    • Fixed timestamp microsecond handling to prevent overflow and ensure accurate time conversion.
  • Refactor
    • Stronger input validation and use of wider integer handling for safer, more reliable processing.
  • Chores
    • Removed an unused header include to streamline the build.

Copy link

coderabbitai bot commented Sep 1, 2025

Walkthrough

Reworks kmsg input parsing to use unsigned conversions and stricter consumption/delimiter checks for priority, sequence, and timestamp; uses 64-bit-safe arithmetic for timestamp math; adds immediate error handling with fail path and buffer rollback; removes unused #include <time.h>; derives priority via FLB_KLOG_PRI(val).

Changes

Cohort / File(s) Summary
Kmsg parser: unsigned parsing & validation
plugins/in_kmsg/in_kmsg.c
Replace signed parsing with unsigned (strtoul/strtoull), add ERANGE/errno and consumption checks, enforce comma delimiters between fields, advance parsing pointers safely, compute tv_sec/tv_usec using 64-bit-safe division/modulo (KMSG_USEC_PER_SEC), convert tv_usec→nsec for flb_time_set, add fail path with ctx->buffer_id--, and remove unused #include <time.h>.

Sequence Diagram(s)

sequenceDiagram
  participant Kernel as Kernel log line
  participant Parser as in_kmsg.c Parser
  participant Buffer as Buffer/State

  Kernel->>Parser: Provide raw kmsg line
  Parser->>Parser: strtoul(priority) -> val
  alt parse error / no digits / ERANGE
    Parser-->>Buffer: goto fail (ctx->buffer_id--)
    Parser->>Kernel: discard line
  else valid priority
    Parser->>Parser: derive FLB_KLOG_PRI(val)
    Parser->>Parser: advance, strtoull(sequence)
    alt seq error / missing comma
      Parser-->>Buffer: goto fail (ctx->buffer_id--)
      Parser->>Kernel: discard line
    else valid sequence
      Parser->>Parser: advance, strtoull(timestamp)
      alt timestamp error / missing comma
        Parser-->>Buffer: goto fail (ctx->buffer_id--)
        Parser->>Kernel: discard line
      else valid timestamp
        Parser->>Parser: tv_sec = val / KMSG_USEC_PER_SEC
        Parser->>Parser: tv_usec = val % KMSG_USEC_PER_SEC
        Parser->>Buffer: commit parsed record
      end
    end
  end
  note right of Parser: Added strict delimiter & overflow checks
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • edsiper
  • koleini
  • fujimotos

Poem

I munched the bytes where kernel whispers flow,
counted commas, carrots, and places to go.
Unsigned hops and 64-bit crunch,
if parsing stumbles I backtrack my lunch.
Now logs are tidy — a rabbit’s small glow. 🥕🐇


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between c1b28df and ee12e7e.

📒 Files selected for processing (1)
  • plugins/in_kmsg/in_kmsg.c (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • plugins/in_kmsg/in_kmsg.c
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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: 2

🧹 Nitpick comments (1)
plugins/in_kmsg/in_kmsg.c (1)

179-180: tv_usec calc fix is correct; consider simpler modulo form and constant.

Functionally correct and 64-bit safe. For readability/consistency with KMSG_USEC_PER_SEC, modulo is clearer.

Apply:

-    tv.tv_sec  = val/1000000;
-    tv.tv_usec = val - ((uint64_t)tv.tv_sec * 1000000);
+    tv.tv_sec  = val / KMSG_USEC_PER_SEC;
+    tv.tv_usec = val % KMSG_USEC_PER_SEC;
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 1132279 and c1b28df.

📒 Files selected for processing (1)
  • plugins/in_kmsg/in_kmsg.c (3 hunks)
🔇 Additional comments (3)
plugins/in_kmsg/in_kmsg.c (3)

125-130: Good switch to unsigned parsing with correct overflow guard.

Using strtoul for priority with the ULONG_MAX/ERANGE check is correct for an unsigned field.


137-137: Priority derivation from unsigned value looks right.

Mapping via FLB_KLOG_PRI(val) after unsigned parsing aligns with the field semantics.


157-162: Nice: strict sequence parsing with delimiter check.

The end != p and immediate ',' enforcement eliminates ambiguous inputs.

Also applies to: 164-165

Comment on lines 173 to 175
/* ensure something was consumed */
if (end == p) {
goto fail;
}

Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Also enforce semicolon adjacency after timestamp (drop the later strchr).

Mirror the sequence check for the timestamp so we don’t accept stray characters between the number and ';'. Advance p via end and remove the subsequent scan.

Apply:

     /* ensure something was consumed */
     if (end == p) {
         goto fail;
     }
 
+    /* after the timestamp, the next char must be ';' */
+    if (*end != ';') {
+        goto fail;
+    }
+    p = ++end;
+
-    /* Now process the human readable message */
-    p = strchr(p, ';');
-    if (!p) {
-        goto fail;
-    }
-    p++;
+    /* Now process the human readable message */

Also applies to: 183-189

🤖 Prompt for AI Agents
In plugins/in_kmsg/in_kmsg.c around lines 173-177 (and similarly for 183-189),
the code only checks that the timestamp parse advanced p but then later searches
for ';' with strchr allowing stray chars; instead set p = end immediately after
parsing the number and then require that *p == ';' (advance p++ after checking)
so no intervening characters are permitted; remove the subsequent strchr() call
and its logic and mirror this exact sequence for the other timestamp parse at
183-189.

* use strtoul for priority since it is unsigned
* check for overflow correctly by comparing against ULONG_MAX
* fail if no digits were consumed or next character is not comma
* calculate tv.tv_usec correctly in case tv.tv_sec 32-bit
* remove redundant errno = 0 added in 3254b9a
* remove erroneous time.h added with 72d9dc8

Signed-off-by: Erik Karlsson <[email protected]>
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