Skip to content

fix(tags): don't panic on tag filters with empty operands#758

Open
nikolauspschuetz wants to merge 1 commit into
cucumber:mainfrom
nikolauspschuetz:fix/tag-filter-empty-component
Open

fix(tags): don't panic on tag filters with empty operands#758
nikolauspschuetz wants to merge 1 commit into
cucumber:mainfrom
nikolauspschuetz:fix/tag-filter-empty-component

Conversation

@nikolauspschuetz

Copy link
Copy Markdown

Summary

ApplyTagFilter panics on a --tags filter that contains an empty operand.

matchAnd trims each operand and strips @, then immediately indexes tag[0]:

tag = strings.TrimSpace(tag)
tag = strings.Replace(tag, "@", "", -1)
if tag[0] == '~' {   // panics when tag == ""

Any filter that splits to an empty operand triggers runtime error: index out of range [0] with length 0:

  • @one,,@two (double comma)
  • @one, (trailing comma)
  • ,@one (leading comma)
  • @one&& (trailing &&)
  • @one&&&&@two (double &&)

Since the filter string comes straight from user input (-t/--tags), a small typo crashes the whole run with a stack trace instead of a graceful result.

Fix

Guard the empty operand before indexing. An empty tag can never be present on a pickle, so it fails the AND — identical to what the existing else branch (contains(tags, "")false) would compute if it were reachable — and is harmlessly ignored for the OR (comma) case:

tag = strings.Replace(tag, "@", "", -1)
if tag == "" {
    and = false
    continue
}
if tag[0] == '~' {

Tests

Added regression cases to Test_ApplyTagFilter for all five malformed filters above (each previously panicked; behaviour: @one,,@two{p1, p2}, @one,{p1}, ,@one{p1}, @one&&{}, @one&&&&@two{}).

Verified:

  • Reproduced the panic on main (index out of range [0] with length 0).
  • go test ./internal/tags/ — passes with the new cases.
  • go vet + gofmt clean.

matchAnd trims and strips '@' from each operand and then indexes tag[0]
without checking for an empty string. A user-supplied --tags filter with
an empty operand — e.g. "@one,,@two", a trailing "@one,", a leading
",@one", or "@one&&" — produces an empty operand and panics with
'index out of range [0] with length 0' instead of filtering.

Guard the empty operand before indexing: an empty tag can never be present
on a pickle, so it fails the AND (matching the existing else branch) and is
harmlessly ignored for OR. Adds regression cases covering the malformed
filters that previously panicked.

Signed-off-by: Nikolaus Schuetz <nikolauspschuetz@gmail.com>
@nikolauspschuetz

Copy link
Copy Markdown
Author

One design note worth your call: this fix makes an empty operand forgiving@one,,@two is treated the same as @one,@two (empty piece ignored for OR, fails AND), matching what the existing else branch already computes for a non-present tag. I went this way because ApplyTagFilter has no error return, so the alternative — rejecting a malformed filter with a validation error — would mean changing the signature and all callers, which felt like your decision rather than a drive-by. Happy to switch to a hard "invalid tag filter" error instead if you'd prefer strict validation over lenient tolerance.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant