提交 f471610c 编写于 作者: P prr

8011257: Better Byte Component Rasters

Reviewed-by: bae, vadim, mschoene
上级 7e29bf28
...@@ -159,7 +159,7 @@ public class ByteBandedRaster extends SunWritableRaster { ...@@ -159,7 +159,7 @@ public class ByteBandedRaster extends SunWritableRaster {
throw new RasterFormatException("ByteBandedRasters must have"+ throw new RasterFormatException("ByteBandedRasters must have"+
"BandedSampleModels"); "BandedSampleModels");
} }
verify(false); verify();
} }
...@@ -731,16 +731,30 @@ public class ByteBandedRaster extends SunWritableRaster { ...@@ -731,16 +731,30 @@ public class ByteBandedRaster extends SunWritableRaster {
} }
/** /**
* Verify that the layout parameters are consistent with * Verify that the layout parameters are consistent with the data.
* the data. If strictCheck * Verifies whether the data buffer has enough data for the raster,
* is false, this method will check for ArrayIndexOutOfBounds conditions. If * taking into account offsets, after ensuring all offsets are >=0.
* strictCheck is true, this method will check for additional error * @throws RasterFormatException if a problem is detected.
* conditions such as line wraparound (width of a line greater than
* the scanline stride).
* @return String Error string, if the layout is incompatible with
* the data. Otherwise returns null.
*/ */
private void verify (boolean strictCheck) { private void verify() {
/* Need to re-verify the dimensions since a sample model may be
* specified to the constructor
*/
if (width <= 0 || height <= 0 ||
height > (Integer.MAX_VALUE / width))
{
throw new RasterFormatException("Invalid raster dimension");
}
if (scanlineStride < 0 ||
scanlineStride > (Integer.MAX_VALUE / height))
{
// integer overflow
throw new RasterFormatException("Incorrect scanline stride: "
+ scanlineStride);
}
// Make sure data for Raster is in a legal range // Make sure data for Raster is in a legal range
for (int i=0; i < dataOffsets.length; i++) { for (int i=0; i < dataOffsets.length; i++) {
if (dataOffsets[i] < 0) { if (dataOffsets[i] < 0) {
...@@ -750,32 +764,41 @@ public class ByteBandedRaster extends SunWritableRaster { ...@@ -750,32 +764,41 @@ public class ByteBandedRaster extends SunWritableRaster {
} }
} }
int maxSize = 0; int lastScanOffset = (height - 1) * scanlineStride;
int size; int lastPixelOffset = lastScanOffset + (width-1);
if (lastPixelOffset < lastScanOffset) {
throw new RasterFormatException("Invalid raster dimension");
}
int maxIndex = 0;
int index;
for (int i=0; i < numDataElements; i++) { for (int i=0; i < numDataElements; i++) {
size = (height-1)*scanlineStride + (width-1) + dataOffsets[i]; index = lastPixelOffset + dataOffsets[i];
if (size > maxSize) { if (index < lastPixelOffset) {
maxSize = size; throw new RasterFormatException("Invalid raster dimension");
}
if (index > maxIndex) {
maxIndex = index;
} }
} }
if (data.length == 1) { if (data.length == 1) {
if (data[0].length < maxSize*numDataElements) { if (data[0].length <= maxIndex*numDataElements) {
throw new RasterFormatException("Data array too small "+ throw new RasterFormatException("Data array too small "+
"(it is "+data[0].length+ "(it is "+data[0].length+
" and should be "+ " and should be > "+
(maxSize*numDataElements)+ (maxIndex*numDataElements)+
" )"); " )");
} }
} }
else { else {
for (int i=0; i < numDataElements; i++) { for (int i=0; i < numDataElements; i++) {
if (data[i].length < maxSize) { if (data[i].length <= maxIndex) {
throw new RasterFormatException("Data array too small "+ throw new RasterFormatException("Data array too small "+
"(it is "+data[i].length+ "(it is "+data[i].length+
" and should be "+ " and should be > "+
maxSize+" )"); maxIndex+" )");
} }
} }
} }
......
...@@ -885,9 +885,6 @@ public class ByteComponentRaster extends SunWritableRaster { ...@@ -885,9 +885,6 @@ public class ByteComponentRaster extends SunWritableRaster {
} }
} }
int maxSize = 0;
int size;
// we can be sure that width and height are greater than 0 // we can be sure that width and height are greater than 0
if (scanlineStride < 0 || if (scanlineStride < 0 ||
scanlineStride > (Integer.MAX_VALUE / height)) scanlineStride > (Integer.MAX_VALUE / height))
...@@ -913,6 +910,8 @@ public class ByteComponentRaster extends SunWritableRaster { ...@@ -913,6 +910,8 @@ public class ByteComponentRaster extends SunWritableRaster {
} }
lastPixelOffset += lastScanOffset; lastPixelOffset += lastScanOffset;
int index;
int maxIndex = 0;
for (int i = 0; i < numDataElements; i++) { for (int i = 0; i < numDataElements; i++) {
if (dataOffsets[i] > (Integer.MAX_VALUE - lastPixelOffset)) { if (dataOffsets[i] > (Integer.MAX_VALUE - lastPixelOffset)) {
throw new RasterFormatException("Incorrect band offset: " throw new RasterFormatException("Incorrect band offset: "
...@@ -920,15 +919,15 @@ public class ByteComponentRaster extends SunWritableRaster { ...@@ -920,15 +919,15 @@ public class ByteComponentRaster extends SunWritableRaster {
} }
size = lastPixelOffset + dataOffsets[i]; index = lastPixelOffset + dataOffsets[i];
if (size > maxSize) { if (index > maxIndex) {
maxSize = size; maxIndex = index;
} }
} }
if (data.length < maxSize) { if (data.length <= maxIndex) {
throw new RasterFormatException("Data array too small (should be " throw new RasterFormatException("Data array too small (should be > "
+ maxSize + " )"); + maxIndex + " )");
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册