Skip to content

Fix search_n reading past last for random access iterators - #600

Open
sebxt wants to merge 1 commit into
electronicarts:masterfrom
sebxt:fix/search-n-random-access-past-end
Open

sebxt wants to merge 1 commit into
electronicarts:masterfrom
sebxt:fix/search-n-random-access-past-end

Conversation

@sebxt

@sebxt sebxt commented Oct 20, 2026

Copy link
Copy Markdown

The random access implementation of search_n did not count the mismatching element after a forward scan, so tailSize was one too large. That let the outer loop step lookAhead onto last, which reads out of bounds and can return a match that extends past the end of the range.

Example: search_n(a, a + 4, 2, 1) with a = { 0, 1, 0, 1, 1 } returned a + 3 instead of a + 4.

Changes:

  • Subtract the mismatching element from tailSize as well.
  • Add regression cases to TestAlgorithm.cpp. Each array has trailing elements outside the searched range so the old behavior is caught without a sanitizer.

The new checks fail before the change and pass after it. The full EASTLTest suite passes (Debug, clang, macOS x86_64).

Fixes #599

Signed-off-by: Sebastian Turner <sebastiant3484@gmail.com>

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

search_n with random access iterators can return a match that runs past last

1 participant