Program: 91677
Project: 91677
Buggy source:
/** 
 * Returns an operation for conversion or transformation between two coordinate reference systems. This method extracts the authority code from the supplied   {@code sourceCRS} and {@code targetCRS}, and submit them to the   {@link #registry}. If no operation is found for those codes, then this method returns   {@code null}.
 * @param sourceCRS  source coordinate reference system.
 * @param targetCRS  target coordinate reference system.
 * @return a coordinate operation from {@code sourceCRS} to {@code targetCRS}, or   {@code null}if no such operation is explicitly defined in the underlying database.
 * @throws IllegalArgumentException if the coordinate systems are not of the same type or axes do not match.
 * @throws IncommensurableException if the units are not compatible or a unit conversion is non-linear.
 * @throws FactoryException if an error occurred while creating the operation.
 */
private CoordinateOperation search(final CoordinateReferenceSystem sourceCRS,final CoordinateReferenceSystem targetCRS) throws IllegalArgumentException, IncommensurableException, FactoryException {
  final String sourceID=findCode(sourceCRS);
  if (sourceID == null) {
    return null;
  }
  final String targetID=findCode(targetCRS);
  if (targetID == null) {
    return null;
  }
  if (sourceID.equals(targetID)) {
    return null;
  }
  final boolean inverse;
  Collection<CoordinateOperation> operations;
  boolean mdOnly=Semaphores.queryAndSet(Semaphores.METADATA_ONLY);
  try {
    try {
      operations=registry.createFromCoordinateReferenceSystemCodes(sourceID,targetID);
      inverse=Containers.isNullOrEmpty(operations);
      if (inverse) {
        operations=registry.createFromCoordinateReferenceSystemCodes(targetID,sourceID);
        if (Containers.isNullOrEmpty(operations)) {
          return null;
        }
      }
    }
  finally {
      if (!mdOnly) {
        Semaphores.clear(Semaphores.METADATA_ONLY);
      }
    }
  }
 catch (  NoSuchAuthorityCodeException|MissingFactoryResourceException e) {
    log(null,e);
    return null;
  }
  CoordinateOperation bestChoice;
  while (true) {
    bestChoice=null;
    double largestArea=0;
    double finestAccuracy=Double.POSITIVE_INFINITY;
    boolean stopAtFirstDeprecated=false;
    for (final Iterator<CoordinateOperation> it=operations.iterator(); ; ) {
      CoordinateOperation candidate;
      mdOnly=Semaphores.queryAndSet(Semaphores.METADATA_ONLY);
      try {
        try {
          if (!it.hasNext())           break;
          candidate=it.next();
        }
  finally {
          if (!mdOnly) {
            Semaphores.clear(Semaphores.METADATA_ONLY);
          }
        }
      }
 catch (      BackingStoreException exception) {
        throw exception.unwrapOrRethrow(FactoryException.class);
      }
      if (candidate != null) {
        final boolean isDeprecated=(candidate instanceof Deprecable) && ((Deprecable)candidate).isDeprecated();
        if (isDeprecated && stopAtFirstDeprecated) {
          break;
        }
        final double area=Extents.area(Extents.intersection(Extents.getGeographicBoundingBox(areaOfInterest),Extents.getGeographicBoundingBox(candidate.getDomainOfValidity())));
        if (bestChoice == null || area >= largestArea) {
          final double accuracy=CRS.getLinearAccuracy(candidate);
          if (bestChoice == null || area != largestArea || accuracy < finestAccuracy) {
            bestChoice=candidate;
            if (!Double.isNaN(area)) {
              largestArea=area;
            }
            finestAccuracy=Double.isNaN(accuracy) ? Double.POSITIVE_INFINITY : accuracy;
            stopAtFirstDeprecated=!isDeprecated;
          }
        }
      }
    }
    if (bestChoice == null)     break;
    final CoordinateOperation deferred=bestChoice;
    try {
      if (bestChoice instanceof DeferredCoordinateOperation) {
        bestChoice=((DeferredCoordinateOperation)bestChoice).create();
      }
      if (bestChoice instanceof SingleOperation && bestChoice.getMathTransform() == null) {
        bestChoice=fromDefiningConversion((SingleOperation)bestChoice,inverse ? targetCRS : sourceCRS,inverse ? sourceCRS : targetCRS);
        if (bestChoice == null) {
          return null;
        }
      }
      if (inverse) {
        bestChoice=inverse(bestChoice);
      }
    }
 catch (    NoninvertibleTransformException|MissingFactoryResourceException e) {
      boolean removed;
      try {
        removed=operations.remove(deferred);
      }
 catch (      UnsupportedOperationException ignored) {
        operations=new ArrayList<>(operations);
        removed=operations.remove(deferred);
      }
      if (removed) {
        log(null,e);
        continue;
      }
      throw (e instanceof FactoryException) ? (FactoryException)e : new FactoryException(e);
    }
    bestChoice=complete(bestChoice,sourceCRS,targetCRS);
    if (filter(bestChoice))     break;
  }
  return bestChoice;
}

