Program: 107336
Project: 107336
Buggy source:
public List<FirewallRuleVO> updateFirewallRules(final HostVO host,final String routerName,final String routerIp,final List<FirewallRuleVO> fwRules){
  final List<FirewallRuleVO> result=new ArrayList<FirewallRuleVO>();
  if (fwRules.size() == 0) {
    s_logger.debug("String_Node_Str");
    return result;
  }
  Commands cmds=new Commands(OnError.Continue);
  final List<FirewallRuleVO> lbRules=new ArrayList<FirewallRuleVO>();
  final List<FirewallRuleVO> fwdRules=new ArrayList<FirewallRuleVO>();
  int i=0;
  for (  FirewallRuleVO rule : fwRules) {
    IPAddressVO ip=_ipAddressDao.findById(rule.getPublicIpAddress());
    VlanVO vlan=_vlanDao.findById(new Long(ip.getVlanDbId()));
    String vlanNetmask=vlan.getVlanNetmask();
    rule.setVlanNetmask(vlanNetmask);
    if (rule.isForwarding()) {
      fwdRules.add(rule);
      final SetFirewallRuleCommand cmd=new SetFirewallRuleCommand(routerName,routerIp,rule,false);
      cmds.addCommand(cmd);
    }
 else     if (rule.getGroupId() != null) {
      lbRules.add(rule);
    }
  }
  if (lbRules.size() > 0) {
    final LoadBalancerConfigurator cfgrtr=new HAProxyConfigurator();
    final String[] cfg=cfgrtr.generateConfiguration(fwRules);
    final String[][] addRemoveRules=cfgrtr.generateFwRules(fwRules);
    final LoadBalancerCfgCommand cmd=new LoadBalancerCfgCommand(cfg,addRemoveRules,routerName,routerIp);
    cmds.addCommand(cmd);
  }
  if (cmds.size() == 0) {
    return result;
  }
  Answer[] answers=null;
  try {
    answers=_agentMgr.send(host.getId(),cmds);
  }
 catch (  final AgentUnavailableException e) {
    s_logger.warn("String_Node_Str",e);
  }
catch (  final OperationTimedoutException e) {
    s_logger.warn("String_Node_Str",e);
  }
  if (answers == null) {
    return result;
  }
  i=0;
  for (  final FirewallRuleVO rule : fwdRules) {
    final Answer ans=answers[i++];
    if (ans != null) {
      if (ans.getResult()) {
        result.add(rule);
      }
 else {
        s_logger.warn("String_Node_Str" + rule.toString());
      }
    }
  }
  if (i == (answers.length - 1)) {
    final Answer lbAnswer=answers[i];
    if (lbAnswer.getResult()) {
      result.addAll(lbRules);
    }
 else {
      s_logger.warn("String_Node_Str");
    }
  }
  return result;
}

Fixed version (ground truth):
public List<FirewallRuleVO> updateFirewallRules(final HostVO host,final String routerName,final String routerIp,final List<FirewallRuleVO> fwRules){
  final List<FirewallRuleVO> result=new ArrayList<FirewallRuleVO>();
  if (fwRules.size() == 0) {
    s_logger.debug("String_Node_Str");
    return result;
  }
  Commands cmds=new Commands(OnError.Continue);
  final List<FirewallRuleVO> lbRules=new ArrayList<FirewallRuleVO>();
  final List<FirewallRuleVO> fwdRules=new ArrayList<FirewallRuleVO>();
  int i=0;
  for (  FirewallRuleVO rule : fwRules) {
    IPAddressVO ip=_ipAddressDao.findById(rule.getPublicIpAddress());
    VlanVO vlan=_vlanDao.findById(new Long(ip.getVlanDbId()));
    String vlanNetmask=vlan.getVlanNetmask();
    rule.setVlanNetmask(vlanNetmask);
    if (rule.isForwarding()) {
      fwdRules.add(rule);
      final SetFirewallRuleCommand cmd=new SetFirewallRuleCommand(routerName,routerIp,rule,true);
      cmds.addCommand(cmd);
    }
 else     if (rule.getGroupId() != null) {
      lbRules.add(rule);
    }
  }
  if (lbRules.size() > 0) {
    final LoadBalancerConfigurator cfgrtr=new HAProxyConfigurator();
    final String[] cfg=cfgrtr.generateConfiguration(fwRules);
    final String[][] addRemoveRules=cfgrtr.generateFwRules(fwRules);
    final LoadBalancerCfgCommand cmd=new LoadBalancerCfgCommand(cfg,addRemoveRules,routerName,routerIp);
    cmds.addCommand(cmd);
  }
  if (cmds.size() == 0) {
    return result;
  }
  Answer[] answers=null;
  try {
    answers=_agentMgr.send(host.getId(),cmds);
  }
 catch (  final AgentUnavailableException e) {
    s_logger.warn("String_Node_Str",e);
  }
catch (  final OperationTimedoutException e) {
    s_logger.warn("String_Node_Str",e);
  }
  if (answers == null) {
    return result;
  }
  i=0;
  for (  final FirewallRuleVO rule : fwdRules) {
    final Answer ans=answers[i++];
    if (ans != null) {
      if (ans.getResult()) {
        result.add(rule);
      }
 else {
        s_logger.warn("String_Node_Str" + rule.toString());
      }
    }
  }
  if (i == (answers.length - 1)) {
    final Answer lbAnswer=answers[i];
    if (lbAnswer.getResult()) {
      result.addAll(lbRules);
    }
 else {
      s_logger.warn("String_Node_Str");
    }
  }
  return result;
}