Program: 32689
Project: 32689
Buggy source:
protected VPackBuilder close(final boolean sort) throws VPackBuilderNeedOpenCompoundException, VPackKeyTypeException, VPackNeedAttributeTranslatorException {
  if (isClosed()) {
    throw new VPackBuilderNeedOpenCompoundException();
  }
  final byte head=head();
  final boolean isArray=head == 0x06 || head == 0x13;
  final List<Integer> in=index.get(stack.size() - 1);
  final int tos=stack.get(stack.size() - 1);
  if (in.isEmpty()) {
    return closeEmptyArrayOrObject(tos,isArray);
  }
  if (head == 0x13 || head == 0x14 || (head == 0x06 && options.isBuildUnindexedArrays()) || head == 0x0b && (options.isBuildUnindexedObjects() || in.size() == 1)) {
    if (closeCompactArrayOrObject(tos,isArray,in)) {
      return this;
    }
  }
  if (isArray) {
    return closeArray(tos,in);
  }
  buffer[tos]=(byte)0x0b;
  final int offsetSize;
  if (size - tos + in.size() - 6 <= 0xff) {
    offsetSize=1;
  }
 else   if ((size - 1 - tos) + 2 * in.size() <= 0xffff) {
    offsetSize=2;
  }
 else   if (((size - 1 - tos) / 2) + 4 * in.size() / 2 <= Integer.MAX_VALUE) {
    offsetSize=4;
  }
 else {
    offsetSize=8;
  }
  if (offsetSize == 1) {
    final int targetPos=3;
    if ((size - 1) > (tos + 9)) {
      for (int i=tos + targetPos; i < tos + 9; i++) {
        remove(tos + targetPos);
      }
    }
    final int diff=9 - targetPos;
    final int n=in.size();
    for (int i=0; i < n; i++) {
      in.set(i,in.get(i) - diff);
    }
  }
  if (sort && in.size() >= 2) {
    sortObjectIndex(tos,in);
  }
  for (int i=0; i < in.size(); i++) {
    long x=in.get(i);
    ensureCapacity(size + offsetSize);
    for (int j=0; j < offsetSize; j++) {
      addUnchecked((byte)(x & 0xff));
      x>>=8;
    }
  }
  if (offsetSize > 1) {
    if (offsetSize == 2) {
      buffer[tos]=(byte)(buffer[tos] + 1);
    }
 else     if (offsetSize == 4) {
      buffer[tos]=(byte)(buffer[tos] + 2);
    }
 else {
      buffer[tos]=(byte)(buffer[tos] + 3);
      appendLength(in.size());
    }
  }
  long x=size - tos;
  for (int i=1; i <= offsetSize; i++) {
    buffer[tos + i]=(byte)(x & 0xff);
    x>>=8;
  }
  if (offsetSize < 8) {
    x=in.size();
    for (int i=offsetSize + 1; i <= 2 * offsetSize; i++) {
      buffer[tos + i]=(byte)(x & 0xff);
      x>>=8;
    }
  }
  stack.remove(stack.size() - 1);
  return this;
}

Fixed version (ground truth):
protected VPackBuilder close(final boolean sort) throws VPackBuilderNeedOpenCompoundException, VPackKeyTypeException, VPackNeedAttributeTranslatorException {
  if (isClosed()) {
    throw new VPackBuilderNeedOpenCompoundException();
  }
  final byte head=head();
  final boolean isArray=head == 0x06 || head == 0x13;
  final List<Integer> in=index.get(stack.size() - 1);
  final int tos=stack.get(stack.size() - 1);
  if (in.isEmpty()) {
    return closeEmptyArrayOrObject(tos,isArray);
  }
  if (head == 0x13 || head == 0x14 || (head == 0x06 && options.isBuildUnindexedArrays()) || head == 0x0b && (options.isBuildUnindexedObjects() || in.size() == 1)) {
    if (closeCompactArrayOrObject(tos,isArray,in)) {
      return this;
    }
  }
  if (isArray) {
    return closeArray(tos,in);
  }
  buffer[tos]=(byte)0x0b;
  final int offsetSize;
  if (size - tos + in.size() - 6 <= 0xff) {
    offsetSize=1;
  }
 else   if ((size - tos) + 2 * in.size() <= 0xffff) {
    offsetSize=2;
  }
 else   if (((size - tos) / 2) + 4 * in.size() / 2 <= Integer.MAX_VALUE) {
    offsetSize=4;
  }
 else {
    offsetSize=8;
  }
  if (offsetSize == 1) {
    final int targetPos=3;
    if ((size - 1) > (tos + 9)) {
      for (int i=tos + targetPos; i < tos + 9; i++) {
        remove(tos + targetPos);
      }
    }
    final int diff=9 - targetPos;
    final int n=in.size();
    for (int i=0; i < n; i++) {
      in.set(i,in.get(i) - diff);
    }
  }
  if (sort && in.size() >= 2) {
    sortObjectIndex(tos,in);
  }
  for (int i=0; i < in.size(); i++) {
    long x=in.get(i);
    ensureCapacity(size + offsetSize);
    for (int j=0; j < offsetSize; j++) {
      addUnchecked((byte)(x & 0xff));
      x>>=8;
    }
  }
  if (offsetSize > 1) {
    if (offsetSize == 2) {
      buffer[tos]=(byte)(buffer[tos] + 1);
    }
 else     if (offsetSize == 4) {
      buffer[tos]=(byte)(buffer[tos] + 2);
    }
 else {
      buffer[tos]=(byte)(buffer[tos] + 3);
      appendLength(in.size());
    }
  }
  long x=size - tos;
  for (int i=1; i <= offsetSize; i++) {
    buffer[tos + i]=(byte)(x & 0xff);
    x>>=8;
  }
  if (offsetSize < 8) {
    x=in.size();
    for (int i=offsetSize + 1; i <= 2 * offsetSize; i++) {
      buffer[tos + i]=(byte)(x & 0xff);
      x>>=8;
    }
  }
  stack.remove(stack.size() - 1);
  return this;
}