Program: 106098
Project: 106098
Buggy source:
private Pair<List<UserVmJoinVO>,Integer> searchForUserVMsInternal(ListVMsCmd cmd){
  Account caller=CallContext.current().getCallingAccount();
  List<Long> permittedAccounts=new ArrayList<Long>();
  Pair<List<Long>,List<Long>> idPair=_aclService.getAclEntityPermission(caller.getId(),AclEntityType.VM.toString(),AccessType.ListEntry);
  List<Long> grantedIds=idPair.first();
  List<Long> revokedIds=idPair.second();
  boolean listAll=cmd.listAll();
  Long id=cmd.getId();
  Ternary<Long,Boolean,ListProjectResourcesCriteria> domainIdRecursiveListProject=new Ternary<Long,Boolean,ListProjectResourcesCriteria>(cmd.getDomainId(),cmd.isRecursive(),null);
  _accountMgr.buildACLSearchParameters(caller,id,cmd.getAccountName(),cmd.getProjectId(),permittedAccounts,domainIdRecursiveListProject,listAll,false);
  Long domainId=domainIdRecursiveListProject.first();
  Boolean isRecursive=domainIdRecursiveListProject.second();
  ListProjectResourcesCriteria listProjectResourcesCriteria=domainIdRecursiveListProject.third();
  Filter searchFilter=new Filter(UserVmJoinVO.class,"String_Node_Str",true,cmd.getStartIndex(),cmd.getPageSizeVal());
  SearchBuilder<UserVmJoinVO> sb=_userVmJoinDao.createSearchBuilder();
  sb.select(null,Func.DISTINCT,sb.entity().getId());
  _accountMgr.buildACLViewSearchBuilder(sb,domainId,isRecursive,permittedAccounts,listProjectResourcesCriteria,grantedIds,revokedIds);
  Map<String,String> tags=cmd.getTags();
  String hypervisor=cmd.getHypervisor();
  Object name=cmd.getName();
  Object state=cmd.getState();
  Object zoneId=cmd.getZoneId();
  Object keyword=cmd.getKeyword();
  boolean isAdmin=false;
  boolean isRootAdmin=false;
  if (_accountMgr.isAdmin(caller.getType())) {
    isAdmin=true;
  }
  if (_accountMgr.isRootAdmin(caller.getId())) {
    isRootAdmin=true;
  }
  Object groupId=cmd.getGroupId();
  Object networkId=cmd.getNetworkId();
  if (HypervisorType.getType(hypervisor) == HypervisorType.None && hypervisor != null) {
    throw new InvalidParameterValueException("String_Node_Str" + hypervisor);
  }
  Object templateId=cmd.getTemplateId();
  Object isoId=cmd.getIsoId();
  Object vpcId=cmd.getVpcId();
  Object affinityGroupId=cmd.getAffinityGroupId();
  Object pod=null;
  Object hostId=null;
  Object storageId=null;
  if (cmd instanceof ListVMsCmdByAdmin) {
    ListVMsCmdByAdmin adCmd=(ListVMsCmdByAdmin)cmd;
    pod=adCmd.getPodId();
    hostId=adCmd.getHostId();
    storageId=adCmd.getStorageId();
  }
  sb.and("String_Node_Str",sb.entity().getDisplayName(),SearchCriteria.Op.LIKE);
  sb.and("String_Node_Str",sb.entity().getId(),SearchCriteria.Op.EQ);
  sb.and("String_Node_Str",sb.entity().getName(),SearchCriteria.Op.LIKE);
  sb.and("String_Node_Str",sb.entity().getState(),SearchCriteria.Op.EQ);
  sb.and("String_Node_Str",sb.entity().getState(),SearchCriteria.Op.NEQ);
  sb.and("String_Node_Str",sb.entity().getState(),SearchCriteria.Op.NIN);
  sb.and("String_Node_Str",sb.entity().getDataCenterId(),SearchCriteria.Op.EQ);
  sb.and("String_Node_Str",sb.entity().getPodId(),SearchCriteria.Op.EQ);
  sb.and("String_Node_Str",sb.entity().getHypervisorType(),SearchCriteria.Op.EQ);
  sb.and("String_Node_Str",sb.entity().getHostId(),SearchCriteria.Op.EQ);
  sb.and("String_Node_Str",sb.entity().getTemplateId(),SearchCriteria.Op.EQ);
  sb.and("String_Node_Str",sb.entity().getIsoId(),SearchCriteria.Op.EQ);
  sb.and("String_Node_Str",sb.entity().getInstanceGroupId(),SearchCriteria.Op.EQ);
  if (groupId != null && (Long)groupId != -1) {
    sb.and("String_Node_Str",sb.entity().getInstanceGroupId(),SearchCriteria.Op.EQ);
  }
  if (networkId != null) {
    sb.and("String_Node_Str",sb.entity().getNetworkId(),SearchCriteria.Op.EQ);
  }
  if (vpcId != null && networkId == null) {
    sb.and("String_Node_Str",sb.entity().getVpcId(),SearchCriteria.Op.EQ);
  }
  if (storageId != null) {
    sb.and("String_Node_Str",sb.entity().getPoolId(),SearchCriteria.Op.EQ);
  }
  if (affinityGroupId != null) {
    sb.and("String_Node_Str",sb.entity().getAffinityGroupId(),SearchCriteria.Op.EQ);
  }
  if (!isRootAdmin) {
    sb.and("String_Node_Str",sb.entity().isDisplayVm(),SearchCriteria.Op.EQ);
  }
  SearchCriteria<UserVmJoinVO> sc=sb.create();
  _accountMgr.buildACLViewSearchCriteria(sc,domainId,isRecursive,permittedAccounts,listProjectResourcesCriteria);
  if (tags != null && !tags.isEmpty()) {
    SearchCriteria<UserVmJoinVO> tagSc=_userVmJoinDao.createSearchCriteria();
    for (    String key : tags.keySet()) {
      SearchCriteria<UserVmJoinVO> tsc=_userVmJoinDao.createSearchCriteria();
      tsc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,key);
      tsc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,tags.get(key));
      tagSc.addOr("String_Node_Str",SearchCriteria.Op.SC,tsc);
    }
    sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,tagSc);
  }
  if (groupId != null && (Long)groupId != -1) {
    sc.setParameters("String_Node_Str",groupId);
  }
  if (keyword != null) {
    SearchCriteria<UserVmJoinVO> ssc=_userVmJoinDao.createSearchCriteria();
    ssc.addOr("String_Node_Str",SearchCriteria.Op.LIKE,"String_Node_Str" + keyword + "String_Node_Str");
    ssc.addOr("String_Node_Str",SearchCriteria.Op.LIKE,"String_Node_Str" + keyword + "String_Node_Str");
    ssc.addOr("String_Node_Str",SearchCriteria.Op.LIKE,"String_Node_Str" + keyword + "String_Node_Str");
    ssc.addOr("String_Node_Str",SearchCriteria.Op.EQ,keyword);
    sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,ssc);
  }
  if (id != null) {
    sc.setParameters("String_Node_Str",id);
  }
  if (templateId != null) {
    sc.setParameters("String_Node_Str",templateId);
  }
  if (isoId != null) {
    sc.setParameters("String_Node_Str",isoId);
  }
  if (networkId != null) {
    sc.setParameters("String_Node_Str",networkId);
  }
  if (vpcId != null && networkId == null) {
    sc.setParameters("String_Node_Str",vpcId);
  }
  if (name != null) {
    sc.setParameters("String_Node_Str","String_Node_Str" + name + "String_Node_Str");
  }
  if (state != null) {
    sc.setParameters("String_Node_Str",state);
  }
  if (hypervisor != null) {
    sc.setParameters("String_Node_Str",hypervisor);
  }
  if (!isAdmin) {
    sc.setParameters("String_Node_Str","String_Node_Str","String_Node_Str");
  }
  if (zoneId != null) {
    sc.setParameters("String_Node_Str",zoneId);
    if (state == null) {
      sc.setParameters("String_Node_Str","String_Node_Str");
    }
  }
  if (affinityGroupId != null) {
    sc.setParameters("String_Node_Str",affinityGroupId);
  }
  if (cmd instanceof ListVMsCmdByAdmin) {
    ListVMsCmdByAdmin aCmd=(ListVMsCmdByAdmin)cmd;
    if (aCmd.getPodId() != null) {
      sc.setParameters("String_Node_Str",pod);
      if (state == null) {
        sc.setParameters("String_Node_Str","String_Node_Str");
      }
    }
    if (hostId != null) {
      sc.setParameters("String_Node_Str",hostId);
    }
    if (storageId != null) {
      sc.setParameters("String_Node_Str",storageId);
    }
  }
  if (!isRootAdmin) {
    sc.setParameters("String_Node_Str",1);
  }
  Pair<List<UserVmJoinVO>,Integer> uniqueVmPair=_userVmJoinDao.searchAndCount(sc,searchFilter);
  Integer count=uniqueVmPair.second();
  if (count.intValue() == 0) {
    return uniqueVmPair;
  }
  List<UserVmJoinVO> uniqueVms=uniqueVmPair.first();
  Long[] vmIds=new Long[uniqueVms.size()];
  int i=0;
  for (  UserVmJoinVO v : uniqueVms) {
    vmIds[i++]=v.getId();
  }
  List<UserVmJoinVO> vms=_userVmJoinDao.searchByIds(vmIds);
  return new Pair<List<UserVmJoinVO>,Integer>(vms,count);
}

