Raster.createBandedRaster(int, int, int, int, int[], int[], Point) does not check for negative scanlineStride

XMLWordPrintable

    • 2d
    • b08

      from spec of
       java.awt.image.Raster.createBandedRaster(int dataType, int w, int h, int scanlineStride, int[] bankIndices, int[] bandOffsets, Point location),
      we have:
       IllegalArgumentException – if scanlineStride is less than 0.

      However, when I tried large negative values such as -1000 or Integer.MIN_VALUE, I encountered a java.lang.NegativeArraySizeException instead of the expected IllegalArgumentException.

      Code Example:
      void main(String[] args) {

              int scanlineStride = -100;
              int[] bankIndices = {0, 1, 2};
              int[] bandOffsets = {0, 0, 0};
                  WritableRaster.createBandedRaster(
                          DataBuffer.TYPE_BYTE,
                          2, // width
                          2, // height
                          scanlineStride,
                          bankIndices,
                          bandOffsets,
                          new Point(0, 0)
                  );
       }

      After reviewing the OpenJDK code, I noticed that the exception is thrown before execution reaches the check for scanlineStride < 0. Specifically, it occurs in the following block:

      case DataBuffer.TYPE_BYTE:
          d = new DataBufferByte(size, banks);
          break;

      As a result, the documented exception behavior is not consistently enforced for all negative values of scanlineStride.



      I think this warrants a documentation enhancement to clarify the actual behavior.

      Spec (Java SE 27 ) : https://download.java.net/java/early_access/jdk27/docs/api/java.desktop/java/awt/image/Raster.html#createBandedRaster(int,int,int,int,int%5B%5D,int%5B%5D,java.awt.Point)

            Assignee:
            Philip Race
            Reporter:
            Hamza Nassour
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: