Program: 89882
Project: 89882
Buggy source:
@Override @DB public UserVm createVirtualMachine(DeployVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException, ConcurrentOperationException {
  Account caller=UserContext.current().getAccount();
  String accountName=cmd.getAccountName();
  Long domainId=cmd.getDomainId();
  List<Long> networkList=cmd.getNetworkIds();
  Account owner=_accountDao.findActiveAccount(accountName,domainId);
  if (owner == null) {
    throw new InvalidParameterValueException("String_Node_Str" + accountName + "String_Node_Str"+ domainId);
  }
  _accountMgr.checkAccess(caller,owner);
  long accountId=owner.getId();
  DataCenterVO dc=_dcDao.findById(cmd.getZoneId());
  if (dc == null) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getZoneId());
  }
  if (dc.getDomainId() != null) {
    DomainVO domain=_domainDao.findById(dc.getDomainId());
    if (domain == null) {
      throw new CloudRuntimeException("String_Node_Str" + dc.getDomainId() + "String_Node_Str"+ dc);
    }
    _accountMgr.checkAccess(caller,domain);
    _accountMgr.checkAccess(owner,domain);
  }
  ServiceOfferingVO offering=_serviceOfferingDao.findById(cmd.getServiceOfferingId());
  if (offering == null || offering.getRemoved() != null) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getServiceOfferingId());
  }
  VMTemplateVO template=_templateDao.findById(cmd.getTemplateId());
  if (template == null || template.getRemoved() != null) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getTemplateId());
  }
  boolean isIso=Storage.ImageFormat.ISO == template.getFormat();
  if (isIso && !template.isBootable()) {
    throw new InvalidParameterValueException("String_Node_Str" + template.getId());
  }
  Pair<DiskOfferingVO,Long> rootDiskOffering=new Pair<DiskOfferingVO,Long>(null,null);
  List<Pair<DiskOfferingVO,Long>> dataDiskOfferings=new ArrayList<Pair<DiskOfferingVO,Long>>();
  if (isIso) {
    if (cmd.getDiskOfferingId() == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    DiskOfferingVO diskOffering=_diskOfferingDao.findById(cmd.getDiskOfferingId());
    if (diskOffering == null) {
      throw new InvalidParameterValueException("String_Node_Str" + cmd.getDiskOfferingId());
    }
    Long size=null;
    if (diskOffering.getDiskSize() == 0) {
      size=cmd.getSize();
      if (size == null) {
        throw new InvalidParameterValueException("String_Node_Str" + diskOffering + "String_Node_Str");
      }
    }
    rootDiskOffering.first(diskOffering);
    rootDiskOffering.second(size);
  }
 else {
    rootDiskOffering.first(offering);
    if (cmd.getDiskOfferingId() != null) {
      DiskOfferingVO diskOffering=_diskOfferingDao.findById(cmd.getDiskOfferingId());
      if (diskOffering == null) {
        throw new InvalidParameterValueException("String_Node_Str" + cmd.getDiskOfferingId());
      }
      Long size=null;
      if (diskOffering.getDiskSize() == 0) {
        size=cmd.getSize();
        if (size == null) {
          throw new InvalidParameterValueException("String_Node_Str" + diskOffering + "String_Node_Str");
        }
      }
      dataDiskOfferings.add(new Pair<DiskOfferingVO,Long>(diskOffering,size));
    }
  }
  String password=PasswordGenerator.generateRandomPassword(6);
  if (!template.getEnablePassword()) {
    password="String_Node_Str";
  }
  if (password == null || password.equals("String_Node_Str") || (!validPassword(password))) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  String networkDomain=null;
  if (networkDomain == null) {
    networkDomain="String_Node_Str" + Long.toHexString(owner.getId()) + _defaultNetworkDomain;
  }
  String userData=cmd.getUserData();
  byte[] decodedUserData=null;
  if (userData != null) {
    if (userData.length() >= 2 * MAX_USER_DATA_LENGTH_BYTES) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    decodedUserData=org.apache.commons.codec.binary.Base64.decodeBase64(userData.getBytes());
    if (decodedUserData.length > MAX_USER_DATA_LENGTH_BYTES) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if (decodedUserData.length < 1) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
  }
  _accountMgr.checkAccess(caller,template);
  DataCenterDeployment plan=new DataCenterDeployment(dc.getId());
  s_logger.debug("String_Node_Str");
  if (dc.getNetworkType() == NetworkType.Basic && networkList == null) {
    Long singleNetworkId=null;
    SearchBuilder<NetworkVO> sb=_networkDao.createSearchBuilder();
    sb.and("String_Node_Str",sb.entity().getId(),SearchCriteria.Op.EQ);
    sb.and("String_Node_Str",sb.entity().getName(),SearchCriteria.Op.EQ);
    SearchCriteria<NetworkVO> sc=sb.create();
    sc.setParameters("String_Node_Str",BroadcastDomainType.Native);
    sc.setParameters("String_Node_Str",dc.getId());
    List<NetworkVO> networks=_networkDao.search(sc,null);
    if (networks != null && networks.isEmpty()) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
 else {
      networkList=new ArrayList<Long>();
      singleNetworkId=networks.get(0).getId();
      networkList.add(singleNetworkId);
    }
  }
  if (networkList == null || networkList.isEmpty()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  List<Pair<NetworkVO,NicProfile>> networks=new ArrayList<Pair<NetworkVO,NicProfile>>();
  for (  Long networkId : networkList) {
    NetworkVO network=_networkDao.findById(networkId);
    if (network == null) {
      throw new InvalidParameterValueException("String_Node_Str" + networkId);
    }
 else {
      if (!network.isShared()) {
        List<NetworkVO> networkMap=_networkDao.listBy(accountId,networkId);
        if (networkMap == null || networkMap.isEmpty()) {
          throw new PermissionDeniedException("String_Node_Str" + networkId + "String_Node_Str");
        }
      }
      networks.add(new Pair<NetworkVO,NicProfile>(network,null));
    }
  }
  long id=_vmDao.getNextInSequence(Long.class,"String_Node_Str");
  String hostName=cmd.getName();
  String instanceName=VirtualMachineName.getVmName(id,owner.getId(),_instance);
  if (hostName == null) {
    hostName=instanceName;
  }
 else {
    hostName=hostName.toLowerCase();
    UserVm vm=_vmDao.findVmByZoneIdAndName(dc.getId(),hostName);
    if (vm != null && !(vm.getState() == State.Expunging || vm.getState() == State.Error)) {
      throw new InvalidParameterValueException("String_Node_Str" + hostName + "String_Node_Str"+ dc.getId());
    }
 else     if (!NetUtils.verifyHostName(hostName)) {
      throw new InvalidParameterValueException("String_Node_Str" + "String_Node_Str");
    }
  }
  UserVmVO vm=new UserVmVO(id,instanceName,cmd.getDisplayName(),template.getId(),template.getGuestOSId(),offering.getOfferHA(),domainId,owner.getId(),offering.getId(),userData,hostName);
  if (_itMgr.allocate(vm,template,offering,rootDiskOffering,dataDiskOfferings,networks,null,plan,cmd.getHypervisor(),owner) == null) {
    return null;
  }
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + vm);
  }
  UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_VM_CREATE,accountId,dc.getId(),vm.getId(),vm.getName(),offering.getId(),template.getId(),null);
  _usageEventDao.persist(usageEvent);
  return vm;
}

