Program: 11181
Project: 11181
Buggy source:
protected void readRectangle(Dataset dataset,Rectangle srcRect) throws GDALException, TilerException {
  int dataBandCount=dataset.getRasterCount();
  if (dataBandCount <= 0) {
    throw new TilerException("String_Node_Str");
  }
  if (selectedBand >= 0 && selectedBand >= dataBandCount) {
    throw new IllegalArgumentException("String_Node_Str");
  }
  bufferBandCount=selectedBand >= 0 ? 1 : (addAlpha && dataBandCount == 3) ? 4 : dataBandCount;
  bufferType=0;
  Band[] bands=null;
  if (selectedBand >= 0) {
    bands=new Band[1];
    bands[0]=dataset.GetRasterBand(selectedBand + 1);
    bufferType=bands[0].getDataType();
  }
 else {
    bands=new Band[dataBandCount];
    for (int i=0; i < dataBandCount; i++) {
      bands[i]=dataset.GetRasterBand(i + 1);
      if (i == 0) {
        bufferType=bands[i].getDataType();
      }
 else       if (bands[i].getDataType() != bufferType) {
        throw new TilerException("String_Node_Str");
      }
    }
  }
  int dataTypeSize=gdal.GetDataTypeSize(bufferType);
  bufferTypeSize=dataTypeSize / 8;
  int pixels=width * height;
  int bandSize=pixels * bufferTypeSize;
  buffer=ByteBuffer.allocateDirect(bandSize * bufferBandCount);
  buffer.order(ByteOrder.LITTLE_ENDIAN);
  dataRectangle=new Rectangle(0,0,width,height);
  if (srcRect == null) {
    srcRect=new Rectangle(0,0,dataset.getRasterXSize(),dataset.getRasterYSize());
  }
 else   if (srcRect.x < 0 || srcRect.y < 0 || srcRect.x + srcRect.width > dataset.getRasterXSize() || srcRect.y + srcRect.height > dataset.getRasterYSize()) {
    Rectangle newSrcRect=new Rectangle(0,0,dataset.getRasterXSize(),dataset.getRasterYSize());
    newSrcRect=srcRect.intersection(newSrcRect);
    if (!newSrcRect.isEmpty()) {
      int dstX=dataRectangle.width * (newSrcRect.x - srcRect.x) / srcRect.width;
      int dstY=dataRectangle.height * (newSrcRect.y - srcRect.y) / srcRect.height;
      int dstWidth=dataRectangle.width - dataRectangle.width * (srcRect.width - newSrcRect.width) / srcRect.width;
      int dstHeight=dataRectangle.height - dataRectangle.height * (srcRect.height - newSrcRect.height) / srcRect.height;
      dataRectangle=new Rectangle(dstX,dstY,dstWidth,dstHeight);
    }
 else {
      throw new TilerException("String_Node_Str");
    }
    srcRect=newSrcRect;
  }
  for (int b=0; b < bands.length; b++) {
    if (!srcRect.isEmpty()) {
      buffer.position(b * bandSize + dataRectangle.x + dataRectangle.y * width);
      ByteBuffer sliced=buffer.slice();
      int returnVal=bands[b].ReadRaster_Direct(srcRect.x,srcRect.y,srcRect.width,srcRect.height,dataRectangle.width,dataRectangle.height,bufferType,sliced,bufferTypeSize,bufferTypeSize * width);
      if (returnVal != gdalconstConstants.CE_None) {
        throw new GDALException();
      }
    }
  }
  if (bufferBandCount == 4 && dataBandCount != 4) {
    for (int y=0; y < height; y++) {
      for (int x=0; x < width; x++) {
        int index=getBufferIndex(x,y,3) * bufferTypeSize;
        for (int i=0; i < bufferTypeSize; i++) {
          if (dataRectangle.contains(x,y))           buffer.put(index + i,(byte)0xff);
 else           buffer.put(index + i,(byte)0x00);
        }
      }
    }
  }
  buffer.rewind();
  Band lastBand=bands[bands.length - 1];
  boolean indexed=lastBand.GetRasterColorInterpretation() == gdalconstConstants.GCI_PaletteIndex;
  indexColorModel=indexed ? lastBand.GetRasterColorTable().getIndexColorModel(dataTypeSize) : null;
}

