Program: 107342
Project: 107342
Buggy source:
@Override @DB @SuppressWarnings("String_Node_Str") public boolean revokeNetworkGroupIngress(RevokeNetworkGroupIngressCmd cmd){
  Account account=UserContext.current().getAccount();
  Long userId=UserContext.current().getUserId();
  Long domainId=cmd.getDomainId();
  Integer startPort=cmd.getStartPort();
  Integer endPort=cmd.getEndPort();
  Integer icmpType=cmd.getIcmpType();
  Integer icmpCode=cmd.getIcmpCode();
  String protocol=cmd.getProtocol();
  String networkGroup=cmd.getNetworkGroupName();
  String cidrList=cmd.getCidrList();
  Map groupList=cmd.getUserNetworkGroupList();
  String[] cidrs=null;
  Long accountId=null;
  Integer startPortOrType=null;
  Integer endPortOrCode=null;
  if (protocol == null) {
    protocol="String_Node_Str";
  }
  if (!NetUtils.isValidNetworkGroupProto(protocol)) {
    s_logger.debug("String_Node_Str" + protocol);
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + protocol);
  }
  if ("String_Node_Str".equalsIgnoreCase(protocol)) {
    if ((icmpType == null) || (icmpCode == null)) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + icmpType + "String_Node_Str"+ icmpCode);
    }
    if (icmpType == -1 && icmpCode != -1) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
    }
    if (icmpCode > 255) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
    }
    startPortOrType=icmpType;
    endPortOrCode=icmpCode;
  }
 else   if (protocol.equals("String_Node_Str")) {
    if ((startPort != null) || (endPort != null)) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
    }
    startPortOrType=0;
    endPortOrCode=0;
  }
 else {
    if ((startPort == null) || (endPort == null)) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + startPort + "String_Node_Str"+ endPort);
    }
    if (startPort == 0 && endPort == 0) {
      endPort=65535;
    }
    if (startPort > endPort) {
      s_logger.debug("String_Node_Str" + startPort + "String_Node_Str"+ endPort);
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
    }
    if (startPort > 65535 || endPort > 65535 || startPort < -1 || endPort < -1) {
      s_logger.debug("String_Node_Str" + startPort + "String_Node_Str"+ endPort);
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
    }
    if (startPort < 0 || endPort < 0) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
    }
    startPortOrType=startPort;
    endPortOrCode=endPort;
  }
  if ((account == null) || isAdmin(account.getType())) {
    if ((account.getAccountName() != null) && (domainId != null)) {
      if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(),domainId)) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + networkGroup + "String_Node_Str");
        }
        throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + networkGroup + "String_Node_Str");
      }
      Account groupOwner=_accountDao.findActiveAccount(account.getAccountName(),domainId);
      if (groupOwner == null) {
        throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + account.getAccountName() + "String_Node_Str"+ domainId);
      }
      accountId=groupOwner.getId();
    }
 else {
      if (account != null) {
        accountId=account.getId();
        domainId=account.getDomainId();
      }
    }
  }
 else {
    if (account != null) {
      accountId=account.getId();
      domainId=account.getDomainId();
    }
  }
  if (accountId == null) {
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + networkGroup + "String_Node_Str");
  }
  NetworkGroupVO sg=_networkGroupDao.findByAccountAndName(accountId,networkGroup);
  if (sg == null) {
    s_logger.debug("String_Node_Str" + networkGroup);
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + networkGroup);
  }
  if (cidrList == null && groupList == null) {
    s_logger.debug("String_Node_Str");
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
  }
  List<String> authorizedCidrs=new ArrayList<String>();
  if (cidrList != null) {
    if (protocol.equals("String_Node_Str")) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
    }
    cidrs=cidrList.split("String_Node_Str");
    for (    String cidr : cidrs) {
      if (!NetUtils.isValidCIDR(cidr)) {
        s_logger.debug("String_Node_Str" + cidr + "String_Node_Str");
        throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + cidr + "String_Node_Str");
      }
      authorizedCidrs.add(cidr);
    }
  }
  List<NetworkGroupVO> authorizedGroups=new ArrayList<NetworkGroupVO>();
  if (groupList != null) {
    Collection userGroupCollection=groupList.values();
    Iterator iter=userGroupCollection.iterator();
    while (iter.hasNext()) {
      HashMap userGroup=(HashMap)iter.next();
      String group=(String)userGroup.get("String_Node_Str");
      String authorizedAccountName=(String)userGroup.get("String_Node_Str");
      if ((group == null) || (authorizedAccountName == null)) {
        throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
      }
      Account authorizedAccount=_accountDao.findActiveAccount(authorizedAccountName,domainId);
      if (authorizedAccount == null) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + authorizedAccountName + "String_Node_Str"+ domainId+ "String_Node_Str"+ networkGroup+ "String_Node_Str"+ protocol+ "String_Node_Str"+ startPortOrType+ "String_Node_Str"+ endPortOrCode);
        }
        throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + authorizedAccountName + "String_Node_Str"+ networkGroup+ "String_Node_Str"+ protocol+ "String_Node_Str"+ startPortOrType+ "String_Node_Str"+ endPortOrCode);
      }
      NetworkGroupVO groupVO=_networkGroupDao.findByAccountAndName(authorizedAccount.getId(),group);
      if (groupVO == null) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + accountId + "String_Node_Str"+ group);
        }
        throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + userGroup + "String_Node_Str");
      }
      authorizedGroups.add(groupVO);
    }
  }
  if (userId == null) {
    userId=Long.valueOf(1);
  }
  if (!_enabled) {
    return false;
  }
  int numDeleted=0;
  final int numToDelete=cidrList.length() + authorizedGroups.size();
  final Transaction txn=Transaction.currentTxn();
  NetworkGroupVO networkGroupHandle=_networkGroupDao.findByAccountAndName(accountId,networkGroup);
  if (networkGroupHandle == null) {
    s_logger.warn("String_Node_Str" + networkGroup);
    return false;
  }
  try {
    txn.start();
    networkGroupHandle=_networkGroupDao.acquireInLockTable(networkGroupHandle.getId());
    if (networkGroupHandle == null) {
      s_logger.warn("String_Node_Str" + networkGroup);
      return false;
    }
    for (    final NetworkGroupVO ngVO : authorizedGroups) {
      numDeleted+=_ingressRuleDao.deleteByPortProtoAndGroup(networkGroupHandle.getId(),protocol,startPort,endPort,ngVO.getId());
    }
    for (    final String cidr : cidrs) {
      numDeleted+=_ingressRuleDao.deleteByPortProtoAndCidr(networkGroupHandle.getId(),protocol,startPort,endPort,cidr);
    }
    s_logger.debug("String_Node_Str" + networkGroup + "String_Node_Str"+ numToDelete+ "String_Node_Str"+ numDeleted);
    final Set<Long> affectedVms=new HashSet<Long>();
    affectedVms.addAll(_networkGroupVMMapDao.listVmIdsByNetworkGroup(networkGroupHandle.getId()));
    scheduleRulesetUpdateToHosts(affectedVms,true,null);
    return true;
  }
 catch (  Exception e) {
    s_logger.warn("String_Node_Str",e);
    throw new CloudRuntimeException("String_Node_Str",e);
  }
 finally {
    if (networkGroup != null) {
      _networkGroupDao.releaseFromLockTable(networkGroupHandle.getId());
    }
    txn.commit();
  }
}

