fix(builtins): prevent panic in strings.count on empty needle - #796
Open
Anand Krishnamoorthi (anakrish) wants to merge 1 commit into
Open
fix(builtins): prevent panic in strings.count on empty needle#796Anand Krishnamoorthi (anakrish) wants to merge 1 commit into
Anand Krishnamoorthi (anakrish) wants to merge 1 commit into
Conversation
strings.count panicked when called with an empty needle because .windows(0) is invalid. Fix three issues in strings_count: 1. Wrong param index: params[0] was used for the substring error span instead of params[1]. 2. Panic: .windows(needle.len()) panics when needle is empty (len == 0). 3. Semantics: the byte-level .windows() approach counted overlapping matches; OPA uses non-overlapping match semantics (Go strings.Count). The fix: - Correct the param index to params[1] for the substring argument. - Handle empty needle explicitly: return chars().count() + 1, matching Go's strings.Count behaviour (empty needle matches between every character and at both ends). - Use str::matches() for non-overlapping count on non-empty needles. A YAML regression test is added covering basic match, no-match, multi-match, empty-needle (must not panic), empty-needle on empty string, non-overlapping semantics, and multi-byte UTF-8 strings. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Anand Krishnamoorthi (anakrish)
force-pushed
the
fix/strings-count-empty-needle-panic
branch
from
August 28, 2026 15:34
67aadd3 to
e0e0e31
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
strings.countpanicked at runtime when called with an empty needle string because.windows(0)is not valid in Rust. This PR fixes three tightly coupled issues instrings_count:Bugs fixed
params[0]used forsubstringerror span instead ofparams[1]).windows(needle.len())panics when needle is empty (len == 0).windows()counted overlapping matches; OPA uses non-overlapping semantics (Gostrings.Count)strings.count("aaaa", "aa")returned 3 instead of 2Fix
Tests
A YAML regression test is added at
tests/interpreter/cases/builtins/strings/strings_count.yamlcovering:char_count + 1)strings.count("aaaa", "aa") = 2, not 3)