Program: 92189
Project: 92189
Buggy source:
/** 
 * Given a transformation chain, replaces the elements around   {@code transforms.get(index)} transform byalternative objects to use when formatting WKT. The replacement is performed in-place in the given list. <p>This method shall replace only the previous element and the few next elements that need to be changed as a result of the previous change. This method is not expected to continue the iteration after the changes that are of direct concern to this object.</p> <p>This method is invoked (indirectly) only by  {@link ConcatenatedTransform#getPseudoSteps()} in orderto get the  {@link ParameterValueGroup} of a map projection, or to format a {@code ProjectedCRS} WKT.</p>
 * @param transforms The full chain of concatenated transforms.
 * @param index      The index of this transform in the {@code transforms} chain.
 * @param inverse    Always {@code false}, except if we are formatting the inverse transform.
 * @return Index of the last transform processed. Iteration should continue at that index + 1.
 * @see ConcatenatedTransform#getPseudoSteps()
 * @see AbstractMathTransform#beforeFormat(List,int,boolean)
 */
final int beforeFormat(final List<Object> transforms,int index,final boolean inverse){
  Matrix before=null;
  Matrix after=null;
  if (index != 0) {
    final Object candidate=transforms.get(index - 1);
    if (candidate instanceof MathTransform) {
      before=MathTransforms.getMatrix((MathTransform)candidate);
    }
  }
  if (index + 1 < transforms.size()) {
    final Object candidate=transforms.get(index + 1);
    if (candidate instanceof MathTransform) {
      after=MathTransforms.getMatrix((MathTransform)candidate);
    }
  }
  final boolean hasBefore=(before != null);
  final boolean hasAfter=(after != null);
  Matrix userDefined=inverse ? denormalize : normalize;
  if (!inverse)   try {
    userDefined=Matrices.inverse(userDefined);
  }
 catch (  NoninvertibleMatrixException e) {
    unexpectedException(e);
    return index;
  }
  if (hasBefore) {
    userDefined=Matrices.multiply(userDefined,before);
  }
  before=userDefined.isIdentity() ? null : userDefined;
  userDefined=inverse ? normalize : denormalize;
  if (!inverse)   try {
    userDefined=Matrices.inverse(userDefined);
  }
 catch (  NoninvertibleMatrixException e) {
    unexpectedException(e);
    return index;
  }
  if (hasAfter) {
    userDefined=Matrices.multiply(after,userDefined);
  }
  after=userDefined.isIdentity() ? null : userDefined;
  if (before == null) {
    if (hasBefore) {
      final Object old=transforms.remove(--index);
      assert(old instanceof LinearTransform);
    }
  }
 else {
    if (hasBefore) {
      final Object old=transforms.set(index - 1,before);
      assert(old instanceof LinearTransform);
    }
 else {
      transforms.add(index++,before);
    }
  }
  transforms.set(index,inverse ? new InverseWKT() : this);
  if (after == null) {
    if (hasAfter) {
      final Object old=transforms.remove(index + 1);
      assert(old instanceof LinearTransform);
    }
  }
 else {
    index++;
    if (hasAfter) {
      final Object old=transforms.set(index,after);
      assert(old instanceof LinearTransform);
    }
 else {
      transforms.add(index,after);
    }
  }
  return index;
}

Fixed version (ground truth):
/** 
 * Given a transformation chain, replaces the elements around   {@code transforms.get(index)} transform byalternative objects to use when formatting WKT. The replacement is performed in-place in the given list. <p>This method shall replace only the previous element and the few next elements that need to be changed as a result of the previous change. This method is not expected to continue the iteration after the changes that are of direct concern to this object.</p> <p>This method is invoked (indirectly) only by  {@link ConcatenatedTransform#getPseudoSteps()} in orderto get the  {@link ParameterValueGroup} of a map projection, or to format a {@code ProjectedCRS} WKT.</p>
 * @param transforms The full chain of concatenated transforms.
 * @param index      The index of this transform in the {@code transforms} chain.
 * @param inverse    Always {@code false}, except if we are formatting the inverse transform.
 * @return Index of the last transform processed. Iteration should continue at that index + 1.
 * @see ConcatenatedTransform#getPseudoSteps()
 * @see AbstractMathTransform#beforeFormat(List,int,boolean)
 */
final int beforeFormat(final List<Object> transforms,int index,final boolean inverse){
  Matrix before=null;
  Matrix after=null;
  if (index != 0) {
    final Object candidate=transforms.get(index - 1);
    if (candidate instanceof MathTransform) {
      before=MathTransforms.getMatrix((MathTransform)candidate);
    }
  }
  if (index + 1 < transforms.size()) {
    final Object candidate=transforms.get(index + 1);
    if (candidate instanceof MathTransform) {
      after=MathTransforms.getMatrix((MathTransform)candidate);
    }
  }
  final boolean hasBefore=(before != null);
  final boolean hasAfter=(after != null);
  Matrix userDefined=inverse ? denormalize : normalize;
  if (!inverse)   try {
    userDefined=Matrices.inverse(userDefined);
  }
 catch (  NoninvertibleMatrixException e) {
    unexpectedException(e);
    return index;
  }
  if (hasBefore) {
    userDefined=Matrices.multiply(userDefined,before);
  }
  before=userDefined.isIdentity() ? null : userDefined;
  userDefined=inverse ? normalize : denormalize;
  if (!inverse)   try {
    userDefined=Matrices.inverse(userDefined);
  }
 catch (  NoninvertibleMatrixException e) {
    unexpectedException(e);
    return index;
  }
  if (hasAfter) {
    userDefined=Matrices.multiply(after,userDefined);
  }
  after=userDefined.isIdentity() ? null : userDefined;
  if (before == null) {
    if (hasBefore) {
      final Object old=transforms.remove(--index);
      assert(old instanceof LinearTransform);
    }
  }
 else {
    if (hasBefore) {
      final Object old=transforms.set(index - 1,before);
      assert(old instanceof LinearTransform);
    }
 else {
      transforms.add(index++,before);
    }
  }
  transforms.set(index,new WKT(inverse));
  if (after == null) {
    if (hasAfter) {
      final Object old=transforms.remove(index + 1);
      assert(old instanceof LinearTransform);
    }
  }
 else {
    index++;
    if (hasAfter) {
      final Object old=transforms.set(index,after);
      assert(old instanceof LinearTransform);
    }
 else {
      transforms.add(index,after);
    }
  }
  return index;
}