Program: 4411
Project: 4411
Buggy source:
/** 
 * Returns true if this has slots for an incoming connection, <b>without accounting for this' ultrapeer capabilities</b>.  More specifically: <ul> <li>if ultrapeerHeader==null, returns true if this has space for an unrouted old-style connection. <li>if ultrapeerHeader.equals("true"), returns true if this has slots for a leaf connection. <li>if ultrapeerHeader.equals("false"), returns true if this has slots for an ultrapeer connection. </ul> <tt>useragentHeader</tt> is used to prefer LimeWire and certain trusted vendors.  <tt>outgoing</tt> is currently unused, but may be used to prefer incoming or outgoing connections in the forward.
 * @param outgoing true if this is an outgoing connection; true if incoming
 * @param ultrapeerHeader the value of the X-Ultrapeer header, or nullif it was not written
 * @param useragentHeader the value of the User-Agent header, or null ifit was not written
 * @return true if a connection of the given type is allowed
 */
public boolean allowConnection(HandshakeResponse hr,boolean leaf){
  if (!ConnectionSettings.PREFERENCING_ACTIVE.getValue())   return true;
  int limeAttempts=ConnectionSettings.LIME_ATTEMPTS.getValue();
  if (!ConnectionSettings.ALLOW_WHILE_DISCONNECTED.getValue() && _preferredConnections <= 0) {
    return false;
  }
 else   if (RouterService.isShieldedLeaf()) {
    if (Sockets.getAttempts() < limeAttempts ? !hr.isLimeWire() : !hr.isGoodUltrapeer()) {
      return false;
    }
 else     if (_shieldedConnections < _preferredConnections) {
      if (checkLocale(hr.getLocalePref()))       _needPref=false;
      return true;
    }
 else {
      if (_needPref && checkLocale(hr.getLocalePref()))       return true;
      return false;
    }
  }
 else   if (hr.isLeaf() || leaf) {
    if (!allowUltrapeer2LeafConnection(hr)) {
      return false;
    }
    int leaves=getNumInitializedClientConnections();
    int nonLimeWireLeaves=_nonLimeWireLeaves;
    if (!hr.isLimeWire()) {
      if (leaves < UltrapeerSettings.MAX_LEAVES.getValue() && nonLimeWireLeaves < RESERVED_NON_LIMEWIRE_LEAVES) {
        return true;
      }
 else {
        return false;
      }
    }
    if (hr.isGoodLeaf()) {
      return (leaves + Math.max(0,RESERVED_NON_LIMEWIRE_LEAVES - nonLimeWireLeaves)) < UltrapeerSettings.MAX_LEAVES.getValue();
    }
    return leaves < (UltrapeerSettings.MAX_LEAVES.getValue() - RESERVED_GOOD_LEAF_CONNECTIONS);
  }
 else   if (hr.isUltrapeer()) {
    int peers=getNumInitializedConnections();
    int nonLimeWirePeers=_nonLimeWirePeers;
    int locale_num=0;
    if (peers == 0 && Sockets.getAttempts() < limeAttempts ? hr.isLimeWire() && hr.isGoodUltrapeer() : hr.isGoodUltrapeer()) {
      if (isIdle())       return hr.isLimeWire();
      if (!ConnectionSettings.USE_LOCALE_PREF.getValue() || _preferredConnections > ConnectionSettings.NUM_LOCALE_PREF.getValue())       return true;
    }
    if (!allowUltrapeer2UltrapeerConnection(hr)) {
      return false;
    }
    if (ConnectionSettings.USE_LOCALE_PREF.getValue()) {
      if (checkLocale(hr.getLocalePref()) && _localeMatchingPeers < ConnectionSettings.NUM_LOCALE_PREF.getValue()) {
        return true;
      }
      locale_num=getNumLimeWireLocalePrefSlots();
    }
    if (!hr.isLimeWire()) {
      if (peers < _preferredConnections && nonLimeWirePeers < RESERVED_NON_LIMEWIRE_PEERS) {
        return true;
      }
    }
    return (peers + Math.max(0,RESERVED_NON_LIMEWIRE_PEERS - nonLimeWirePeers) + locale_num) < _preferredConnections;
  }
  return false;
}

