Program: 105789
Project: 105789
Buggy source:
private VMInstanceVO orchestrateReConfigureVm(String vmUuid,ServiceOffering oldServiceOffering,boolean reconfiguringOnExistingHost) throws ResourceUnavailableException, ConcurrentOperationException {
  VMInstanceVO vm=_vmDao.findByUuid(vmUuid);
  long newServiceofferingId=vm.getServiceOfferingId();
  ServiceOffering newServiceOffering=_offeringDao.findById(vm.getId(),newServiceofferingId);
  HostVO hostVo=_hostDao.findById(vm.getHostId());
  Float memoryOvercommitRatio=CapacityManager.MemOverprovisioningFactor.valueIn(hostVo.getClusterId());
  Float cpuOvercommitRatio=CapacityManager.CpuOverprovisioningFactor.valueIn(hostVo.getClusterId());
  long minMemory=(long)(newServiceOffering.getRamSize() / memoryOvercommitRatio);
  ScaleVmCommand reconfigureCmd=new ScaleVmCommand(vm.getInstanceName(),newServiceOffering.getCpu(),(int)(newServiceOffering.getSpeed() / cpuOvercommitRatio),newServiceOffering.getSpeed(),minMemory * 1024L * 1024L,newServiceOffering.getRamSize() * 1024L * 1024L,newServiceOffering.getLimitCpuUse());
  Long dstHostId=vm.getHostId();
  ItWorkVO work=new ItWorkVO(UUID.randomUUID().toString(),_nodeId,State.Running,vm.getType(),vm.getId());
  work.setStep(Step.Prepare);
  work.setResourceType(ItWorkVO.ResourceType.Host);
  work.setResourceId(vm.getHostId());
  work=_workDao.persist(work);
  boolean success=false;
  try {
    if (reconfiguringOnExistingHost) {
      vm.setServiceOfferingId(oldServiceOffering.getId());
      _capacityMgr.releaseVmCapacity(vm,false,false,vm.getHostId());
      vm.setServiceOfferingId(newServiceofferingId);
      _capacityMgr.allocateVmCapacity(vm,false);
    }
    Answer reconfigureAnswer=_agentMgr.send(vm.getHostId(),reconfigureCmd);
    if (reconfigureAnswer == null || !reconfigureAnswer.getResult()) {
      s_logger.error("String_Node_Str" + (reconfigureAnswer == null ? "String_Node_Str" : reconfigureAnswer.getDetails()));
      throw new CloudRuntimeException("String_Node_Str" + (reconfigureAnswer == null ? "String_Node_Str" : reconfigureAnswer.getDetails()));
    }
    success=true;
  }
 catch (  OperationTimedoutException e) {
    throw new AgentUnavailableException("String_Node_Str" + vm,dstHostId);
  }
catch (  AgentUnavailableException e) {
    throw e;
  }
 finally {
    if (!success) {
      _capacityMgr.releaseVmCapacity(vm,false,false,vm.getHostId());
      vm.setServiceOfferingId(oldServiceOffering.getId());
      _capacityMgr.allocateVmCapacity(vm,false);
    }
  }
  return vm;
}

Fixed version (ground truth):
private VMInstanceVO orchestrateReConfigureVm(String vmUuid,ServiceOffering oldServiceOffering,boolean reconfiguringOnExistingHost) throws ResourceUnavailableException, ConcurrentOperationException {
  VMInstanceVO vm=_vmDao.findByUuid(vmUuid);
  long newServiceofferingId=vm.getServiceOfferingId();
  ServiceOffering newServiceOffering=_offeringDao.findById(vm.getId(),newServiceofferingId);
  HostVO hostVo=_hostDao.findById(vm.getHostId());
  Float memoryOvercommitRatio=CapacityManager.MemOverprovisioningFactor.valueIn(hostVo.getClusterId());
  Float cpuOvercommitRatio=CapacityManager.CpuOverprovisioningFactor.valueIn(hostVo.getClusterId());
  long minMemory=(long)(newServiceOffering.getRamSize() / memoryOvercommitRatio);
  ScaleVmCommand reconfigureCmd=new ScaleVmCommand(vm.getInstanceName(),newServiceOffering.getCpu(),(int)(newServiceOffering.getSpeed() / cpuOvercommitRatio),newServiceOffering.getSpeed(),minMemory * 1024L * 1024L,newServiceOffering.getRamSize() * 1024L * 1024L,newServiceOffering.getLimitCpuUse());
  Long dstHostId=vm.getHostId();
  ItWorkVO work=new ItWorkVO(UUID.randomUUID().toString(),_nodeId,State.Running,vm.getType(),vm.getId());
  work.setStep(Step.Prepare);
  work.setResourceType(ItWorkVO.ResourceType.Host);
  work.setResourceId(vm.getHostId());
  _workDao.persist(work);
  boolean success=false;
  try {
    if (reconfiguringOnExistingHost) {
      vm.setServiceOfferingId(oldServiceOffering.getId());
      _capacityMgr.releaseVmCapacity(vm,false,false,vm.getHostId());
      vm.setServiceOfferingId(newServiceofferingId);
      _capacityMgr.allocateVmCapacity(vm,false);
    }
    Answer reconfigureAnswer=_agentMgr.send(vm.getHostId(),reconfigureCmd);
    if (reconfigureAnswer == null || !reconfigureAnswer.getResult()) {
      s_logger.error("String_Node_Str" + (reconfigureAnswer == null ? "String_Node_Str" : reconfigureAnswer.getDetails()));
      throw new CloudRuntimeException("String_Node_Str" + (reconfigureAnswer == null ? "String_Node_Str" : reconfigureAnswer.getDetails()));
    }
    success=true;
  }
 catch (  OperationTimedoutException e) {
    throw new AgentUnavailableException("String_Node_Str" + vm,dstHostId);
  }
catch (  AgentUnavailableException e) {
    throw e;
  }
 finally {
    if (!success) {
      _capacityMgr.releaseVmCapacity(vm,false,false,vm.getHostId());
      vm.setServiceOfferingId(oldServiceOffering.getId());
      _capacityMgr.allocateVmCapacity(vm,false);
    }
  }
  return vm;
}