Program: 105691
Project: 105691
Buggy source:
public UserVm restoreVMInternal(Account caller,UserVmVO vm,Long newTemplateId) throws InsufficientCapacityException, ResourceUnavailableException {
  Long userId=caller.getId();
  Account owner=_accountDao.findById(vm.getAccountId());
  _userDao.findById(userId);
  long vmId=vm.getId();
  boolean needRestart=false;
  if (owner == null) {
    throw new InvalidParameterValueException("String_Node_Str" + vm + "String_Node_Str"+ vm.getAccountId());
  }
  if (owner.getState() == Account.State.disabled) {
    throw new PermissionDeniedException("String_Node_Str" + vm + "String_Node_Str"+ vm.getAccountId());
  }
  if (vm.getState() != VirtualMachine.State.Running && vm.getState() != VirtualMachine.State.Stopped) {
    throw new CloudRuntimeException("String_Node_Str" + vm.getUuid() + "String_Node_Str"+ vm.getState()+ "String_Node_Str");
  }
  if (vm.getState() == VirtualMachine.State.Running) {
    needRestart=true;
  }
  List<VolumeVO> rootVols=_volsDao.findByInstanceAndType(vmId,Volume.Type.ROOT);
  if (rootVols.isEmpty()) {
    InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str" + vm.getUuid());
    ex.addProxyObject(vm.getUuid(),"String_Node_Str");
    throw ex;
  }
  VolumeVO root=rootVols.get(0);
  Long templateId=root.getTemplateId();
  boolean isISO=false;
  if (templateId == null) {
    isISO=true;
    templateId=vm.getIsoId();
  }
  List<VMSnapshotVO> vmSnapshots=_vmSnapshotDao.findByVm(vmId);
  if (vmSnapshots.size() > 0 && vm.getHypervisorType() == HypervisorType.VMware) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  VMTemplateVO template=null;
  if (newTemplateId != null) {
    template=_templateDao.findById(newTemplateId);
    _accountMgr.checkAccess(caller,null,true,template);
    if (isISO) {
      if (!template.getFormat().equals(ImageFormat.ISO)) {
        throw new InvalidParameterValueException("String_Node_Str");
      }
    }
 else {
      if (template.getFormat().equals(ImageFormat.ISO)) {
        throw new InvalidParameterValueException("String_Node_Str");
      }
    }
  }
 else {
    if (isISO && templateId == null) {
      throw new CloudRuntimeException("String_Node_Str");
    }
    template=_templateDao.findById(templateId);
    if (template == null) {
      InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str");
      ex.addProxyObject(vm.getUuid(),"String_Node_Str");
      ex.addProxyObject(root.getUuid(),"String_Node_Str");
      throw ex;
    }
  }
  if (needRestart) {
    try {
      _itMgr.stop(vm.getUuid());
    }
 catch (    ResourceUnavailableException e) {
      s_logger.debug("String_Node_Str" + vm.getUuid() + "String_Node_Str",e);
      CloudRuntimeException ex=new CloudRuntimeException("String_Node_Str");
      ex.addProxyObject(vm.getUuid(),"String_Node_Str");
      throw ex;
    }
  }
  Volume newVol=null;
  if (newTemplateId != null) {
    if (isISO) {
      newVol=volumeMgr.allocateDuplicateVolume(root,null);
      vm.setIsoId(newTemplateId);
      vm.setGuestOSId(template.getGuestOSId());
      vm.setTemplateId(newTemplateId);
      _vmDao.update(vmId,vm);
    }
 else {
      newVol=volumeMgr.allocateDuplicateVolume(root,newTemplateId);
      vm.setGuestOSId(template.getGuestOSId());
      vm.setTemplateId(newTemplateId);
      _vmDao.update(vmId,vm);
    }
  }
 else {
    newVol=volumeMgr.allocateDuplicateVolume(root,null);
  }
  if (vm instanceof UserVm) {
    _resourceLimitMgr.incrementResourceCount(vm.getAccountId(),ResourceType.volume);
  }
  UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_VOLUME_CREATE,newVol.getAccountId(),newVol.getDataCenterId(),newVol.getId(),newVol.getName(),newVol.getDiskOfferingId(),templateId,newVol.getSize());
  _usageEventDao.persist(usageEvent);
  handleManagedStorage(vm,root);
  _volsDao.attachVolume(newVol.getId(),vmId,newVol.getDeviceId());
  _volsDao.detachVolume(root.getId());
  volumeMgr.destroyVolume(root);
  if (vm.getHypervisorType() == HypervisorType.VMware) {
    VolumeInfo volumeInStorage=volFactory.getVolume(root.getId());
    if (volumeInStorage != null) {
      s_logger.info("String_Node_Str" + root.getId() + "String_Node_Str");
      AsyncCallFuture<VolumeApiResult> future=_volService.expungeVolumeAsync(volFactory.getVolume(root.getId()));
      try {
        future.get();
      }
 catch (      Exception e) {
        s_logger.debug("String_Node_Str" + root.getId(),e);
      }
    }
  }
  Map<VirtualMachineProfile.Param,Object> params=null;
  String password=null;
  if (template.getEnablePassword()) {
    password=_mgr.generateRandomPassword();
    boolean result=resetVMPasswordInternal(vmId,password);
    if (result) {
      vm.setPassword(password);
      _vmDao.loadDetails(vm);
      encryptAndStorePassword(vm,password);
    }
 else {
      throw new CloudRuntimeException("String_Node_Str");
    }
  }
  if (needRestart) {
    try {
      if (vm.getDetail("String_Node_Str") != null) {
        params=new HashMap<VirtualMachineProfile.Param,Object>();
        params.put(VirtualMachineProfile.Param.VmPassword,password);
      }
      _itMgr.start(vm.getUuid(),params);
      vm=_vmDao.findById(vmId);
      if (template.getEnablePassword()) {
        vm.setPassword(password);
        if (vm.isUpdateParameters()) {
          vm.setUpdateParameters(false);
          _vmDao.loadDetails(vm);
          if (vm.getDetail("String_Node_Str") != null) {
            _vmDetailsDao.remove(_vmDetailsDao.findDetail(vm.getId(),"String_Node_Str").getId());
          }
          _vmDao.update(vm.getId(),vm);
        }
      }
    }
 catch (    Exception e) {
      s_logger.debug("String_Node_Str" + vm.getUuid(),e);
      CloudRuntimeException ex=new CloudRuntimeException("String_Node_Str" + e.getMessage());
      ex.addProxyObject(vm.getUuid(),"String_Node_Str");
      throw ex;
    }
  }
  s_logger.debug("String_Node_Str" + vmId + "String_Node_Str"+ template.getUuid()+ "String_Node_Str");
  return vm;
}