Fixed version (ground truth):
/** 
 * Returns true if this has slots for an incoming connection, <b>without accounting for this' ultrapeer capabilities</b>.  More specifically: <ul> <li>if ultrapeerHeader==null, returns true if this has space for an unrouted old-style connection. <li>if ultrapeerHeader.equals("true"), returns true if this has slots for a leaf connection. <li>if ultrapeerHeader.equals("false"), returns true if this has slots for an ultrapeer connection. </ul> <tt>useragentHeader</tt> is used to prefer LimeWire and certain trusted vendors.  <tt>outgoing</tt> is currently unused, but may be used to prefer incoming or outgoing connections in the forward.
 * @param outgoing true if this is an outgoing connection; true if incoming
 * @param ultrapeerHeader the value of the X-Ultrapeer header, or nullif it was not written
 * @param useragentHeader the value of the User-Agent header, or null ifit was not written
 * @return true if a connection of the given type is allowed
 */
public boolean allowConnection(HandshakeResponse hr,boolean leaf){
  if (!ConnectionSettings.PREFERENCING_ACTIVE.getValue())   return true;
  int limeAttempts=ConnectionSettings.LIME_ATTEMPTS.getValue();
  if (!ConnectionSettings.ALLOW_WHILE_DISCONNECTED.getValue() && _preferredConnections <= 0) {
    return false;
  }
 else   if (RouterService.isShieldedLeaf()) {
    if (Sockets.getAttempts() < limeAttempts ? !hr.isLimeWire() : !hr.isGoodUltrapeer()) {
      return false;
    }
 else     if (_shieldedConnections < _preferredConnections) {
      if (checkLocale(hr.getLocalePref()))       _needPref=false;
      return true;
    }
 else {
      if (_needPref && checkLocale(hr.getLocalePref()))       return true;
      return false;
    }
  }
 else   if (hr.isLeaf() || leaf) {
    if (!allowUltrapeer2LeafConnection(hr)) {
      return false;
    }
    int leaves=getNumInitializedClientConnections();
    int nonLimeWireLeaves=_nonLimeWireLeaves;
    if (!hr.isLimeWire()) {
      if (leaves < UltrapeerSettings.MAX_LEAVES.getValue() && nonLimeWireLeaves < RESERVED_NON_LIMEWIRE_LEAVES) {
        return true;
      }
 else {
        return false;
      }
    }
    if (hr.isGoodLeaf()) {
      return (leaves + Math.max(0,RESERVED_NON_LIMEWIRE_LEAVES - nonLimeWireLeaves)) < UltrapeerSettings.MAX_LEAVES.getValue();
    }
    return leaves < (UltrapeerSettings.MAX_LEAVES.getValue() - RESERVED_GOOD_LEAF_CONNECTIONS);
  }
 else   if (hr.isUltrapeer()) {
    int peers=getNumInitializedConnections();
    int nonLimeWirePeers=_nonLimeWirePeers;
    int locale_num=0;
    if (peers == 0 && (Sockets.getAttempts() < limeAttempts ? (hr.isLimeWire() && hr.isGoodUltrapeer()) : hr.isGoodUltrapeer())) {
      if (isIdle())       return hr.isLimeWire();
      if (!ConnectionSettings.USE_LOCALE_PREF.getValue() || _preferredConnections > ConnectionSettings.NUM_LOCALE_PREF.getValue())       return true;
    }
    if (!allowUltrapeer2UltrapeerConnection(hr)) {
      return false;
    }
    if (ConnectionSettings.USE_LOCALE_PREF.getValue()) {
      if (checkLocale(hr.getLocalePref()) && _localeMatchingPeers < ConnectionSettings.NUM_LOCALE_PREF.getValue()) {
        return true;
      }
      locale_num=getNumLimeWireLocalePrefSlots();
    }
    if (!hr.isLimeWire()) {
      if (peers < _preferredConnections && nonLimeWirePeers < RESERVED_NON_LIMEWIRE_PEERS) {
        return true;
      }
    }
    return (peers + Math.max(0,RESERVED_NON_LIMEWIRE_PEERS - nonLimeWirePeers) + locale_num) < _preferredConnections;
  }
  return false;
}