Program: 106114
Project: 106114
Buggy source:
@Override @DB public void allocateDirectIp(final NicProfile nic,final DataCenter dc,final VirtualMachineProfile vm,final Network network,final String requestedIpv4,final String requestedIpv6) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException {
  Transaction.execute(new TransactionCallbackWithExceptionNoReturn<InsufficientAddressCapacityException>(){
    @Override public void doInTransactionWithoutResult(    TransactionStatus status) throws InsufficientAddressCapacityException {
      boolean ipv4=false;
      if (network.getGateway() != null) {
        if (nic.getIp4Address() == null) {
          ipv4=true;
          PublicIp ip=null;
          if (requestedIpv4 != null && vm.getType() == VirtualMachine.Type.DomainRouter) {
            Nic placeholderNic=_networkModel.getPlaceholderNicForRouter(network,null);
            if (placeholderNic != null) {
              IPAddressVO userIp=_ipAddressDao.findByIpAndSourceNetworkId(network.getId(),placeholderNic.getIp4Address());
              ip=PublicIp.createFromAddrAndVlan(userIp,_vlanDao.findById(userIp.getVlanId()));
              s_logger.debug("String_Node_Str" + placeholderNic.getIp4Address() + "String_Node_Str"+ network);
            }
          }
          if (ip == null) {
            ip=assignPublicIpAddress(dc.getId(),null,vm.getOwner(),VlanType.DirectAttached,network.getId(),requestedIpv4,false);
          }
          nic.setIp4Address(ip.getAddress().toString());
          nic.setGateway(ip.getGateway());
          nic.setNetmask(ip.getNetmask());
          nic.setIsolationUri(IsolationType.Vlan.toUri(ip.getVlanTag()));
          nic.setBroadcastType(network.getBroadcastDomainType());
          nic.setBroadcastUri(network.getBroadcastUri());
          nic.setFormat(AddressFormat.Ip4);
          nic.setReservationId(String.valueOf(ip.getVlanTag()));
          nic.setMacAddress(ip.getMacAddress());
        }
        nic.setDns1(dc.getDns1());
        nic.setDns2(dc.getDns2());
      }
      if (network.getIp6Gateway() != null) {
        if (nic.getIp6Address() == null) {
          UserIpv6Address ip=_ipv6Mgr.assignDirectIp6Address(dc.getId(),vm.getOwner(),network.getId(),requestedIpv6);
          Vlan vlan=_vlanDao.findById(ip.getVlanId());
          nic.setIp6Address(ip.getAddress().toString());
          nic.setIp6Gateway(vlan.getIp6Gateway());
          nic.setIp6Cidr(vlan.getIp6Cidr());
          if (ipv4) {
            nic.setFormat(AddressFormat.DualStack);
          }
 else {
            nic.setIsolationUri(IsolationType.Vlan.toUri(vlan.getVlanTag()));
            nic.setBroadcastType(BroadcastDomainType.Vlan);
            nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlan.getVlanTag()));
            nic.setFormat(AddressFormat.Ip6);
            nic.setReservationId(String.valueOf(vlan.getVlanTag()));
            nic.setMacAddress(ip.getMacAddress());
          }
        }
        nic.setIp6Dns1(dc.getIp6Dns1());
        nic.setIp6Dns2(dc.getIp6Dns2());
      }
    }
  }
);
}

Fixed version (ground truth):
@Override @DB public void allocateDirectIp(final NicProfile nic,final DataCenter dc,final VirtualMachineProfile vm,final Network network,final String requestedIpv4,final String requestedIpv6) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException {
  Transaction.execute(new TransactionCallbackWithExceptionNoReturn<InsufficientAddressCapacityException>(){
    @Override public void doInTransactionWithoutResult(    TransactionStatus status) throws InsufficientAddressCapacityException {
      boolean ipv4=false;
      if (network.getGateway() != null) {
        if (nic.getIp4Address() == null) {
          ipv4=true;
          PublicIp ip=null;
          if (requestedIpv4 != null && vm.getType() == VirtualMachine.Type.DomainRouter) {
            Nic placeholderNic=_networkModel.getPlaceholderNicForRouter(network,null);
            if (placeholderNic != null) {
              IPAddressVO userIp=_ipAddressDao.findByIpAndSourceNetworkId(network.getId(),placeholderNic.getIp4Address());
              ip=PublicIp.createFromAddrAndVlan(userIp,_vlanDao.findById(userIp.getVlanId()));
              s_logger.debug("String_Node_Str" + placeholderNic.getIp4Address() + "String_Node_Str"+ network);
            }
          }
          if (ip == null) {
            ip=assignPublicIpAddress(dc.getId(),null,vm.getOwner(),VlanType.DirectAttached,network.getId(),requestedIpv4,false);
          }
          nic.setIp4Address(ip.getAddress().toString());
          nic.setGateway(ip.getGateway());
          nic.setNetmask(ip.getNetmask());
          nic.setIsolationUri(IsolationType.Vlan.toUri(ip.getVlanTag()));
          nic.setBroadcastType(network.getBroadcastDomainType());
          if (network.getBroadcastUri() != null)           nic.setBroadcastUri(network.getBroadcastUri());
 else           nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(ip.getVlanTag()));
          nic.setFormat(AddressFormat.Ip4);
          nic.setReservationId(String.valueOf(ip.getVlanTag()));
          nic.setMacAddress(ip.getMacAddress());
        }
        nic.setDns1(dc.getDns1());
        nic.setDns2(dc.getDns2());
      }
      if (network.getIp6Gateway() != null) {
        if (nic.getIp6Address() == null) {
          UserIpv6Address ip=_ipv6Mgr.assignDirectIp6Address(dc.getId(),vm.getOwner(),network.getId(),requestedIpv6);
          Vlan vlan=_vlanDao.findById(ip.getVlanId());
          nic.setIp6Address(ip.getAddress().toString());
          nic.setIp6Gateway(vlan.getIp6Gateway());
          nic.setIp6Cidr(vlan.getIp6Cidr());
          if (ipv4) {
            nic.setFormat(AddressFormat.DualStack);
          }
 else {
            nic.setIsolationUri(IsolationType.Vlan.toUri(vlan.getVlanTag()));
            nic.setBroadcastType(BroadcastDomainType.Vlan);
            nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlan.getVlanTag()));
            nic.setFormat(AddressFormat.Ip6);
            nic.setReservationId(String.valueOf(vlan.getVlanTag()));
            nic.setMacAddress(ip.getMacAddress());
          }
        }
        nic.setIp6Dns1(dc.getIp6Dns1());
        nic.setIp6Dns2(dc.getIp6Dns2());
      }
    }
  }
);
}