Fixed version (ground truth):
public UserVm restoreVMInternal(Account caller,UserVmVO vm,Long newTemplateId) throws InsufficientCapacityException, ResourceUnavailableException {
  Long userId=caller.getId();
  Account owner=_accountDao.findById(vm.getAccountId());
  _userDao.findById(userId);
  long vmId=vm.getId();
  boolean needRestart=false;
  if (owner == null) {
    throw new InvalidParameterValueException("String_Node_Str" + vm + "String_Node_Str"+ vm.getAccountId());
  }
  if (owner.getState() == Account.State.disabled) {
    throw new PermissionDeniedException("String_Node_Str" + vm + "String_Node_Str"+ vm.getAccountId());
  }
  if (vm.getState() != VirtualMachine.State.Running && vm.getState() != VirtualMachine.State.Stopped) {
    throw new CloudRuntimeException("String_Node_Str" + vm.getUuid() + "String_Node_Str"+ vm.getState()+ "String_Node_Str");
  }
  if (vm.getState() == VirtualMachine.State.Running) {
    needRestart=true;
  }
  List<VolumeVO> rootVols=_volsDao.findByInstanceAndType(vmId,Volume.Type.ROOT);
  if (rootVols.isEmpty()) {
    InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str" + vm.getUuid());
    ex.addProxyObject(vm.getUuid(),"String_Node_Str");
    throw ex;
  }
  VolumeVO root=rootVols.get(0);
  Long templateId=root.getTemplateId();
  boolean isISO=false;
  if (templateId == null) {
    isISO=true;
    templateId=vm.getIsoId();
  }
  List<VMSnapshotVO> vmSnapshots=_vmSnapshotDao.findByVm(vmId);
  if (vmSnapshots.size() > 0 && vm.getHypervisorType() == HypervisorType.VMware) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  VMTemplateVO template=null;
  if (newTemplateId != null) {
    template=_templateDao.findById(newTemplateId);
    _accountMgr.checkAccess(caller,null,true,template);
    if (isISO) {
      if (!template.getFormat().equals(ImageFormat.ISO)) {
        throw new InvalidParameterValueException("String_Node_Str");
      }
    }
 else {
      if (template.getFormat().equals(ImageFormat.ISO)) {
        throw new InvalidParameterValueException("String_Node_Str");
      }
    }
  }
 else {
    if (isISO && templateId == null) {
      throw new CloudRuntimeException("String_Node_Str");
    }
    template=_templateDao.findById(templateId);
    if (template == null) {
      InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str");
      ex.addProxyObject(vm.getUuid(),"String_Node_Str");
      ex.addProxyObject(root.getUuid(),"String_Node_Str");
      throw ex;
    }
  }
  if (needRestart) {
    try {
      _itMgr.stop(vm.getUuid());
    }
 catch (    ResourceUnavailableException e) {
      s_logger.debug("String_Node_Str" + vm.getUuid() + "String_Node_Str",e);
      CloudRuntimeException ex=new CloudRuntimeException("String_Node_Str");
      ex.addProxyObject(vm.getUuid(),"String_Node_Str");
      throw ex;
    }
  }
  Volume newVol=null;
  if (newTemplateId != null) {
    if (isISO) {
      newVol=volumeMgr.allocateDuplicateVolume(root,null);
      vm.setIsoId(newTemplateId);
      vm.setGuestOSId(template.getGuestOSId());
      vm.setTemplateId(newTemplateId);
      _vmDao.update(vmId,vm);
    }
 else {
      newVol=volumeMgr.allocateDuplicateVolume(root,newTemplateId);
      vm.setGuestOSId(template.getGuestOSId());
      vm.setTemplateId(newTemplateId);
      _vmDao.update(vmId,vm);
    }
  }
 else {
    newVol=volumeMgr.allocateDuplicateVolume(root,null);
  }
  _resourceLimitMgr.incrementResourceCount(vm.getAccountId(),ResourceType.volume);
  UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_VOLUME_CREATE,newVol.getAccountId(),newVol.getDataCenterId(),newVol.getId(),newVol.getName(),newVol.getDiskOfferingId(),templateId,newVol.getSize());
  _usageEventDao.persist(usageEvent);
  handleManagedStorage(vm,root);
  _volsDao.attachVolume(newVol.getId(),vmId,newVol.getDeviceId());
  _volsDao.detachVolume(root.getId());
  volumeMgr.destroyVolume(root);
  if (vm.getHypervisorType() == HypervisorType.VMware) {
    VolumeInfo volumeInStorage=volFactory.getVolume(root.getId());
    if (volumeInStorage != null) {
      s_logger.info("String_Node_Str" + root.getId() + "String_Node_Str");
      AsyncCallFuture<VolumeApiResult> future=_volService.expungeVolumeAsync(volFactory.getVolume(root.getId()));
      try {
        future.get();
      }
 catch (      Exception e) {
        s_logger.debug("String_Node_Str" + root.getId(),e);
      }
    }
  }
  Map<VirtualMachineProfile.Param,Object> params=null;
  String password=null;
  if (template.getEnablePassword()) {
    password=_mgr.generateRandomPassword();
    boolean result=resetVMPasswordInternal(vmId,password);
    if (result) {
      vm.setPassword(password);
      _vmDao.loadDetails(vm);
      encryptAndStorePassword(vm,password);
    }
 else {
      throw new CloudRuntimeException("String_Node_Str");
    }
  }
  if (needRestart) {
    try {
      if (vm.getDetail("String_Node_Str") != null) {
        params=new HashMap<VirtualMachineProfile.Param,Object>();
        params.put(VirtualMachineProfile.Param.VmPassword,password);
      }
      _itMgr.start(vm.getUuid(),params);
      vm=_vmDao.findById(vmId);
      if (template.getEnablePassword()) {
        vm.setPassword(password);
        if (vm.isUpdateParameters()) {
          vm.setUpdateParameters(false);
          _vmDao.loadDetails(vm);
          if (vm.getDetail("String_Node_Str") != null) {
            _vmDetailsDao.remove(_vmDetailsDao.findDetail(vm.getId(),"String_Node_Str").getId());
          }
          _vmDao.update(vm.getId(),vm);
        }
      }
    }
 catch (    Exception e) {
      s_logger.debug("String_Node_Str" + vm.getUuid(),e);
      CloudRuntimeException ex=new CloudRuntimeException("String_Node_Str" + e.getMessage());
      ex.addProxyObject(vm.getUuid(),"String_Node_Str");
      throw ex;
    }
  }
  s_logger.debug("String_Node_Str" + vmId + "String_Node_Str"+ template.getUuid()+ "String_Node_Str");
  return vm;
}