Fixed version (ground truth):
protected void readRectangle(Dataset dataset,Rectangle srcRect) throws GDALException, TilerException {
  int dataBandCount=dataset.getRasterCount();
  if (dataBandCount <= 0) {
    throw new TilerException("String_Node_Str");
  }
  if (selectedBand >= 0 && selectedBand >= dataBandCount) {
    throw new IllegalArgumentException("String_Node_Str");
  }
  bufferBandCount=selectedBand >= 0 ? 1 : (addAlpha && dataBandCount == 3) ? 4 : dataBandCount;
  bufferType=0;
  Band[] bands=null;
  if (selectedBand >= 0) {
    bands=new Band[1];
    bands[0]=dataset.GetRasterBand(selectedBand + 1);
    bufferType=bands[0].getDataType();
  }
 else {
    bands=new Band[dataBandCount];
    for (int i=0; i < dataBandCount; i++) {
      bands[i]=dataset.GetRasterBand(i + 1);
      if (i == 0) {
        bufferType=bands[i].getDataType();
      }
 else       if (bands[i].getDataType() != bufferType) {
        throw new TilerException("String_Node_Str");
      }
    }
  }
  int dataTypeSize=gdal.GetDataTypeSize(bufferType);
  bufferTypeSize=dataTypeSize / 8;
  floatingPoint=isTypeFloatingPoint(bufferType);
  int pixels=width * height;
  int bandSize=pixels * bufferTypeSize;
  buffer=ByteBuffer.allocateDirect(bandSize * bufferBandCount);
  buffer.order(ByteOrder.LITTLE_ENDIAN);
  dataRectangle=new Rectangle(0,0,width,height);
  if (srcRect == null) {
    srcRect=new Rectangle(0,0,dataset.getRasterXSize(),dataset.getRasterYSize());
  }
 else   if (srcRect.x < 0 || srcRect.y < 0 || srcRect.x + srcRect.width > dataset.getRasterXSize() || srcRect.y + srcRect.height > dataset.getRasterYSize()) {
    Rectangle newSrcRect=new Rectangle(0,0,dataset.getRasterXSize(),dataset.getRasterYSize());
    newSrcRect=srcRect.intersection(newSrcRect);
    if (!newSrcRect.isEmpty()) {
      int dstX=dataRectangle.width * (newSrcRect.x - srcRect.x) / srcRect.width;
      int dstY=dataRectangle.height * (newSrcRect.y - srcRect.y) / srcRect.height;
      int dstWidth=dataRectangle.width - dataRectangle.width * (srcRect.width - newSrcRect.width) / srcRect.width;
      int dstHeight=dataRectangle.height - dataRectangle.height * (srcRect.height - newSrcRect.height) / srcRect.height;
      dataRectangle=new Rectangle(dstX,dstY,dstWidth,dstHeight);
    }
 else {
      throw new TilerException("String_Node_Str");
    }
    srcRect=newSrcRect;
  }
  for (int b=0; b < bands.length; b++) {
    if (!srcRect.isEmpty()) {
      buffer.position(b * bandSize + dataRectangle.x + dataRectangle.y * width);
      ByteBuffer sliced=buffer.slice();
      int returnVal=bands[b].ReadRaster_Direct(srcRect.x,srcRect.y,srcRect.width,srcRect.height,dataRectangle.width,dataRectangle.height,bufferType,sliced,bufferTypeSize,bufferTypeSize * width);
      if (returnVal != gdalconstConstants.CE_None) {
        throw new GDALException();
      }
    }
  }
  if (bufferBandCount == 4 && dataBandCount != 4) {
    for (int y=0; y < height; y++) {
      for (int x=0; x < width; x++) {
        int index=getBufferIndex(x,y,3) * bufferTypeSize;
        for (int i=0; i < bufferTypeSize; i++) {
          if (dataRectangle.contains(x,y))           buffer.put(index + i,(byte)0xff);
 else           buffer.put(index + i,(byte)0x00);
        }
      }
    }
  }
  buffer.rewind();
  Band lastBand=bands[bands.length - 1];
  boolean indexed=lastBand.GetRasterColorInterpretation() == gdalconstConstants.GCI_PaletteIndex;
  indexColorModel=indexed ? lastBand.GetRasterColorTable().getIndexColorModel(dataTypeSize) : null;
}