Program: 173188
Project: 173188
Buggy source:
/** 
 * Serialize a message in buffers.
 * @param message
 * @param state
 * @param targetBuffer
 * @return the number of complete messages written
 */
private boolean serializeBufferedMessage(MPIMessage message,SerializeState state,MPIBuffer targetBuffer){
  ByteBuffer targetByteBuffer=targetBuffer.getByteBuffer();
  byte[] tempBytes=new byte[targetBuffer.getCapacity()];
  int targetRemainingSpace=targetByteBuffer.remaining();
  int currentSourceBuffer=state.getBufferNo();
  int bytesCopiedFromSource=state.getBytesCopied();
  int canCopy=0;
  int needsCopy=0;
  List<MPIBuffer> buffers=message.getBuffers();
  MPIBuffer currentMPIBuffer=null;
  int totalBytes=state.getTotalBytes();
  while (targetRemainingSpace > 0 && currentSourceBuffer < buffers.size()) {
    currentMPIBuffer=buffers.get(currentSourceBuffer);
    ByteBuffer currentSourceByteBuffer=currentMPIBuffer.getByteBuffer();
    if (currentSourceBuffer == 0 && bytesCopiedFromSource == 0) {
      bytesCopiedFromSource+=16;
    }
    needsCopy=currentMPIBuffer.getSize() - bytesCopiedFromSource;
    currentSourceByteBuffer.position(bytesCopiedFromSource);
    canCopy=needsCopy > targetRemainingSpace ? targetRemainingSpace : needsCopy;
    currentSourceByteBuffer.get(tempBytes,0,canCopy);
    targetByteBuffer.put(tempBytes,0,canCopy);
    totalBytes+=canCopy;
    targetRemainingSpace-=canCopy;
    bytesCopiedFromSource+=canCopy;
    if (targetRemainingSpace < 6) {
      if (canCopy == needsCopy) {
        currentSourceBuffer++;
        bytesCopiedFromSource=0;
      }
      break;
    }
    currentSourceBuffer++;
    bytesCopiedFromSource=0;
  }
  targetBuffer.setSize(targetByteBuffer.position());
  state.setTotalBytes(totalBytes);
  if (currentSourceBuffer == buffers.size() && currentMPIBuffer != null) {
    state.setBufferNo(0);
    state.setBytesCopied(0);
    return true;
  }
 else {
    state.setBufferNo(currentSourceBuffer);
    state.setBytesCopied(bytesCopiedFromSource);
    return false;
  }
}

Fixed version (ground truth):
/** 
 * Serialize a message in buffers.
 * @param message
 * @param state
 * @param targetBuffer
 * @return the number of complete messages written
 */
private boolean serializeBufferedMessage(MPIMessage message,SerializeState state,MPIBuffer targetBuffer){
  ByteBuffer targetByteBuffer=targetBuffer.getByteBuffer();
  byte[] tempBytes=new byte[targetBuffer.getCapacity()];
  int targetRemainingSpace=targetByteBuffer.remaining();
  int currentSourceBuffer=state.getBufferNo();
  int bytesCopiedFromSource=state.getBytesCopied();
  int canCopy=0;
  int needsCopy=0;
  List<MPIBuffer> buffers=message.getBuffers();
  MPIBuffer currentMPIBuffer=null;
  int totalBytes=state.getTotalBytes();
  while (targetRemainingSpace > 0 && currentSourceBuffer < buffers.size()) {
    currentMPIBuffer=buffers.get(currentSourceBuffer);
    ByteBuffer currentSourceByteBuffer=currentMPIBuffer.getByteBuffer();
    if (currentSourceBuffer == 0 && bytesCopiedFromSource == 0) {
      bytesCopiedFromSource+=16;
    }
    needsCopy=currentMPIBuffer.getSize() - bytesCopiedFromSource;
    currentSourceByteBuffer.position(bytesCopiedFromSource);
    canCopy=needsCopy > targetRemainingSpace ? targetRemainingSpace : needsCopy;
    currentSourceByteBuffer.get(tempBytes,0,canCopy);
    targetByteBuffer.put(tempBytes,0,canCopy);
    totalBytes+=canCopy;
    targetRemainingSpace-=canCopy;
    bytesCopiedFromSource+=canCopy;
    if (targetRemainingSpace < 6) {
      if (canCopy == needsCopy) {
        currentSourceBuffer++;
        bytesCopiedFromSource=0;
      }
      break;
    }
    currentSourceBuffer++;
    bytesCopiedFromSource=0;
  }
  targetBuffer.setSize(targetByteBuffer.position());
  state.setTotalBytes(totalBytes);
  if (currentSourceBuffer == buffers.size() && currentMPIBuffer != null) {
    state.setBufferNo(0);
    state.setBytesCopied(0);
    message.release();
    return true;
  }
 else {
    state.setBufferNo(currentSourceBuffer);
    state.setBytesCopied(bytesCopiedFromSource);
    return false;
  }
}