Skip to content

fix: check empty search ranges - #1606

Open
qownain wants to merge 1 commit into
TheAlgorithms:masterfrom
qownain:fix-binary-search-empty-range
Open

qownain wants to merge 1 commit into
TheAlgorithms:masterfrom
qownain:fix-binary-search-empty-range

Conversation

@qownain

@qownain qownain commented Nov 20, 2026

Copy link
Copy Markdown

Description of Change

The iterative binary search reads the midpoint before checking whether the range is empty. For {2, 3, 4, 10, 40}, searching [2, 1] for 4 returns 2 instead of -1. An empty array can also cause an invalid read.

This checks the range before reading the array. The added assertions cover empty ranges, boundary values, missing values, and single element ranges.

Validation

C11 compilation with warnings treated as errors, the CMake binary_search target, and all assertions pass. Another 462 range and target combinations match a linear search. Formatting and whitespace checks pass.

The installed clang-tidy cannot load the existing configuration because AnalyzeTemporaryDtors is no longer supported.

Checklist

  • Added description of change
  • Added tests and example, test must pass
  • Relevant comments added
  • PR title follows semantic commit guidelines
  • Searched existing issues and open pull requests for duplicates

Prepared with Codex assistance.

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.

1 participant