Fix bug where bin number could overflow when looking for max_off #1595
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.
When searching for
max_off
,hts_itr_query()
andhts_itr_multi_bam()
look for a bin to the right of the end of the region. For whole chromosomes, this would be HTS_POS_MAX, which is far beyond the maximum bin position supported. Thebin
calculation overflowed leading to either a negative bin number or an incorrect positive one, depending on the number of levels in the index. Negative bin numbers simply caused time to be wasted as the search loop eventually counted up to zero, but incorrect positive ones could cause the iterator to finish too early.Fix by catching the out-of-bounds case and setting max_off to UINT64_MAX, which should be used for bins beyond the end of the indexable range.
Luckily in practice this didn't cause too much harm, at least for the default
min_shift
value. Indexes with up to six levels overflowed to negative bin numbers. For seven you got one referencing a region starting at about 17Gbases, and 257Gbases for eight, so it's unlikely searches on real data were affected. The fix is trivial though and avoids some negative value shifts so worth doing.