Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8345005

Validate alignment in align_down and related functions

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • None
    • hotspot

      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 from JDK-8318127.]

            kbarrett Kim Barrett
            kbarrett Kim Barrett
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: