Program: 89139
Project: 89139
Buggy source:
@Override public List<CapacityVO> listCapacities(ListCapacityCmd cmd){
  Integer capacityType=cmd.getType();
  Long zoneId=cmd.getZoneId();
  Long podId=cmd.getPodId();
  Long clusterId=cmd.getClusterId();
  Boolean fetchLatest=cmd.getFetchLatest();
  zoneId=_accountMgr.checkAccessAndSpecifyAuthority(UserContext.current().getCaller(),zoneId);
  if (fetchLatest != null && fetchLatest) {
    _alertMgr.recalculateCapacity();
  }
  List<SummedCapacity> summedCapacities=_capacityDao.findCapacityBy(capacityType,zoneId,podId,clusterId);
  List<CapacityVO> capacities=new ArrayList<CapacityVO>();
  for (  SummedCapacity summedCapacity : summedCapacities) {
    CapacityVO capacity=new CapacityVO(null,summedCapacity.getDataCenterId(),podId,clusterId,summedCapacity.getUsedCapacity() + summedCapacity.getReservedCapacity(),summedCapacity.getTotalCapacity(),summedCapacity.getCapacityType());
    if (summedCapacity.getCapacityType() == Capacity.CAPACITY_TYPE_CPU) {
      capacity.setTotalCapacity((long)(summedCapacity.getTotalCapacity() * ApiDBUtils.getCpuOverprovisioningFactor()));
    }
    capacities.add(capacity);
  }
  List<DataCenterVO> dcList=new ArrayList<DataCenterVO>();
  if (zoneId == null && podId == null && clusterId == null) {
    dcList=ApiDBUtils.listZones();
  }
 else   if (zoneId != null) {
    dcList.add(ApiDBUtils.findZoneById(zoneId));
  }
 else {
    if (capacityType == null || capacityType == Capacity.CAPACITY_TYPE_STORAGE) {
      capacities.add(_storageMgr.getStoragePoolUsedStats(null,clusterId,podId,zoneId));
    }
  }
  for (  DataCenterVO zone : dcList) {
    zoneId=zone.getId();
    if ((capacityType == null || capacityType == Capacity.CAPACITY_TYPE_SECONDARY_STORAGE) && podId == null && clusterId == null) {
      capacities.add(_storageMgr.getSecondaryStorageUsedStats(null,zoneId));
    }
    if (capacityType == null || capacityType == Capacity.CAPACITY_TYPE_STORAGE) {
      capacities.add(_storageMgr.getStoragePoolUsedStats(null,clusterId,podId,zoneId));
    }
  }
  return capacities;
}

Fixed version (ground truth):
@Override public List<CapacityVO> listCapacities(ListCapacityCmd cmd){
  Integer capacityType=cmd.getType();
  Long zoneId=cmd.getZoneId();
  Long podId=cmd.getPodId();
  Long clusterId=cmd.getClusterId();
  Boolean fetchLatest=cmd.getFetchLatest();
  zoneId=_accountMgr.checkAccessAndSpecifyAuthority(UserContext.current().getCaller(),zoneId);
  if (fetchLatest != null && fetchLatest) {
    _alertMgr.recalculateCapacity();
  }
  List<SummedCapacity> summedCapacities=_capacityDao.findCapacityBy(capacityType,zoneId,podId,clusterId);
  List<CapacityVO> capacities=new ArrayList<CapacityVO>();
  for (  SummedCapacity summedCapacity : summedCapacities) {
    CapacityVO capacity=new CapacityVO(null,summedCapacity.getDataCenterId(),podId,clusterId,summedCapacity.getUsedCapacity() + summedCapacity.getReservedCapacity(),summedCapacity.getTotalCapacity(),summedCapacity.getCapacityType());
    if (summedCapacity.getCapacityType() == Capacity.CAPACITY_TYPE_CPU) {
      capacity.setTotalCapacity((long)(summedCapacity.getTotalCapacity() * ApiDBUtils.getCpuOverprovisioningFactor()));
    }
    capacities.add(capacity);
  }
  List<DataCenterVO> dcList=new ArrayList<DataCenterVO>();
  if (zoneId == null && podId == null && clusterId == null) {
    dcList=ApiDBUtils.listZones();
  }
 else   if (zoneId != null) {
    dcList.add(ApiDBUtils.findZoneById(zoneId));
  }
 else {
    if (clusterId != null) {
      zoneId=ApiDBUtils.findClusterById(clusterId).getDataCenterId();
    }
 else {
      zoneId=ApiDBUtils.findPodById(podId).getDataCenterId();
    }
    if (capacityType == null || capacityType == Capacity.CAPACITY_TYPE_STORAGE) {
      capacities.add(_storageMgr.getStoragePoolUsedStats(null,clusterId,podId,zoneId));
    }
  }
  for (  DataCenterVO zone : dcList) {
    zoneId=zone.getId();
    if ((capacityType == null || capacityType == Capacity.CAPACITY_TYPE_SECONDARY_STORAGE) && podId == null && clusterId == null) {
      capacities.add(_storageMgr.getSecondaryStorageUsedStats(null,zoneId));
    }
    if (capacityType == null || capacityType == Capacity.CAPACITY_TYPE_STORAGE) {
      capacities.add(_storageMgr.getStoragePoolUsedStats(null,clusterId,podId,zoneId));
    }
  }
  return capacities;
}