Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{Source,Buffer}.indexOf(ByteString, Int) should return start index for empty ByteString #423

Open
fzhinkin opened this issue Dec 4, 2024 · 4 comments

Comments

@fzhinkin
Copy link
Collaborator

fzhinkin commented Dec 4, 2024

For consistency with CharSequence.indexOf (which out functions are mimicking), Source.indexOf(BS, Int) and Buffer.indexOf(BS, Int) should return a start index (0, by default) when byte string to search is empty.

Currently, both functions return 0 (disregard startIndex value).

@fzhinkin fzhinkin added the bug label Dec 4, 2024
@fzhinkin
Copy link
Collaborator Author

fzhinkin commented Dec 4, 2024

It is not clear though, how (s as Source).indexOf(ByteString(), 100500) show work.

If it returns 100500 without fetching data, the index may lay outside of actual source's data capacity.
Perhaps, we should still fetch the data?

@JakeWharton
Copy link
Contributor

I don't think you can get away with returning the start index without first requiring at least that much data to be loaded.

The CharSequence one doesn't quite return the start index if the search string is empty and there's not enough data. Doing "short".indexOf("", 10) will return 5 and not 10.

I don't love the idea of returning whatever arbitrary maximum index is available from the Source/Buffer because it can be more non-determinstic as opposed to a fully-static String, so I think the only choice might be to require the data.

@qwwdfsad
Copy link
Collaborator

qwwdfsad commented Dec 5, 2024

On a side note -- could you please mention fuzzer in issues found by fuzzer? (so the final bounty list would be easier to compile)

@fzhinkin fzhinkin self-assigned this Dec 10, 2024
@fzhinkin
Copy link
Collaborator Author

After all, I convinced myself that indexOf(empty byte string) should behave the same way CharSequence.indexOf("").
That means, Buffer|Source.indexOf(ByteString, someIndex) == max(0, min(someIndex, buffer.size)).

For Source, we'll try to fetch max(0, someIndex) bytes first.

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

No branches or pull requests

3 participants