Repository navigation
fix: stop BinarySearch2dArray crashing on two-column matrices - #7789
Merged
DenizAltunkapan merged 1 commit intoNov 30, 2026
Merged
DenizAltunkapan merged 1 commit into
DenizAltunkapan merged 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7789 +/- ##
============================================
- Coverage 82.58% 82.58% -0.01%
Complexity 8604 8604
============================================
Files 848 848
Lines 26460 26460
Branches 5181 5181
============================================
- Hits 21853 21852 -1
- Misses 3823 3824 +1
Partials 784 784 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Aditya2375
marked this pull request as ready for review
November 29, 2026 19:17
Aditya2375
requested review from
DenizAltunkapan,
alxkm and
yanglbme
as code owners
November 29, 2026 19:17
DenizAltunkapan
approved these changes
Nov 30, 2026
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.
Describe your change:
Fixed
BinarySearch2dArray.binarySearchthrowingArrayIndexOutOfBoundsExceptionon any matrix with exactly two columns when the target is not found in the middle column.The method compares the target against the middle column (
midCol = colCount / 2) to pick a row band, then searches the right part of the candidate row viaarr[startRow][midCol + 1]. For a two-column matrixmidColis 1, which is already the last column, somidCol + 1reads past the end of the row and the search crashes - including for targets that are present in the matrix:The existing tests only use four-column and single-column matrices, which is why this survived.
The fix guards the right-side branch with
midCol + 1 < colCount. When there is no column to the right of the middle column, the search falls through to theendRowchecks, which route the target to the correct row segment (or report[-1, -1]when it is absent). No other path changes: for three or more columns the guard is always true.I verified the fix by differential testing against a brute-force scan on 200,000 randomized matrices (1-7 rows/columns, sorted with duplicates, targets both present and absent): zero crashes, zero mismatched results. Added regression tests for the two-column cases (present target, absent target, larger matrix) and a value-gap case; they fail on the current code and pass with the fix. All 21 tests in
BinarySearch2dArrayTestpass.Checklist:
clang-format -i --style=file path/to/your/file.java