Program: 91724
Project: 91724
Buggy source:
/** 
 * Returns a new iterator for creating MGRS codes in a single UTM or UPS zone.
 * @param areaOfInterest  the envelope for which to return MGRS codes. This envelope can be in any CRS.
 * @param geographicArea  the area of interest transformed into a normalized geographic CRS.
 * @param sourceCRS       the horizontal part of the {@code areaOfInterest} CRS.
 * @param targetCRS       the UTM or UPS projected CRS of the zone for which to create MGRS references.
 * @param step            the amount of metres to add or subtract to grid coordinates during iteration.
 */
IteratorOneZone(final Coder coder,Rectangle2D areaOfInterest,final Envelope geographicArea,final SingleCRS sourceCRS,final ProjectedCRS targetCRS,final int step) throws FactoryException, TransformException {
  super(coder);
  this.areaOfInterest=areaOfInterest;
  this.encoder=encoder(targetCRS);
  this.step=step;
  this.cell=new IntervalRectangle();
  final int zone=Math.abs(encoder.crsZone);
  if (zone == Encoder.POLE) {
    if (encoder.crsZone < 0) {
      cell.ymin=Latitude.MIN_VALUE;
      cell.ymax=TransverseMercator.Zoner.SOUTH_BOUNDS;
    }
 else {
      cell.ymin=TransverseMercator.Zoner.NORTH_BOUNDS;
      cell.ymax=Latitude.MAX_VALUE;
    }
    cell.xmin=Longitude.MIN_VALUE;
    cell.xmax=Longitude.MAX_VALUE;
  }
 else {
    if (encoder.crsZone < 0) {
      cell.ymin=TransverseMercator.Zoner.SOUTH_BOUNDS;
    }
 else {
      cell.ymax=TransverseMercator.Zoner.NORTH_BOUNDS;
    }
    final double λ0=ZONER.centralMeridian(zone);
    cell.xmin=λ0 - ZONER.width;
    cell.xmax=λ0 + ZONER.width;
  }
  double t;
  boolean clip=false;
  if ((t=geographicArea.getMinimum(1)) >= cell.ymin)   cell.ymin=t;
 else   clip=true;
  if ((t=geographicArea.getMaximum(1)) <= cell.ymax)   cell.ymax=t;
 else   clip=true;
  if ((t=geographicArea.getMinimum(0)) >= cell.xmin)   cell.xmin=t;
 else   clip=true;
  if ((t=geographicArea.getMaximum(0)) <= cell.xmax)   cell.xmax=t;
 else   clip=true;
  if (clip) {
    final Rectangle2D bounds=Shapes2D.transform(CRS.findOperation(geographicArea.getCoordinateReferenceSystem(),sourceCRS,null),cell,cell);
    areaOfInterest=bounds.createIntersection(areaOfInterest);
  }
  final MathTransform2D op=(MathTransform2D)CRS.findOperation(sourceCRS,targetCRS,null).getMathTransform();
  final Rectangle2D bounds=Shapes2D.transform(op,areaOfInterest,null);
  gridX=(((int)(bounds.getMinX() / step))) * step;
  gridY=(((int)(bounds.getMinY() / step))) * step;
  xEnd=(((int)(bounds.getMaxX() / step)) + 1) * step;
  yEnd=(((int)(bounds.getMaxY() / step)) + 1) * step;
  xStart=gridX;
  if (encoder.crsZone < 0) {
    final int y=gridY;
    gridY=yEnd - step;
    yEnd=y - step;
  }
  gridToAOI=op.inverse();
}

Fixed version (ground truth):
/** 
 * Returns a new iterator for creating MGRS codes in a single UTM or UPS zone.
 * @param areaOfInterest  the envelope for which to return MGRS codes. This envelope can be in any CRS.
 * @param geographicArea  the area of interest transformed into a normalized geographic CRS.
 * @param sourceCRS       the horizontal part of the {@code areaOfInterest} CRS.
 * @param targetCRS       the UTM or UPS projected CRS of the zone for which to create MGRS references.
 * @param step            the amount of metres to add or subtract to grid coordinates during iteration.
 */
