Program: 107367
Project: 107367
Buggy source:
@Override @DB public boolean deletePortForwardingRule(DeletePortForwardingRuleCmd cmd){
  Long ruleId=cmd.getId();
  Long userId=UserContext.current().getUserId();
  Account account=UserContext.current().getAccount();
  FirewallRuleVO rule=_firewallRulesDao.findById(ruleId);
  if (rule == null) {
    throw new InvalidParameterValueException("String_Node_Str" + ruleId);
  }
  String publicIp=rule.getPublicIpAddress();
  String privateIp=rule.getPrivateIpAddress();
  IPAddressVO ipAddress=_ipAddressDao.findById(publicIp);
  if (ipAddress == null) {
    throw new InvalidParameterValueException("String_Node_Str" + ruleId);
  }
  String privatePort=rule.getPrivatePort();
  String publicPort=rule.getPublicPort();
  if (!NetUtils.isValidPort(publicPort) || !NetUtils.isValidPort(privatePort)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  String proto=rule.getProtocol();
  if (!NetUtils.isValidProto(proto)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  Account ruleOwner=_accountDao.findById(ipAddress.getAccountId());
  if (ruleOwner == null) {
    throw new InvalidParameterValueException("String_Node_Str" + ruleId);
  }
  if (account != null) {
    if (isAdmin(account.getType())) {
      if (!_domainDao.isChildDomain(account.getDomainId(),ruleOwner.getDomainId())) {
        throw new PermissionDeniedException("String_Node_Str" + ruleId + "String_Node_Str");
      }
    }
 else     if (account.getId() != ruleOwner.getId()) {
      throw new PermissionDeniedException("String_Node_Str" + ruleId + "String_Node_Str");
    }
  }
  Transaction txn=Transaction.currentTxn();
  boolean locked=false;
  boolean success=false;
  try {
    IPAddressVO ipVO=_ipAddressDao.acquireInLockTable(publicIp);
    if (ipVO == null) {
      throw new PermissionDeniedException("String_Node_Str");
    }
    locked=true;
    txn.start();
    List<FirewallRuleVO> fwdings=_firewallRulesDao.listIPForwardingForUpdate(publicIp,publicPort,proto);
    FirewallRuleVO fwRule=null;
    if (fwdings.size() == 0) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
 else     if (fwdings.size() == 1) {
      fwRule=fwdings.get(0);
      if (fwRule.getPrivateIpAddress().equalsIgnoreCase(privateIp) && fwRule.getPrivatePort().equals(privatePort)) {
        _firewallRulesDao.expunge(fwRule.getId());
      }
 else {
        throw new InvalidParameterValueException("String_Node_Str");
      }
    }
 else {
      throw new CloudRuntimeException("String_Node_Str");
    }
    fwRule.setEnabled(false);
    success=updateFirewallRule(fwRule,null,null);
    String description;
    String type=EventTypes.EVENT_NET_RULE_DELETE;
    String level=EventVO.LEVEL_INFO;
    String ruleName=rule.isForwarding() ? "String_Node_Str" : "String_Node_Str";
    if (success) {
      description="String_Node_Str" + ruleName + "String_Node_Str"+ publicIp+ "String_Node_Str"+ rule.getPublicPort()+ "String_Node_Str"+ rule.getPrivateIpAddress()+ "String_Node_Str"+ rule.getPrivatePort()+ "String_Node_Str"+ rule.getProtocol();
    }
 else {
      level=EventVO.LEVEL_ERROR;
      description="String_Node_Str" + ruleName + "String_Node_Str"+ publicIp+ "String_Node_Str"+ rule.getPublicPort()+ "String_Node_Str"+ rule.getPrivateIpAddress()+ "String_Node_Str"+ rule.getPrivatePort()+ "String_Node_Str"+ rule.getProtocol();
    }
    EventUtils.saveEvent(userId,ipAddress.getAccountId(),level,type,description);
    txn.commit();
  }
 catch (  Exception ex) {
    txn.rollback();
    s_logger.error("String_Node_Str" + ruleId,ex);
    return false;
  }
 finally {
    if (locked) {
      _ipAddressDao.releaseFromLockTable(publicIp);
    }
    txn.close();
  }
  return success;
}

Fixed version (ground truth):
@Override @DB public boolean deletePortForwardingRule(Long id,boolean sysContext){
  Long ruleId=id;
  Long userId=null;
  Account account=null;
  if (sysContext) {
    userId=User.UID_SYSTEM;
    account=_accountDao.findById(User.UID_SYSTEM);
  }
 else {
    userId=UserContext.current().getUserId();
    account=UserContext.current().getAccount();
  }
  FirewallRuleVO rule=_firewallRulesDao.findById(ruleId);
  if (rule == null) {
    throw new InvalidParameterValueException("String_Node_Str" + ruleId);
  }
  String publicIp=rule.getPublicIpAddress();
  String privateIp=rule.getPrivateIpAddress();
  IPAddressVO ipAddress=_ipAddressDao.findById(publicIp);
  if (ipAddress == null) {
    throw new InvalidParameterValueException("String_Node_Str" + ruleId);
  }
  String privatePort=rule.getPrivatePort();
  String publicPort=rule.getPublicPort();
  if (!NetUtils.isValidPort(publicPort) || !NetUtils.isValidPort(privatePort)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  String proto=rule.getProtocol();
  if (!NetUtils.isValidProto(proto)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  Account ruleOwner=_accountDao.findById(ipAddress.getAccountId());
  if (ruleOwner == null) {
    throw new InvalidParameterValueException("String_Node_Str" + ruleId);
  }
  if (account != null) {
    if (isAdmin(account.getType())) {
      if (!_domainDao.isChildDomain(account.getDomainId(),ruleOwner.getDomainId())) {
        throw new PermissionDeniedException("String_Node_Str" + ruleId + "String_Node_Str");
      }
    }
 else     if (account.getId() != ruleOwner.getId()) {
      throw new PermissionDeniedException("String_Node_Str" + ruleId + "String_Node_Str");
    }
  }
  Transaction txn=Transaction.currentTxn();
  boolean locked=false;
  boolean success=false;
  try {
    IPAddressVO ipVO=_ipAddressDao.acquireInLockTable(publicIp);
    if (ipVO == null) {
      throw new PermissionDeniedException("String_Node_Str");
    }
    locked=true;
    txn.start();
    List<FirewallRuleVO> fwdings=_firewallRulesDao.listIPForwardingForUpdate(publicIp,publicPort,proto);
    FirewallRuleVO fwRule=null;
    if (fwdings.size() == 0) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
 else     if (fwdings.size() == 1) {
      fwRule=fwdings.get(0);
      if (fwRule.getPrivateIpAddress().equalsIgnoreCase(privateIp) && fwRule.getPrivatePort().equals(privatePort)) {
        _firewallRulesDao.expunge(fwRule.getId());
      }
 else {
        throw new InvalidParameterValueException("String_Node_Str");
      }
    }
 else {
      throw new CloudRuntimeException("String_Node_Str");
    }
    fwRule.setEnabled(false);
    success=updateFirewallRule(fwRule,null,null);
    String description;
    String type=EventTypes.EVENT_NET_RULE_DELETE;
    String level=EventVO.LEVEL_INFO;
    String ruleName=rule.isForwarding() ? "String_Node_Str" : "String_Node_Str";
    if (success) {
      description="String_Node_Str" + ruleName + "String_Node_Str"+ publicIp+ "String_Node_Str"+ rule.getPublicPort()+ "String_Node_Str"+ rule.getPrivateIpAddress()+ "String_Node_Str"+ rule.getPrivatePort()+ "String_Node_Str"+ rule.getProtocol();
    }
 else {
      level=EventVO.LEVEL_ERROR;
      description="String_Node_Str" + ruleName + "String_Node_Str"+ publicIp+ "String_Node_Str"+ rule.getPublicPort()+ "String_Node_Str"+ rule.getPrivateIpAddress()+ "String_Node_Str"+ rule.getPrivatePort()+ "String_Node_Str"+ rule.getProtocol();
    }
    EventUtils.saveEvent(userId,ipAddress.getAccountId(),level,type,description);
    txn.commit();
  }
 catch (  Exception ex) {
    txn.rollback();
    s_logger.error("String_Node_Str" + ruleId,ex);
    return false;
  }
 finally {
    if (locked) {
      _ipAddressDao.releaseFromLockTable(publicIp);
    }
    txn.close();
  }
  return success;
}