fix: off-by-one bounds guards in SegmentTree - #7573
Open
SEPURI-SAI-KRISHNA wants to merge 1 commit into
Open
Conversation
SEPURI-SAI-KRISHNA
requested review from
DenizAltunkapan,
alxkm and
yanglbme
as code owners
August 15, 2026 17:13
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7573 +/- ##
============================================
+ Coverage 80.42% 80.59% +0.16%
- Complexity 7460 7482 +22
============================================
Files 815 815
Lines 24056 24060 +4
Branches 4733 4736 +3
============================================
+ Hits 19348 19390 +42
+ Misses 3945 3906 -39
- Partials 763 764 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Problem
SegmentTreeguardsupdateandgetSumagainst out-of-range positions, but both guards compare againstninstead ofn - 1. Valid positions are0 .. n-1, soindex == nslips past the check.In
updatethe guard is followed immediately by an array read, so the call throws from inside the very method that was supposed to reject it:getSum(0, 5)is the quieter half of the bug: the out-of-range query reachesgetSumTree, matches theqStart <= start && qEnd >= endshort circuit at the root and returns the root sum, so the caller gets a plausible-looking number for a range that does not exist.The constructor is also unguarded.
new SegmentTree(0, arr)computesMath.log(0) == -Infinity, which casts toInteger.MIN_VALUEand yields a segment array size of-1, throwingNegativeArraySizeException; a size larger than the array throwsArrayIndexOutOfBoundsExceptionwhile building the tree.Fix
updaterejectsindex >= nandgetSumrejectsend >= n, preserving the existing contract of returning silently / returning0for out-of-range input.IllegalArgumentExceptionfor anullarray or a size outside[1, arr.length].this.n = n;assignment.Tests
The class had no test class at all.
SegmentTreeTestis added, covering:range sums, single-element trees, negative values and updates reflected in later queries
updateatindex == nand beyond being ignored instead of throwing — this fails on the old codeout-of-range queries returning
0, includinggetSum(0, n)— this fails on the old codeconstructor validation for invalid sizes and a
nullarrayan exhaustive cross-check of every
[start, end]range against a brute-force sum for sizes 1..9, 16 and 17, which covers both the exact powers of two and the sizes in betweenI have read CONTRIBUTING.md.
This pull request is all my own work -- I have not plagiarized it.
All filenames are in PascalCase.
All functions and variable names follow Java naming conventions.
All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
All new algorithms include a corresponding test class that validates their functionality.
All new code is formatted with
clang-format -i --style=file path/to/your/file.java