Program: 4312
Project: 4312
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;
  if (!hr.isLeaf() && !hr.isUltrapeer())   return false;
  int limeAttempts=ConnectionSettings.LIME_ATTEMPTS.getValue();
  if (!ConnectionSettings.ALLOW_WHILE_DISCONNECTED.getValue() && _preferredConnections <= 0) {
    return false;
  }
 else   if (isShieldedLeaf() || !isSupernode()) {
    if (!hr.isUltrapeer()) {
      return false;
    }
    if (!hr.isGoodUltrapeer() || (_connectionAttempts < limeAttempts && !hr.isLimeWire())) {
      return false;
    }
 else     if (_shieldedConnections < _preferredConnections) {
      if (checkLocale(hr.getLocalePref()))       _needPref=false;
      if (isIdle()) {
        return hr.isLimeWire();
      }
      return true;
    }
 else {
      if (_needPref && checkLocale(hr.getLocalePref())) {
        return true;
      }
      return false;
    }
  }
 else   if (hr.isLeaf() || leaf) {
    if (isShieldedLeaf() || !isSupernode()) {
      return false;
    }
    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;
      }
    }
    if (!hr.isGoodLeaf()) {
      return false;
    }
    return (leaves + Math.max(0,RESERVED_NON_LIMEWIRE_LEAVES - nonLimeWireLeaves)) < UltrapeerSettings.MAX_LEAVES.getValue();
  }
 else   if (hr.isGoodUltrapeer()) {
    int peers=getNumInitializedConnections();
    int nonLimeWirePeers=_nonLimeWirePeers;
    int locale_num=0;
    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()) {
      double nonLimeRatio=((double)nonLimeWirePeers) / _preferredConnections;
      if (nonLimeRatio < ConnectionSettings.MIN_NON_LIME_PEERS.getValue())       return true;
      return (nonLimeRatio < ConnectionSettings.MAX_NON_LIME_PEERS.getValue());
    }
 else {
      int minNonLime=(int)(ConnectionSettings.MIN_NON_LIME_PEERS.getValue() * _preferredConnections);
      return (peers + Math.max(0,minNonLime - 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 HandshakeStatus allowConnection(HandshakeResponse hr,boolean leaf){
  if (!ConnectionSettings.PREFERENCING_ACTIVE.getValue())   return HandshakeStatus.OK;
  if (!hr.isLeaf() && !hr.isUltrapeer())   return HandshakeStatus.NO_X_ULTRAPEER;
  int limeAttempts=ConnectionSettings.LIME_ATTEMPTS.getValue();
  if (!ConnectionSettings.ALLOW_WHILE_DISCONNECTED.getValue() && _preferredConnections <= 0) {
    return HandshakeStatus.DISCONNECTED;
  }
 else   if (isShieldedLeaf() || !isSupernode()) {
    if (!hr.isUltrapeer()) {
      return HandshakeStatus.WE_ARE_LEAVES;
    }
    if (!hr.isGoodUltrapeer()) {
      return HandshakeStatus.NOT_GOOD_UP;
    }
 else     if (_connectionAttempts < limeAttempts && !hr.isLimeWire()) {
      return HandshakeStatus.STARTING_LIMEWIRE;
    }
 else     if (_shieldedConnections < _preferredConnections) {
      if (checkLocale(hr.getLocalePref()))       _needPref=false;
      if (isIdle()) {
        if (hr.isLimeWire())         return HandshakeStatus.OK;
 else         return HandshakeStatus.IDLE_LIMEWIRE;
      }
      return HandshakeStatus.OK;
    }
 else {
      if (_needPref && checkLocale(hr.getLocalePref())) {
        _needPref=false;
        return HandshakeStatus.OK;
      }
      System.out.println("String_Node_Str" + _shieldedConnections + "String_Node_Str"+ _preferredConnections);
      return HandshakeStatus.TOO_MANY_UPS;
    }
  }
 else   if (hr.isLeaf() || leaf) {
    if (isShieldedLeaf() || !isSupernode()) {
      return HandshakeStatus.WE_ARE_LEAVES;
    }
    if (!allowUltrapeer2LeafConnection(hr)) {
      return HandshakeStatus.NOT_ALLOWED_LEAF;
    }
    int leaves=getNumInitializedClientConnections();
    int nonLimeWireLeaves=_nonLimeWireLeaves;
    if (!hr.isLimeWire()) {
      if (leaves < UltrapeerSettings.MAX_LEAVES.getValue() && nonLimeWireLeaves < RESERVED_NON_LIMEWIRE_LEAVES) {
        return HandshakeStatus.OK;
      }
    }
    if (!hr.isGoodLeaf()) {
      return HandshakeStatus.NOT_GOOD_LEAF;
    }
    if ((leaves + Math.max(0,RESERVED_NON_LIMEWIRE_LEAVES - nonLimeWireLeaves)) < UltrapeerSettings.MAX_LEAVES.getValue()) {
      return HandshakeStatus.OK;
    }
 else {
      return HandshakeStatus.TOO_MANY_LEAF;
    }
  }
 else   if (hr.isUltrapeer()) {
    int peers=getNumInitializedConnections();
    int nonLimeWirePeers=_nonLimeWirePeers;
    int locale_num=0;
    if (!allowUltrapeer2UltrapeerConnection(hr)) {
      return HandshakeStatus.NOT_ALLOWED_UP;
    }
    if (ConnectionSettings.USE_LOCALE_PREF.getValue()) {
      if (checkLocale(hr.getLocalePref()) && _localeMatchingPeers < ConnectionSettings.NUM_LOCALE_PREF.getValue()) {
        return HandshakeStatus.OK;
      }
      locale_num=getNumLimeWireLocalePrefSlots();
    }
    if (!hr.isLimeWire()) {
      double nonLimeRatio=((double)nonLimeWirePeers) / _preferredConnections;
      if (nonLimeRatio < ConnectionSettings.MIN_NON_LIME_PEERS.getValue())       return HandshakeStatus.OK;
      if (!hr.isGoodUltrapeer()) {
        return HandshakeStatus.NOT_GOOD_UP;
      }
 else       if (nonLimeRatio < ConnectionSettings.MAX_NON_LIME_PEERS.getValue()) {
        return HandshakeStatus.OK;
      }
 else {
        return HandshakeStatus.NON_LIME_RATIO;
      }
    }
 else {
      int minNonLime=(int)(ConnectionSettings.MIN_NON_LIME_PEERS.getValue() * _preferredConnections);
      if (!hr.isGoodUltrapeer()) {
        return HandshakeStatus.NOT_GOOD_UP;
      }
 else       if ((peers + Math.max(0,minNonLime - nonLimeWirePeers) + locale_num) < _preferredConnections) {
        return HandshakeStatus.OK;
      }
 else {
        return HandshakeStatus.NO_LIME_SLOTS;
      }
    }
  }
  return HandshakeStatus.UNKNOWN;
}