IteratorOneZone(final Coder coder,Rectangle2D areaOfInterest,final Envelope geographicArea,final SingleCRS sourceCRS,final ProjectedCRS targetCRS,final int step) throws FactoryException, TransformException {
  super(coder);
  this.areaOfInterest=areaOfInterest;
  this.encoder=encoder(targetCRS);
  this.step=step;
  this.cell=new IntervalRectangle();
  final int zone=Math.abs(encoder.crsZone);
  if (zone == Encoder.POLE) {
    xCenter=PolarStereographicA.UPS_SHIFT;
    if (encoder.crsZone < 0) {
      cell.ymin=Latitude.MIN_VALUE;
      cell.ymax=TransverseMercator.Zoner.SOUTH_BOUNDS;
    }
 else {
      cell.ymin=TransverseMercator.Zoner.NORTH_BOUNDS;
      cell.ymax=Latitude.MAX_VALUE;
    }
    cell.xmin=Longitude.MIN_VALUE;
    cell.xmax=Longitude.MAX_VALUE;
  }
 else {
    xCenter=(int)ZONER.easting;
    if (encoder.crsZone < 0) {
      cell.ymin=TransverseMercator.Zoner.SOUTH_BOUNDS;
    }
 else {
      cell.ymax=TransverseMercator.Zoner.NORTH_BOUNDS;
    }
    final double λ0=ZONER.centralMeridian(zone);
    cell.xmin=λ0 - ZONER.width;
    cell.xmax=λ0 + ZONER.width;
  }
  double t;
  boolean clip=false;
  if ((t=geographicArea.getMinimum(1)) >= cell.ymin)   cell.ymin=t;
 else   clip=true;
  if ((t=geographicArea.getMaximum(1)) <= cell.ymax)   cell.ymax=t;
 else   clip=true;
  if ((t=geographicArea.getMinimum(0)) >= cell.xmin)   cell.xmin=t;
 else   clip=true;
  if ((t=geographicArea.getMaximum(0)) <= cell.xmax)   cell.xmax=t;
 else   clip=true;
  boolean isSpecialCase=ZONER.isSpecialCase(cell.ymin,cell.ymax,cell.xmin,cell.xmax);
  if (clip) {
    final Rectangle2D bounds=Shapes2D.transform(CRS.findOperation(geographicArea.getCoordinateReferenceSystem(),sourceCRS,null),cell,cell);
    areaOfInterest=bounds.createIntersection(areaOfInterest);
  }
  final MathTransform2D op=(MathTransform2D)CRS.findOperation(sourceCRS,targetCRS,null).getMathTransform();
  final Rectangle2D bounds=Shapes2D.transform(op,areaOfInterest,null);
  gridX=(((int)(bounds.getMinX() / step))) * step;
  gridY=(((int)(bounds.getMinY() / step))) * step;
  xEnd=(((int)(bounds.getMaxX() / step)) + 1) * step;
  yEnd=(((int)(bounds.getMaxY() / step)) + 1) * step;
  xStart=gridX;
  if (zone != Encoder.POLE) {
    downward=(encoder.crsZone < 0);
  }
 else {
    downward=yEnd <= PolarStereographicA.UPS_SHIFT;
    if (!downward && gridY < PolarStereographicA.UPS_SHIFT) {
      isSpecialCase=true;
    }
  }
  if (downward) {
    final int y=gridY;
    gridY=yEnd - step;
    yEnd=y - step;
  }
  optimize=!isSpecialCase && Utilities.equalsIgnoreMetadata(geographicArea.getCoordinateReferenceSystem(),sourceCRS);
  gridToAOI=op.inverse();
}