Skip to content

XUtils: ignore empty needles in a multi-term filter - #2102

Closed
rawsun007 wants to merge 1 commit into
htop-dev:mainfrom
rawsun007:fix/filter-empty-needle
Closed

XUtils: ignore empty needles in a multi-term filter#2102
rawsun007 wants to merge 1 commit into
htop-dev:mainfrom
rawsun007:fix/filter-empty-needle

Conversation

@rawsun007

Copy link
Copy Markdown
Contributor

String_contains_i() splits the filter on | and hands each needle to strcasestr(), which reports a match against anything for an empty needle. String_split() emits an empty field for a leading or repeated separator but drops a trailing one, so the same typo behaved differently depending on where it landed:

filter before after
nginx| nothing extra unchanged
|nginx every row only nginx rows
nginx||postgres every row nginx or postgres rows

So while composing a multi-term filter, a stray or half-typed | silently stopped the filter from filtering — and the same mistake at the end of the line was harmless. That inconsistency is what convinced me it wasn't intentional: if an empty alternative were meant to match everything, a trailing | would do it too.

Empty needles are now skipped. A filter consisting only of separators carries no search term and still matches everything, the same as an empty filter — the incremental-search callers already treat a null filter that way.

Fixed in String_contains_i() rather than String_split(), since the empty-field behaviour there is relied on elsewhere and only the filter's semantics are wrong.

Verification, on darwin: linked a small driver against the tree's own XUtils.o and ran 14 cases covering a leading, trailing, doubled and interior separator on both the matching and non-matching side, plus case-insensitivity and separators alone. All pass; reverting the change fails exactly |nginx and nginx||postgres. Full make clean with no new warnings under the tree's flags (-Wall -Wextra -pedantic and the rest).

I could not add a regression test: the only harness here is test_spec.lua, which drives a terminal rather than calling into C, so there is nowhere for a unit test of this to live. Happy to write a test_spec.lua case that types a filter into a live htop if you would rather have coverage that way — it seemed worse than the C-level evidence above, but it is your call.

AI disclosure per docs/ai-contributions-policy.md: found and written with Claude Code (Claude Opus 5), recorded in the commit's Assisted-by: trailer. @rawsun007 authorised the Signed-off-by: and reviewed the change.

`String_contains_i()` splits the filter on `|` and passes each needle to
`strcasestr()`, which reports a match against anything for an empty needle.
`String_split()` emits an empty field for a leading or repeated separator but
drops a trailing one, so the filter behaved inconsistently:

    filter          matches
    nginx|          nothing extra   (trailing separator is dropped)
    |nginx          every row
    nginx||postgres every row

Typing or pasting a stray `|` therefore stopped the filter from filtering,
while the same mistake at the end of the line was harmless. Empty needles are
now skipped, so the two spellings agree.

A filter of only separators carries no search term and still matches
everything, as an empty filter does.

Verified on darwin against the built `XUtils.o`, over 14 cases covering a
leading, trailing, doubled and interior separator on both the matching and
non-matching side; reverting this change fails exactly the `|nginx` and
`nginx||postgres` cases. No new warnings under the tree's own flags.

Assisted-by: Claude Code (Claude Opus 5)
Signed-off-by: Roshan Ramani <roshanramani.dev@gmail.com>
@coderabbitai

coderabbitai Bot commented Sep 5, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Team

Run ID: 2da47536-28fc-4d88-84e2-fda12d31a8db

📥 Commits

Reviewing files that changed from the base of the PR and between a3dd8ad and 0dd0e9b.

📒 Files selected for processing (1)
  • XUtils.c

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


📝 Walkthrough

Walkthrough

String_contains_i now skips empty needles caused by leading or repeated | separators. It tracks the number of non-empty needles searched. A separator-only pattern returns true, which makes it behave like an empty filter. Non-empty needles continue to use case-insensitive substring matching.

Poem

Pipes part, but blanks stand still
Searches skip the empty sill
Needles count what they inspect
Empty filters now connect
Rows await a clearer sign

Merge Risk: ⚪ Minimal · up to 0dd0e

Filters with leading, repeated, or separator-only terms now behave as intended without changing non-empty term matching. The change is ready to merge.


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.

@BenBE BenBE added the enhancement Extension or improvement to existing feature label Sep 5, 2026
@fasterit

fasterit commented Sep 7, 2026

Copy link
Copy Markdown
Member

I'll merge this without the first AI comment. Today's AIs tend to work around their limited memory and context window by adding rather useless, verbose (to a human) commentary. So I'll cull that one. Otherwise thank you for the PR.

@fasterit fasterit added this to the 3.6.0 milestone Sep 7, 2026
@fasterit

fasterit commented Sep 7, 2026

Copy link
Copy Markdown
Member

Merged as b9a2a63

@fasterit fasterit closed this Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Extension or improvement to existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants