Program: 30485
Project: 30485
Buggy source:
/** 
 * Gets the subsequence of the contig in the range [start,stop]
 * @param contig Contig whose subsequence to retrieve.
 * @param start inclusive, 1-based start of region.
 * @param stop inclusive, 1-based stop of region.
 * @return The partial reference sequence associated with this range.
 */
public ReferenceSequence getSubsequenceAt(String contig,long start,long stop){
  if (start > stop + 1)   throw new SAMException(String.format("String_Node_Str",start,stop));
  FastaSequenceIndexEntry indexEntry=index.getIndexEntry(contig);
  if (stop > indexEntry.getSize())   throw new SAMException("String_Node_Str");
  int length=(int)(stop - start + 1);
  byte[] target=new byte[length];
  ByteBuffer targetBuffer=ByteBuffer.wrap(target);
  final int basesPerLine=indexEntry.getBasesPerLine();
  final int bytesPerLine=indexEntry.getBytesPerLine();
  final int terminatorLength=bytesPerLine - basesPerLine;
  long startOffset=((start - 1) / basesPerLine) * bytesPerLine + (start - 1) % basesPerLine;
  final long minBufferSize=Math.min((long)Defaults.NON_ZERO_BUFFER_SIZE,(long)(length % basesPerLine + 2) * (long)bytesPerLine);
  if (minBufferSize > Integer.MAX_VALUE)   throw new SAMException("String_Node_Str" + minBufferSize);
  final ByteBuffer channelBuffer=ByteBuffer.allocate((int)minBufferSize);
  while (targetBuffer.position() < length) {
    startOffset+=Math.max((int)(startOffset % bytesPerLine - basesPerLine + 1),0);
    try {
      startOffset+=readFromPosition(channel,channelBuffer,indexEntry.getLocation() + startOffset);
    }
 catch (    IOException ex) {
      throw new SAMException("String_Node_Str" + contig + "String_Node_Str"+ start+ "String_Node_Str"+ stop+ "String_Node_Str"+ getAbsolutePath(),ex);
    }
    channelBuffer.flip();
    final int positionInContig=(int)start - 1 + targetBuffer.position();
    final int nextBaseSpan=Math.min(basesPerLine - positionInContig % basesPerLine,length - targetBuffer.position());
    int bytesToTransfer=Math.min(nextBaseSpan,channelBuffer.capacity());
    channelBuffer.limit(channelBuffer.position() + bytesToTransfer);
    while (channelBuffer.hasRemaining()) {
      targetBuffer.put(channelBuffer);
      bytesToTransfer=Math.min(basesPerLine,length - targetBuffer.position());
      channelBuffer.limit(Math.min(channelBuffer.position() + bytesToTransfer + terminatorLength,channelBuffer.capacity()));
      channelBuffer.position(Math.min(channelBuffer.position() + terminatorLength,channelBuffer.capacity()));
    }
    channelBuffer.flip();
  }
  return new ReferenceSequence(contig,indexEntry.getSequenceIndex(),target);
}

Fixed version (ground truth):
/** 
 * Gets the subsequence of the contig in the range [start,stop]
 * @param contig Contig whose subsequence to retrieve.
 * @param start inclusive, 1-based start of region.
 * @param stop inclusive, 1-based stop of region.
 * @return The partial reference sequence associated with this range.
 */
public ReferenceSequence getSubsequenceAt(String contig,long start,long stop){
  if (start > stop + 1)   throw new SAMException(String.format("String_Node_Str",start,stop));
  FastaSequenceIndexEntry indexEntry=index.getIndexEntry(contig);
  if (stop > indexEntry.getSize())   throw new SAMException("String_Node_Str");
  int length=(int)(stop - start + 1);
  byte[] target=new byte[length];
  ByteBuffer targetBuffer=ByteBuffer.wrap(target);
  final int basesPerLine=indexEntry.getBasesPerLine();
  final int bytesPerLine=indexEntry.getBytesPerLine();
  final int terminatorLength=bytesPerLine - basesPerLine;
  long startOffset=((start - 1) / basesPerLine) * bytesPerLine + (start - 1) % basesPerLine;
  final long minBufferSize=Math.min((long)Defaults.NON_ZERO_BUFFER_SIZE,(long)(length / basesPerLine + 2) * (long)bytesPerLine);
  if (minBufferSize > Integer.MAX_VALUE)   throw new SAMException("String_Node_Str" + minBufferSize);
  final ByteBuffer channelBuffer=ByteBuffer.allocate((int)minBufferSize);
  while (targetBuffer.position() < length) {
    startOffset+=Math.max((int)(startOffset % bytesPerLine - basesPerLine + 1),0);
    try {
      startOffset+=readFromPosition(channel,channelBuffer,indexEntry.getLocation() + startOffset);
    }
 catch (    IOException ex) {
      throw new SAMException("String_Node_Str" + contig + "String_Node_Str"+ start+ "String_Node_Str"+ stop+ "String_Node_Str"+ getAbsolutePath(),ex);
    }
    channelBuffer.flip();
    final int positionInContig=(int)start - 1 + targetBuffer.position();
    final int nextBaseSpan=Math.min(basesPerLine - positionInContig % basesPerLine,length - targetBuffer.position());
    int bytesToTransfer=Math.min(nextBaseSpan,channelBuffer.capacity());
    channelBuffer.limit(channelBuffer.position() + bytesToTransfer);
    while (channelBuffer.hasRemaining()) {
      targetBuffer.put(channelBuffer);
      bytesToTransfer=Math.min(basesPerLine,length - targetBuffer.position());
      channelBuffer.limit(Math.min(channelBuffer.position() + bytesToTransfer + terminatorLength,channelBuffer.capacity()));
      channelBuffer.position(Math.min(channelBuffer.position() + terminatorLength,channelBuffer.capacity()));
    }
    channelBuffer.flip();
  }
  return new ReferenceSequence(contig,indexEntry.getSequenceIndex(),target);
}