Fixed version (ground truth):
/** 
 * Returns an operation for conversion or transformation between two coordinate reference systems. This method extracts the authority code from the supplied   {@code sourceCRS} and {@code targetCRS}, and submit them to the   {@link #registry}. If no operation is found for those codes, then this method returns   {@code null}.
 * @param sourceCRS  source coordinate reference system.
 * @param targetCRS  target coordinate reference system.
 * @return a coordinate operation from {@code sourceCRS} to {@code targetCRS}, or   {@code null}if no such operation is explicitly defined in the underlying database.
 * @throws IllegalArgumentException if the coordinate systems are not of the same type or axes do not match.
 * @throws IncommensurableException if the units are not compatible or a unit conversion is non-linear.
 * @throws FactoryException if an error occurred while creating the operation.
 */
private CoordinateOperation search(final CoordinateReferenceSystem sourceCRS,final CoordinateReferenceSystem targetCRS) throws IllegalArgumentException, IncommensurableException, FactoryException {
  final List<String> sources=findCode(sourceCRS);
  if (sources.isEmpty())   return null;
  final List<String> targets=findCode(targetCRS);
  if (targets.isEmpty())   return null;
  Collection<CoordinateOperation> operations=null;
  boolean inverse=false;
  for (  final String sourceID : sources) {
    for (    final String targetID : targets) {
      if (sourceID.equals(targetID)) {
        return null;
      }
      final boolean mdOnly=Semaphores.queryAndSet(Semaphores.METADATA_ONLY);
      try {
        try {
          operations=registry.createFromCoordinateReferenceSystemCodes(sourceID,targetID);
          inverse=Containers.isNullOrEmpty(operations);
          if (inverse) {
            operations=registry.createFromCoordinateReferenceSystemCodes(targetID,sourceID);
            if (Containers.isNullOrEmpty(operations)) {
              continue;
            }
          }
        }
  finally {
          if (!mdOnly) {
            Semaphores.clear(Semaphores.METADATA_ONLY);
          }
        }
      }
 catch (      NoSuchAuthorityCodeException|MissingFactoryResourceException e) {
        log(null,e);
        continue;
      }
      break;
    }
  }
  if (operations == null) {
    return null;
  }
  CoordinateOperation bestChoice;
  while (true) {
    bestChoice=null;
    double largestArea=0;
    double finestAccuracy=Double.POSITIVE_INFINITY;
    boolean stopAtFirstDeprecated=false;
    for (final Iterator<CoordinateOperation> it=operations.iterator(); ; ) {
      CoordinateOperation candidate;
      final boolean mdOnly=Semaphores.queryAndSet(Semaphores.METADATA_ONLY);
      try {
        try {
          if (!it.hasNext())           break;
          candidate=it.next();
        }
  finally {
          if (!mdOnly) {
            Semaphores.clear(Semaphores.METADATA_ONLY);
          }
        }
      }
 catch (      BackingStoreException exception) {
        throw exception.unwrapOrRethrow(FactoryException.class);
      }
      if (candidate != null) {
        final boolean isDeprecated=(candidate instanceof Deprecable) && ((Deprecable)candidate).isDeprecated();
        if (isDeprecated && stopAtFirstDeprecated) {
          break;
        }
        final double area=Extents.area(Extents.intersection(Extents.getGeographicBoundingBox(areaOfInterest),Extents.getGeographicBoundingBox(candidate.getDomainOfValidity())));
        if (bestChoice == null || area >= largestArea) {
          final double accuracy=CRS.getLinearAccuracy(candidate);
          if (bestChoice == null || area != largestArea || accuracy < finestAccuracy) {
            bestChoice=candidate;
            if (!Double.isNaN(area)) {
              largestArea=area;
            }
            finestAccuracy=Double.isNaN(accuracy) ? Double.POSITIVE_INFINITY : accuracy;
            stopAtFirstDeprecated=!isDeprecated;
          }
        }
      }
    }
    if (bestChoice == null)     break;
    final CoordinateOperation deferred=bestChoice;
    try {
      if (bestChoice instanceof DeferredCoordinateOperation) {
        bestChoice=((DeferredCoordinateOperation)bestChoice).create();
      }
      if (bestChoice instanceof SingleOperation && bestChoice.getMathTransform() == null) {
        bestChoice=fromDefiningConversion((SingleOperation)bestChoice,inverse ? targetCRS : sourceCRS,inverse ? sourceCRS : targetCRS);
        if (bestChoice == null) {
          return null;
        }
      }
      if (inverse) {
        bestChoice=inverse(bestChoice);
      }
    }
 catch (    NoninvertibleTransformException|MissingFactoryResourceException e) {
      boolean removed;
      try {
        removed=operations.remove(deferred);
      }
 catch (      UnsupportedOperationException ignored) {
        operations=new ArrayList<>(operations);
        removed=operations.remove(deferred);
      }
      if (removed) {
        log(null,e);
        continue;
      }
      throw (e instanceof FactoryException) ? (FactoryException)e : new FactoryException(e);
    }
    bestChoice=complete(bestChoice,sourceCRS,targetCRS);
    if (filter(bestChoice))     break;
  }
  return bestChoice;
}