Fixed version (ground truth):
@Override @DB public UserVm createVirtualMachine(DeployVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException, ConcurrentOperationException {
  Account caller=UserContext.current().getAccount();
  String accountName=cmd.getAccountName();
  Long domainId=cmd.getDomainId();
  List<Long> networkList=cmd.getNetworkIds();
  Account owner=_accountDao.findActiveAccount(accountName,domainId);
  if (owner == null) {
    throw new InvalidParameterValueException("String_Node_Str" + accountName + "String_Node_Str"+ domainId);
  }
  _accountMgr.checkAccess(caller,owner);
  long accountId=owner.getId();
  DataCenterVO dc=_dcDao.findById(cmd.getZoneId());
  if (dc == null) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getZoneId());
  }
  if (dc.getDomainId() != null) {
    DomainVO domain=_domainDao.findById(dc.getDomainId());
    if (domain == null) {
      throw new CloudRuntimeException("String_Node_Str" + dc.getDomainId() + "String_Node_Str"+ dc);
    }
    _accountMgr.checkAccess(caller,domain);
    _accountMgr.checkAccess(owner,domain);
  }
  ServiceOfferingVO offering=_serviceOfferingDao.findById(cmd.getServiceOfferingId());
  if (offering == null || offering.getRemoved() != null) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getServiceOfferingId());
  }
  VMTemplateVO template=_templateDao.findById(cmd.getTemplateId());
  if (template == null || template.getRemoved() != null) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getTemplateId());
  }
  boolean isIso=Storage.ImageFormat.ISO == template.getFormat();
  if (isIso && !template.isBootable()) {
    throw new InvalidParameterValueException("String_Node_Str" + template.getId());
  }
  Pair<DiskOfferingVO,Long> rootDiskOffering=new Pair<DiskOfferingVO,Long>(null,null);
  List<Pair<DiskOfferingVO,Long>> dataDiskOfferings=new ArrayList<Pair<DiskOfferingVO,Long>>();
  if (isIso) {
    if (cmd.getDiskOfferingId() == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    DiskOfferingVO diskOffering=_diskOfferingDao.findById(cmd.getDiskOfferingId());
    if (diskOffering == null) {
      throw new InvalidParameterValueException("String_Node_Str" + cmd.getDiskOfferingId());
    }
    Long size=null;
    if (diskOffering.getDiskSize() == 0) {
      size=cmd.getSize();
      if (size == null) {
        throw new InvalidParameterValueException("String_Node_Str" + diskOffering + "String_Node_Str");
      }
    }
    rootDiskOffering.first(diskOffering);
    rootDiskOffering.second(size);
  }
 else {
    rootDiskOffering.first(offering);
    if (cmd.getDiskOfferingId() != null) {
      DiskOfferingVO diskOffering=_diskOfferingDao.findById(cmd.getDiskOfferingId());
      if (diskOffering == null) {
        throw new InvalidParameterValueException("String_Node_Str" + cmd.getDiskOfferingId());
      }
      Long size=null;
      if (diskOffering.getDiskSize() == 0) {
        size=cmd.getSize();
        if (size == null) {
          throw new InvalidParameterValueException("String_Node_Str" + diskOffering + "String_Node_Str");
        }
      }
      dataDiskOfferings.add(new Pair<DiskOfferingVO,Long>(diskOffering,size));
    }
  }
  String password=PasswordGenerator.generateRandomPassword(6);
  if (!template.getEnablePassword()) {
    password="String_Node_Str";
  }
  if (password == null || password.equals("String_Node_Str") || (!validPassword(password))) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  String networkDomain=null;
  if (networkDomain == null) {
    networkDomain="String_Node_Str" + Long.toHexString(owner.getId()) + _defaultNetworkDomain;
  }
  String userData=cmd.getUserData();
  byte[] decodedUserData=null;
  if (userData != null) {
    if (userData.length() >= 2 * MAX_USER_DATA_LENGTH_BYTES) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    decodedUserData=org.apache.commons.codec.binary.Base64.decodeBase64(userData.getBytes());
    if (decodedUserData.length > MAX_USER_DATA_LENGTH_BYTES) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if (decodedUserData.length < 1) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
  }
  _accountMgr.checkAccess(caller,template);
  DataCenterDeployment plan=new DataCenterDeployment(dc.getId());
  s_logger.debug("String_Node_Str");
  if (dc.getNetworkType() == NetworkType.Basic && networkList == null) {
    Long singleNetworkId=null;
    SearchBuilder<NetworkVO> sb=_networkDao.createSearchBuilder();
    sb.and("String_Node_Str",sb.entity().getId(),SearchCriteria.Op.EQ);
    sb.and("String_Node_Str",sb.entity().getName(),SearchCriteria.Op.EQ);
    SearchCriteria<NetworkVO> sc=sb.create();
    sc.setParameters("String_Node_Str",BroadcastDomainType.Native);
    sc.setParameters("String_Node_Str",dc.getId());
    List<NetworkVO> networks=_networkDao.search(sc,null);
    if (networks != null && networks.isEmpty()) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
 else {
      networkList=new ArrayList<Long>();
      singleNetworkId=networks.get(0).getId();
      networkList.add(singleNetworkId);
    }
  }
  if (networkList == null || networkList.isEmpty()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  List<Pair<NetworkVO,NicProfile>> networks=new ArrayList<Pair<NetworkVO,NicProfile>>();
  for (  Long networkId : networkList) {
    NetworkVO network=_networkDao.findById(networkId);
    if (network == null) {
      throw new InvalidParameterValueException("String_Node_Str" + networkId);
    }
 else {
      if (!network.isShared()) {
        List<NetworkVO> networkMap=_networkDao.listBy(accountId,networkId);
        if (networkMap == null || networkMap.isEmpty()) {
          throw new PermissionDeniedException("String_Node_Str" + networkId + "String_Node_Str");
        }
      }
      networks.add(new Pair<NetworkVO,NicProfile>(network,null));
    }
  }
  long id=_vmDao.getNextInSequence(Long.class,"String_Node_Str");
  String hostName=cmd.getName();
  String instanceName=VirtualMachineName.getVmName(id,owner.getId(),_instance);
  if (hostName == null) {
    hostName=instanceName;
  }
 else {
    hostName=hostName.toLowerCase();
    UserVm vm=_vmDao.findVmByZoneIdAndName(dc.getId(),hostName);
    if (vm != null && !(vm.getState() == State.Expunging || vm.getState() == State.Error)) {
      throw new InvalidParameterValueException("String_Node_Str" + hostName + "String_Node_Str"+ dc.getId());
    }
 else     if (!NetUtils.verifyHostName(hostName)) {
      throw new InvalidParameterValueException("String_Node_Str" + "String_Node_Str");
    }
  }
  UserVmVO vm=new UserVmVO(id,instanceName,cmd.getDisplayName(),template.getId(),template.getGuestOSId(),offering.getOfferHA(),domainId,owner.getId(),offering.getId(),userData,hostName);
  if (_itMgr.allocate(vm,template,offering,rootDiskOffering,dataDiskOfferings,networks,null,plan,cmd.getHypervisor(),owner) == null) {
    return null;
  }
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + vm);
  }
  UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_VM_CREATE,accountId,dc.getId(),vm.getId(),vm.getName(),offering.getId(),template.getId(),null);
  _usageEventDao.persist(usageEvent);
  _accountMgr.incrementResourceCount(accountId,ResourceType.user_vm);
  return vm;
}