BitMap::word_index_round_up tries to be careful about overflow in
bit + (BitsPerWord - 1)
If that doesn't overflow, then it returns word_index of that. But if it overflows, the result of size_in_words is returned instead. But size_in_words just performs exactly the same computation that overflowed to get there; see calc_size_in_words.
An always safe from overflow way to compute it is:
word_index(bit) + (is_word_aligned(bit) ? 0 : 1)
Of course, there is the practical consideration that overflow involves a bitmap that is "unlikely" to fit in memory, e.g > ~2 exabytes on a 64bit platform, making the overflow handling here probably a waste of time for actual bitmap indices. But overflow is quite possible on a 32bit platform, where only 1/2GByte bitmap is needed.
Also note that BitMap::is_word_aligned uses the (slightly) more expensive word_align_up rather than word_align_down. is_word_aligned should just be is_aligned(bit, BitsPerWord).
bit + (BitsPerWord - 1)
If that doesn't overflow, then it returns word_index of that. But if it overflows, the result of size_in_words is returned instead. But size_in_words just performs exactly the same computation that overflowed to get there; see calc_size_in_words.
An always safe from overflow way to compute it is:
word_index(bit) + (is_word_aligned(bit) ? 0 : 1)
Of course, there is the practical consideration that overflow involves a bitmap that is "unlikely" to fit in memory, e.g > ~2 exabytes on a 64bit platform, making the overflow handling here probably a waste of time for actual bitmap indices. But overflow is quite possible on a 32bit platform, where only 1/2GByte bitmap is needed.
Also note that BitMap::is_word_aligned uses the (slightly) more expensive word_align_up rather than word_align_down. is_word_aligned should just be is_aligned(bit, BitsPerWord).
- relates to
-
JDK-8211926 Catastrophic size_t underflow in BitMap::*_large methods
-
- Resolved
-