Fixed version (ground truth):
private Pair<List<UserVmJoinVO>,Integer> searchForUserVMsInternal(ListVMsCmd cmd){
  Account caller=CallContext.current().getCallingAccount();
  List<Long> permittedAccounts=new ArrayList<Long>();
  Pair<List<Long>,List<Long>> idPair=_aclService.getAclEntityPermission(caller.getId(),AclEntityType.VM.toString(),AccessType.ListEntry);
  List<Long> grantedIds=idPair.first();
  List<Long> revokedIds=idPair.second();
  boolean listAll=cmd.listAll();
  Long id=cmd.getId();
  Ternary<Long,Boolean,ListProjectResourcesCriteria> domainIdRecursiveListProject=new Ternary<Long,Boolean,ListProjectResourcesCriteria>(cmd.getDomainId(),cmd.isRecursive(),null);
  _accountMgr.buildACLSearchParameters(caller,id,cmd.getAccountName(),cmd.getProjectId(),permittedAccounts,domainIdRecursiveListProject,listAll,false);
  Long domainId=domainIdRecursiveListProject.first();
  Boolean isRecursive=domainIdRecursiveListProject.second();
  ListProjectResourcesCriteria listProjectResourcesCriteria=domainIdRecursiveListProject.third();
  Filter searchFilter=new Filter(UserVmJoinVO.class,"String_Node_Str",true,cmd.getStartIndex(),cmd.getPageSizeVal());
  SearchBuilder<UserVmJoinVO> sb=_userVmJoinDao.createSearchBuilder();
  sb.select(null,Func.DISTINCT,sb.entity().getId());
  _accountMgr.buildACLViewSearchBuilder(sb,domainId,isRecursive,permittedAccounts,listProjectResourcesCriteria,grantedIds,revokedIds);
  Map<String,String> tags=cmd.getTags();
  String hypervisor=cmd.getHypervisor();
  Object name=cmd.getName();
  Object state=cmd.getState();
  Object zoneId=cmd.getZoneId();
  Object keyword=cmd.getKeyword();
  boolean isAdmin=false;
  boolean isRootAdmin=false;
  if (_accountMgr.isAdmin(caller.getType())) {
    isAdmin=true;
  }
  if (_accountMgr.isRootAdmin(caller.getId())) {
    isRootAdmin=true;
  }
  Object groupId=cmd.getGroupId();
  Object networkId=cmd.getNetworkId();
  if (HypervisorType.getType(hypervisor) == HypervisorType.None && hypervisor != null) {
    throw new InvalidParameterValueException("String_Node_Str" + hypervisor);
  }
  Object templateId=cmd.getTemplateId();
  Object isoId=cmd.getIsoId();
  Object vpcId=cmd.getVpcId();
  Object affinityGroupId=cmd.getAffinityGroupId();
  Object pod=null;
  Object hostId=null;
  Object storageId=null;
  if (cmd instanceof ListVMsCmdByAdmin) {
    ListVMsCmdByAdmin adCmd=(ListVMsCmdByAdmin)cmd;
    pod=adCmd.getPodId();
    hostId=adCmd.getHostId();
    storageId=adCmd.getStorageId();
  }
  sb.and("String_Node_Str",sb.entity().getDisplayName(),SearchCriteria.Op.LIKE);
  sb.and("String_Node_Str",sb.entity().getId(),SearchCriteria.Op.EQ);
  sb.and("String_Node_Str",sb.entity().getName(),SearchCriteria.Op.LIKE);
  sb.and("String_Node_Str",sb.entity().getState(),SearchCriteria.Op.EQ);
  sb.and("String_Node_Str",sb.entity().getState(),SearchCriteria.Op.NEQ);
  sb.and("String_Node_Str",sb.entity().getState(),SearchCriteria.Op.NIN);
  sb.and("String_Node_Str",sb.entity().getDataCenterId(),SearchCriteria.Op.EQ);
  sb.and("String_Node_Str",sb.entity().getPodId(),SearchCriteria.Op.EQ);
  sb.and("String_Node_Str",sb.entity().getHypervisorType(),SearchCriteria.Op.EQ);
  sb.and("String_Node_Str",sb.entity().getHostId(),SearchCriteria.Op.EQ);
  sb.and("String_Node_Str",sb.entity().getTemplateId(),SearchCriteria.Op.EQ);
  sb.and("String_Node_Str",sb.entity().getIsoId(),SearchCriteria.Op.EQ);
  sb.and("String_Node_Str",sb.entity().getInstanceGroupId(),SearchCriteria.Op.EQ);
  if (groupId != null && (Long)groupId != -1) {
    sb.and("String_Node_Str",sb.entity().getInstanceGroupId(),SearchCriteria.Op.EQ);
  }
  if (networkId != null) {
    sb.and("String_Node_Str",sb.entity().getNetworkId(),SearchCriteria.Op.EQ);
  }
  if (vpcId != null && networkId == null) {
    sb.and("String_Node_Str",sb.entity().getVpcId(),SearchCriteria.Op.EQ);
  }
  if (storageId != null) {
    sb.and("String_Node_Str",sb.entity().getPoolId(),SearchCriteria.Op.EQ);
  }
  if (affinityGroupId != null) {
    sb.and("String_Node_Str",sb.entity().getAffinityGroupId(),SearchCriteria.Op.EQ);
  }
  if (!isRootAdmin) {
    sb.and("String_Node_Str",sb.entity().isDisplayVm(),SearchCriteria.Op.EQ);
  }
  SearchCriteria<UserVmJoinVO> sc=sb.create();
  _accountMgr.buildACLViewSearchCriteria(sc,domainId,isRecursive,permittedAccounts,listProjectResourcesCriteria,grantedIds,revokedIds);
  if (tags != null && !tags.isEmpty()) {
    SearchCriteria<UserVmJoinVO> tagSc=_userVmJoinDao.createSearchCriteria();
    for (    String key : tags.keySet()) {
      SearchCriteria<UserVmJoinVO> tsc=_userVmJoinDao.createSearchCriteria();
      tsc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,key);
      tsc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,tags.get(key));
      tagSc.addOr("String_Node_Str",SearchCriteria.Op.SC,tsc);
    }
    sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,tagSc);
  }
  if (groupId != null && (Long)groupId != -1) {
    sc.setParameters("String_Node_Str",groupId);
  }
  if (keyword != null) {
    SearchCriteria<UserVmJoinVO> ssc=_userVmJoinDao.createSearchCriteria();
    ssc.addOr("String_Node_Str",SearchCriteria.Op.LIKE,"String_Node_Str" + keyword + "String_Node_Str");
    ssc.addOr("String_Node_Str",SearchCriteria.Op.LIKE,"String_Node_Str" + keyword + "String_Node_Str");
    ssc.addOr("String_Node_Str",SearchCriteria.Op.LIKE,"String_Node_Str" + keyword + "String_Node_Str");
    ssc.addOr("String_Node_Str",SearchCriteria.Op.EQ,keyword);
    sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,ssc);
  }
  if (id != null) {
    sc.setParameters("String_Node_Str",id);
  }
  if (templateId != null) {
    sc.setParameters("String_Node_Str",templateId);
  }
  if (isoId != null) {
    sc.setParameters("String_Node_Str",isoId);
  }
  if (networkId != null) {
    sc.setParameters("String_Node_Str",networkId);
  }
  if (vpcId != null && networkId == null) {
    sc.setParameters("String_Node_Str",vpcId);
  }
  if (name != null) {
    sc.setParameters("String_Node_Str","String_Node_Str" + name + "String_Node_Str");
  }
  if (state != null) {
    sc.setParameters("String_Node_Str",state);
  }
  if (hypervisor != null) {
    sc.setParameters("String_Node_Str",hypervisor);
  }
  if (!isAdmin) {
    sc.setParameters("String_Node_Str","String_Node_Str","String_Node_Str");
  }
  if (zoneId != null) {
    sc.setParameters("String_Node_Str",zoneId);
    if (state == null) {
      sc.setParameters("String_Node_Str","String_Node_Str");
    }
  }
  if (affinityGroupId != null) {
    sc.setParameters("String_Node_Str",affinityGroupId);
  }
  if (cmd instanceof ListVMsCmdByAdmin) {
    ListVMsCmdByAdmin aCmd=(ListVMsCmdByAdmin)cmd;
    if (aCmd.getPodId() != null) {
      sc.setParameters("String_Node_Str",pod);
      if (state == null) {
        sc.setParameters("String_Node_Str","String_Node_Str");
      }
    }
    if (hostId != null) {
      sc.setParameters("String_Node_Str",hostId);
    }
    if (storageId != null) {
      sc.setParameters("String_Node_Str",storageId);
    }
  }
  if (!isRootAdmin) {
    sc.setParameters("String_Node_Str",1);
  }
  Pair<List<UserVmJoinVO>,Integer> uniqueVmPair=_userVmJoinDao.searchAndCount(sc,searchFilter);
  Integer count=uniqueVmPair.second();
  if (count.intValue() == 0) {
    return uniqueVmPair;
  }
  List<UserVmJoinVO> uniqueVms=uniqueVmPair.first();
  Long[] vmIds=new Long[uniqueVms.size()];
  int i=0;
  for (  UserVmJoinVO v : uniqueVms) {
    vmIds[i++]=v.getId();
  }
  List<UserVmJoinVO> vms=_userVmJoinDao.searchByIds(vmIds);
  return new Pair<List<UserVmJoinVO>,Integer>(vms,count);
}