Functions like align_down and align_up take size and alignment arguments. If the alignment value is larger than the maximum possible value for the size type, the result might be incorrect, or at best one might get an assertion failure with a not very helpful message.
This could be improved by changing the alignment_mask helper function to check whether the alignment is even possible. The idea is to give it an additional non-deduced template parameter of the type of the value to be aligned. Then test whether the maximum for that type is less than the alignment value. If it is, then the requested alignment isn't possible.
alignment_mask should also be changed to return a value of the type to be aligned, rather than of the alignment argument. All uses are either explicitly making such a conversion, or are doing the usual arithmetic conversions between the two values.
A different approach might be to require the value to be aligned and the alignment to be of the same type. That might make these functions less usable. Though overloads with different types might be added that do a checked conversion of the alignment to the type of the value being aligned.
[This is a followup fromJDK-8318127.]
This could be improved by changing the alignment_mask helper function to check whether the alignment is even possible. The idea is to give it an additional non-deduced template parameter of the type of the value to be aligned. Then test whether the maximum for that type is less than the alignment value. If it is, then the requested alignment isn't possible.
alignment_mask should also be changed to return a value of the type to be aligned, rather than of the alignment argument. All uses are either explicitly making such a conversion, or are doing the usual arithmetic conversions between the two values.
A different approach might be to require the value to be aligned and the alignment to be of the same type. That might make these functions less usable. Though overloads with different types might be added that do a checked conversion of the alignment to the type of the value being aligned.
[This is a followup from
- relates to
-
JDK-8318127 align_up has potential overflow
- Closed