Program: 168483
Project: 168483
Buggy source:
/** 
 * {@inheritDoc}
 */
public int read(byte[] data,int pos,int len){
  long offset=Long.MAX_VALUE;
  long bytesToSkip=logicalPosition;
  long curLogicalBlock=0;
  int extIndex;
  long currentExtentLength;
  if (extentDescriptors.size() < 1 || logicalPosition > forkLength) {
    return -1;
  }
  for (extIndex=0; ; ++extIndex) {
    CommonHFSExtentDescriptor cur=getExtent(extIndex,curLogicalBlock);
    if (cur == null) {
      return -1;
    }
    long currentBlockCount=cur.getBlockCount();
    currentExtentLength=currentBlockCount * allocationBlockSize;
    if (bytesToSkip >= currentExtentLength) {
      bytesToSkip-=currentExtentLength;
      curLogicalBlock+=currentBlockCount;
    }
 else {
      offset=fsOffset + firstBlockByteOffset + (cur.getStartBlock() * allocationBlockSize)+ bytesToSkip;
      break;
    }
  }
  if (logicalPosition != lastLogicalPos) {
    sourceFile.seek(offset);
  }
 else   if (sourceFile.getFilePointer() != lastPhysicalPos) {
    sourceFile.seek(lastPhysicalPos);
  }
  long bytesLeftInStream=forkLength - logicalPosition;
  int totalBytesToRead=bytesLeftInStream < len ? (int)bytesLeftInStream : len;
  int bytesLeftToRead=totalBytesToRead;
  for (; ; ++extIndex) {
    CommonHFSExtentDescriptor cur;
    try {
      cur=getExtent(extIndex,curLogicalBlock);
    }
 catch (    RuntimeException e) {
      if (bytesLeftToRead == totalBytesToRead) {
        throw e;
      }
 else {
        break;
      }
    }
    long blockCount=cur.getBlockCount();
    long bytesInExtent=blockCount * allocationBlockSize - bytesToSkip;
    int bytesToReadFromExtent=(bytesInExtent < bytesLeftToRead) ? (int)bytesInExtent : bytesLeftToRead;
    int bytesReadFromExtent=0;
    while (bytesReadFromExtent < bytesToReadFromExtent) {
      int bytesToRead=bytesToReadFromExtent - bytesReadFromExtent;
      int positionInArray=pos + (totalBytesToRead - bytesLeftToRead) + bytesReadFromExtent;
      int bytesRead=sourceFile.read(data,positionInArray,bytesToRead);
      if (bytesRead > 0)       bytesReadFromExtent+=bytesRead;
 else {
        lastPhysicalPos=sourceFile.getFilePointer();
        int totalBytesRead=positionInArray - pos;
        logicalPosition+=totalBytesRead;
        return totalBytesRead;
      }
    }
    bytesLeftToRead-=bytesReadFromExtent;
    bytesToSkip=0;
    curLogicalBlock+=blockCount;
    if (bytesLeftToRead == 0)     break;
  }
  lastPhysicalPos=sourceFile.getFilePointer();
  logicalPosition+=totalBytesToRead - bytesLeftToRead;
  if (bytesLeftToRead < totalBytesToRead) {
    int bytesRead=totalBytesToRead - bytesLeftToRead;
    return bytesRead;
  }
 else   return -1;
}

Fixed version (ground truth):
/** 
 * {@inheritDoc}
 */
public int read(byte[] data,int pos,int len){
  long offset=Long.MAX_VALUE;
  long bytesToSkip=logicalPosition;
  long curLogicalBlock=0;
  int extIndex;
  long currentExtentLength;
  if (extentDescriptors.size() < 1 || logicalPosition > forkLength) {
    return -1;
  }
  for (extIndex=0; ; ++extIndex) {
    CommonHFSExtentDescriptor cur=getExtent(extIndex,curLogicalBlock);
    if (cur == null) {
      return -1;
    }
    long currentBlockCount=cur.getBlockCount();
    currentExtentLength=currentBlockCount * allocationBlockSize;
    if (bytesToSkip >= currentExtentLength) {
      bytesToSkip-=currentExtentLength;
      curLogicalBlock+=currentBlockCount;
    }
 else {
      offset=fsOffset + firstBlockByteOffset + (cur.getStartBlock() * allocationBlockSize)+ bytesToSkip;
      break;
    }
  }
  if (logicalPosition != lastLogicalPos) {
    sourceFile.seek(offset);
  }
 else   if (sourceFile.getFilePointer() != lastPhysicalPos) {
    sourceFile.seek(lastPhysicalPos);
  }
  long bytesLeftInStream=forkLength - logicalPosition;
  int totalBytesToRead=bytesLeftInStream < len ? (int)bytesLeftInStream : len;
  int bytesLeftToRead=totalBytesToRead;
  for (; ; ++extIndex) {
    CommonHFSExtentDescriptor cur;
    try {
      cur=getExtent(extIndex,curLogicalBlock);
    }
 catch (    RuntimeException e) {
      if (bytesLeftToRead == totalBytesToRead) {
        throw e;
      }
 else {
        break;
      }
    }
    sourceFile.seek(fsOffset + firstBlockByteOffset + (cur.getStartBlock() * allocationBlockSize)+ bytesToSkip);
    long blockCount=cur.getBlockCount();
    long bytesInExtent=blockCount * allocationBlockSize - bytesToSkip;
    int bytesToReadFromExtent=(bytesInExtent < bytesLeftToRead) ? (int)bytesInExtent : bytesLeftToRead;
    int bytesReadFromExtent=0;
    while (bytesReadFromExtent < bytesToReadFromExtent) {
      int bytesToRead=bytesToReadFromExtent - bytesReadFromExtent;
      int positionInArray=pos + (totalBytesToRead - bytesLeftToRead) + bytesReadFromExtent;
      int bytesRead=sourceFile.read(data,positionInArray,bytesToRead);
      if (bytesRead > 0)       bytesReadFromExtent+=bytesRead;
 else {
        lastPhysicalPos=sourceFile.getFilePointer();
        int totalBytesRead=positionInArray - pos;
        logicalPosition+=totalBytesRead;
        return totalBytesRead;
      }
    }
    bytesLeftToRead-=bytesReadFromExtent;
    bytesToSkip=0;
    curLogicalBlock+=blockCount;
    if (bytesLeftToRead == 0)     break;
  }
  lastPhysicalPos=sourceFile.getFilePointer();
  logicalPosition+=totalBytesToRead - bytesLeftToRead;
  if (bytesLeftToRead < totalBytesToRead) {
    int bytesRead=totalBytesToRead - bytesLeftToRead;
    return bytesRead;
  }
 else   return -1;
}