Fixed version (ground truth):
@Override @DB @SuppressWarnings("String_Node_Str") public boolean revokeNetworkGroupIngress(RevokeNetworkGroupIngressCmd cmd){
  Account account=UserContext.current().getAccount();
  Long userId=UserContext.current().getUserId();
  Long domainId=cmd.getDomainId();
  String accountName=cmd.getAccountName();
  Integer startPort=cmd.getStartPort();
  Integer endPort=cmd.getEndPort();
  Integer icmpType=cmd.getIcmpType();
  Integer icmpCode=cmd.getIcmpCode();
  String protocol=cmd.getProtocol();
  String networkGroup=cmd.getNetworkGroupName();
  String cidrList=cmd.getCidrList();
  Map groupList=cmd.getUserNetworkGroupList();
  String[] cidrs=null;
  Long accountId=null;
  Integer startPortOrType=null;
  Integer endPortOrCode=null;
  if (protocol == null) {
    protocol="String_Node_Str";
  }
  if (!NetUtils.isValidNetworkGroupProto(protocol)) {
    s_logger.debug("String_Node_Str" + protocol);
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + protocol);
  }
  if ("String_Node_Str".equalsIgnoreCase(protocol)) {
    if ((icmpType == null) || (icmpCode == null)) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + icmpType + "String_Node_Str"+ icmpCode);
    }
    if (icmpType == -1 && icmpCode != -1) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
    }
    if (icmpCode > 255) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
    }
    startPortOrType=icmpType;
    endPortOrCode=icmpCode;
  }
 else   if (protocol.equals("String_Node_Str")) {
    if ((startPort != null) || (endPort != null)) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
    }
    startPortOrType=0;
    endPortOrCode=0;
  }
 else {
    if ((startPort == null) || (endPort == null)) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + startPort + "String_Node_Str"+ endPort);
    }
    if (startPort == 0 && endPort == 0) {
      endPort=65535;
    }
    if (startPort > endPort) {
      s_logger.debug("String_Node_Str" + startPort + "String_Node_Str"+ endPort);
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
    }
    if (startPort > 65535 || endPort > 65535 || startPort < -1 || endPort < -1) {
      s_logger.debug("String_Node_Str" + startPort + "String_Node_Str"+ endPort);
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
    }
    if (startPort < 0 || endPort < 0) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
    }
    startPortOrType=startPort;
    endPortOrCode=endPort;
  }
  if ((account == null) || isAdmin(account.getType())) {
    if ((accountName != null) && (domainId != null)) {
      if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(),domainId)) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + networkGroup + "String_Node_Str");
        }
        throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + networkGroup + "String_Node_Str");
      }
      Account groupOwner=_accountDao.findActiveAccount(accountName,domainId);
      if (groupOwner == null) {
        throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + accountName + "String_Node_Str"+ domainId);
      }
      accountId=groupOwner.getId();
    }
 else {
      if (account != null) {
        accountId=account.getId();
        domainId=account.getDomainId();
      }
    }
  }
 else {
    if (account != null) {
      accountId=account.getId();
      domainId=account.getDomainId();
    }
  }
  if (accountId == null) {
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + networkGroup + "String_Node_Str");
  }
  NetworkGroupVO sg=_networkGroupDao.findByAccountAndName(accountId,networkGroup);
  if (sg == null) {
    s_logger.debug("String_Node_Str" + networkGroup);
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + networkGroup);
  }
  if (cidrList == null && groupList == null) {
    s_logger.debug("String_Node_Str");
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
  }
  List<String> authorizedCidrs=new ArrayList<String>();
  if (cidrList != null) {
    if (protocol.equals("String_Node_Str")) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
    }
    cidrs=cidrList.split("String_Node_Str");
    for (    String cidr : cidrs) {
      if (!NetUtils.isValidCIDR(cidr)) {
        s_logger.debug("String_Node_Str" + cidr + "String_Node_Str");
        throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + cidr + "String_Node_Str");
      }
      authorizedCidrs.add(cidr);
    }
  }
  List<NetworkGroupVO> authorizedGroups=new ArrayList<NetworkGroupVO>();
  if (groupList != null) {
    Collection userGroupCollection=groupList.values();
    Iterator iter=userGroupCollection.iterator();
    while (iter.hasNext()) {
      HashMap userGroup=(HashMap)iter.next();
      String group=(String)userGroup.get("String_Node_Str");
      String authorizedAccountName=(String)userGroup.get("String_Node_Str");
      if ((group == null) || (authorizedAccountName == null)) {
        throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
      }
      Account authorizedAccount=_accountDao.findActiveAccount(authorizedAccountName,domainId);
      if (authorizedAccount == null) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + authorizedAccountName + "String_Node_Str"+ domainId+ "String_Node_Str"+ networkGroup+ "String_Node_Str"+ protocol+ "String_Node_Str"+ startPortOrType+ "String_Node_Str"+ endPortOrCode);
        }
        throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + authorizedAccountName + "String_Node_Str"+ networkGroup+ "String_Node_Str"+ protocol+ "String_Node_Str"+ startPortOrType+ "String_Node_Str"+ endPortOrCode);
      }
      NetworkGroupVO groupVO=_networkGroupDao.findByAccountAndName(authorizedAccount.getId(),group);
      if (groupVO == null) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + accountId + "String_Node_Str"+ group);
        }
        throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + userGroup + "String_Node_Str");
      }
      authorizedGroups.add(groupVO);
    }
  }
  if (userId == null) {
    userId=Long.valueOf(1);
  }
  if (!_enabled) {
    return false;
  }
  int numDeleted=0;
  final int numToDelete=cidrList.length() + authorizedGroups.size();
  final Transaction txn=Transaction.currentTxn();
  NetworkGroupVO networkGroupHandle=_networkGroupDao.findByAccountAndName(accountId,networkGroup);
  if (networkGroupHandle == null) {
    s_logger.warn("String_Node_Str" + networkGroup);
    return false;
  }
  try {
    txn.start();
    networkGroupHandle=_networkGroupDao.acquireInLockTable(networkGroupHandle.getId());
    if (networkGroupHandle == null) {
      s_logger.warn("String_Node_Str" + networkGroup);
      return false;
    }
    for (    final NetworkGroupVO ngVO : authorizedGroups) {
      numDeleted+=_ingressRuleDao.deleteByPortProtoAndGroup(networkGroupHandle.getId(),protocol,startPort,endPort,ngVO.getId());
    }
    for (    final String cidr : cidrs) {
      numDeleted+=_ingressRuleDao.deleteByPortProtoAndCidr(networkGroupHandle.getId(),protocol,startPort,endPort,cidr);
    }
    s_logger.debug("String_Node_Str" + networkGroup + "String_Node_Str"+ numToDelete+ "String_Node_Str"+ numDeleted);
    final Set<Long> affectedVms=new HashSet<Long>();
    affectedVms.addAll(_networkGroupVMMapDao.listVmIdsByNetworkGroup(networkGroupHandle.getId()));
    scheduleRulesetUpdateToHosts(affectedVms,true,null);
    return true;
  }
 catch (  Exception e) {
    s_logger.warn("String_Node_Str",e);
    throw new CloudRuntimeException("String_Node_Str",e);
  }
 finally {
    if (networkGroup != null) {
      _networkGroupDao.releaseFromLockTable(networkGroupHandle.getId());
    }
    txn.commit();
  }
}