Skip to content

in_tail: avoid libc basename() in file name compare - #12782

Open
kevin9327 wants to merge 1 commit into
fluent:masterfrom
kevin9327:fix-in-tail-basename-thread-safety
Open

kevin9327 wants to merge 1 commit into
fluent:masterfrom
kevin9327:fix-in-tail-basename-thread-safety

Conversation

@kevin9327

@kevin9327 kevin9327 commented Nov 12, 2026 •

Copy link
Copy Markdown

flb_tail_target_file_name_cmp called libc basename() twice. On macOS (and some other libcs) basename() uses a process-wide static buffer, so a threaded in_tail input can overwrite that buffer between basename() and strdup(), or during strcmp(), and then treat an unchanged file as rotated. That produces duplicate records.

This replaces basename() with a thread-safe scan for the last path separator (/ and, on Windows, \) on the original strings, so no shared buffer or extra copies are involved. Windows still uses _stricmp.

Fixes #12780


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
  • [N/A] Debug log output from testing the change
  • [N/A] 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.

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

Documentation

  • [N/A] Documentation required for this feature

Backporting

  • [N/A] 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
    • Improved tail file name comparisons to consistently match files by their final path component, including Windows paths with either slash style.
    • File name comparisons remain case-insensitive on Windows and case-sensitive on other platforms.

Apple's basename() uses a process-wide static buffer, so a threaded
in_tail input can overwrite the pointer mid-compare and report a
false rotation. Compare the suffix after the last path separator on
the original strings instead.

Fixes fluent#12780

Signed-off-by: kevin9327 <5299031+kevin9327@users.noreply.github.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T03:45:46.965611Z 505de56 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The tail file name comparison now uses a helper that extracts path basenames without allocating memory. It recognizes platform-specific separators and retains case-insensitive comparison on Windows and case-sensitive comparison elsewhere.

Changes: Tail path name comparison

Layer / File(s) Summary
Extract and compare path basenames
plugins/in_tail/tail_file.h
flb_tail_path_basename returns the portion after the final recognized separator, or an empty string for a null path. flb_tail_target_file_name_cmp compares the extracted components directly and no longer allocates or frees strings, or returns -1 for allocation failures.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · up to 505de

This change removes a thread-safety race in tail file name comparison that could cause unchanged files to be treated as rotated and have their records duplicated. No actionable merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "in_tail: avoid libc basename() in file name compare" matches the main change. The PR replaces libc basename() in flb_tail_target_file_name_cmp with a scan for the last path separator, which…
Linked Issues check Passed Issue #12780 requires that unchanged files not be reported as rotated when a threaded in_tail input runs the name compare concurrently with other tail inputs. The root cause is the shared static buffe…
Out of Scope Changes check Passed The change set, as shown by the whole-PR summary and the tail_file.h diff against the merge base, touches only plugins/in_tail/tail_file.h. All edits are in flb_tail_target_file_name_cmp and its new h…
Full details: Title check

Explanation

The title "in_tail: avoid libc basename() in file name compare" matches the main change. The PR replaces libc basename() in flb_tail_target_file_name_cmp with a scan for the last path separator, which the raw summary confirms via the new flb_tail_path_basename helper and the rewritten comparison. The title names the plugin, the libc call removed, and the affected function's purpose, so it is specific and concise.

Full details: Linked Issues check

Explanation

Issue #12780 requires that unchanged files not be reported as rotated when a threaded in_tail input runs the name compare concurrently with other tail inputs. The root cause is the shared static buffer returned by libc basename(). The diff to plugins/in_tail/tail_file.h removes all libc basename() calls from flb_tail_target_file_name_cmp(). It adds flb_tail_path_basename(), which returns a pointer into the original string after the last '/' (and '' on Windows). The comparison then uses strcmp() on non-Windows and _stricmp() on Windows. No shared state remains, so the race described in the issue no longer exists on any platform. The issue explicitly lists a portable helper that avoids libc basename() as an acceptable alternative to basename_r(). The PR does not add an automated regression test, and the PR description states no test run. The issue does not require one. Runtime behavior on macOS is not verified in the available evidence. Minor semantic difference: libc basename('dir/') returns 'dir', while the new helper returns ''. This does not affect regular file paths.

Full details: Out of Scope Changes check

Explanation

The change set, as shown by the whole-PR summary and the tail_file.h diff against the merge base, touches only plugins/in_tail/tail_file.h. All edits are in flb_tail_target_file_name_cmp and its new helper flb_tail_path_basename. This directly implements the fix for #12780. The removal of flb_strdup() and flb_free() calls and of the -1 allocation-failure returns follows from the removed allocations. This is a necessary part of the fix, not unrelated work. No unrelated changes were found.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: adff7107-9232-46b8-b504-162062576281
Commits

Reviewing files that changed from the base of the PR and between f5a3cd2 and 505de56.

Files selected for processing: 1

  • plugins/in_tail/tail_file.h

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

This branch has not been deployed

No deployments
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.

in_tail: files falsely detected as rotated on macOS with threaded inputs, causing duplicate records

1 participant