Program: 103645
Project: 103645
Buggy source:
/** 
 * Connects and logins to the server
 * @param authority SecurityAuthority
 * @param reasonCode the authentication reason code. Indicates the reason ofthis authentication.
 * @throws XMPPException if we cannot connect to the server - network problem
 * @throws OperationFailedException if login parametersas server port are not correct
 */
private synchronized void connectAndLogin(SecurityAuthority authority,int reasonCode) throws XMPPException, OperationFailedException {
synchronized (initializationLock) {
    String password=JabberActivator.getProtocolProviderFactory().loadPassword(getAccountID());
    if (password == null) {
      UserCredentials credentials=new UserCredentials();
      credentials.setUserName(getAccountID().getUserID());
      credentials=authority.obtainCredentials(ProtocolNames.JABBER,credentials,reasonCode);
      if (credentials == null) {
        fireRegistrationStateChanged(getRegistrationState(),RegistrationState.UNREGISTERED,RegistrationStateChangeEvent.REASON_USER_REQUEST,"String_Node_Str");
        return;
      }
      char[] pass=credentials.getPassword();
      if (pass == null) {
        fireRegistrationStateChanged(getRegistrationState(),RegistrationState.UNREGISTERED,RegistrationStateChangeEvent.REASON_USER_REQUEST,"String_Node_Str");
        return;
      }
      password=new String(pass);
      if (credentials.isPasswordPersistent()) {
        JabberActivator.getProtocolProviderFactory().storePassword(getAccountID(),password);
      }
    }
    try {
      String userID=StringUtils.parseName(getAccountID().getUserID());
      String serviceName=StringUtils.parseServer(getAccountID().getUserID());
      String serverAddress=getAccountID().getAccountPropertyString(ProtocolProviderFactory.SERVER_ADDRESS);
      String serverPort=getAccountID().getAccountPropertyString(ProtocolProviderFactory.SERVER_PORT);
      String accountResource=getAccountID().getAccountPropertyString(ProtocolProviderFactory.RESOURCE);
      try {
        InetSocketAddress srvAddress=NetworkUtils.getSRVRecord("String_Node_Str","String_Node_Str",serviceName);
        if (srvAddress != null)         serverAddress=srvAddress.getHostName();
      }
 catch (      ParseException ex1) {
        logger.error("String_Node_Str" + ex1.getMessage());
      }
      Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
      org.jivesoftware.smack.proxy.ProxyInfo proxy=null;
      String globalProxyType=JabberActivator.getConfigurationService().getString(ProxyInfo.CONNECTON_PROXY_TYPE_PROPERTY_NAME);
      if (globalProxyType == null || globalProxyType.equals(ProxyInfo.ProxyType.NONE.name())) {
        proxy=org.jivesoftware.smack.proxy.ProxyInfo.forNoProxy();
      }
 else {
        String globalProxyAddress=JabberActivator.getConfigurationService().getString(ProxyInfo.CONNECTON_PROXY_ADDRESS_PROPERTY_NAME);
        String globalProxyPortStr=JabberActivator.getConfigurationService().getString(ProxyInfo.CONNECTON_PROXY_PORT_PROPERTY_NAME);
        int globalProxyPort;
        try {
          globalProxyPort=Integer.parseInt(globalProxyPortStr);
        }
 catch (        NumberFormatException ex) {
          throw new OperationFailedException("String_Node_Str",OperationFailedException.INVALID_ACCOUNT_PROPERTIES,ex);
        }
        String globalProxyUsername=JabberActivator.getConfigurationService().getString(ProxyInfo.CONNECTON_PROXY_USERNAME_PROPERTY_NAME);
        String globalProxyPassword=JabberActivator.getConfigurationService().getString(ProxyInfo.CONNECTON_PROXY_PASSWORD_PROPERTY_NAME);
        if (globalProxyAddress == null || globalProxyAddress.length() <= 0) {
          throw new OperationFailedException("String_Node_Str",OperationFailedException.INVALID_ACCOUNT_PROPERTIES);
        }
        if (globalProxyType.equals(ProxyInfo.ProxyType.HTTP.name())) {
          proxy=org.jivesoftware.smack.proxy.ProxyInfo.forHttpProxy(globalProxyAddress,globalProxyPort,globalProxyUsername,globalProxyPassword);
        }
 else         if (globalProxyType.equals(ProxyInfo.ProxyType.SOCKS4.name())) {
          proxy=org.jivesoftware.smack.proxy.ProxyInfo.forSocks4Proxy(globalProxyAddress,globalProxyPort,globalProxyUsername,globalProxyPassword);
        }
 else         if (globalProxyType.equals(ProxyInfo.ProxyType.SOCKS5.name())) {
          proxy=org.jivesoftware.smack.proxy.ProxyInfo.forSocks5Proxy(globalProxyAddress,globalProxyPort,globalProxyUsername,globalProxyPassword);
        }
      }
      ConnectionConfiguration confConn=new ConnectionConfiguration(serverAddress,Integer.parseInt(serverPort),serviceName,proxy);
      confConn.setReconnectionAllowed(false);
      connection=new XMPPConnection(confConn);
      try {
        CertificateVerificationService gvs=getCertificateVerificationService();
        if (gvs != null)         connection.setCustomTrustManager(new HostTrustManager(gvs.getTrustManager(serverAddress,Integer.parseInt(serverPort))));
      }
 catch (      GeneralSecurityException e) {
        logger.error("String_Node_Str",e);
      }
      connection.connect();
      connection.addConnectionListener(new JabberConnectionListener());
      if (abortConnecting) {
        if (abortConnectingAndReconnect) {
          reregister(SecurityAuthority.CONNECTION_FAILED);
        }
 else {
          abortConnecting=false;
          connection.disconnect();
        }
        return;
      }
      fireRegistrationStateChanged(getRegistrationState(),RegistrationState.REGISTERING,RegistrationStateChangeEvent.REASON_NOT_SPECIFIED,null);
      if (accountResource == null || accountResource.equals("String_Node_Str"))       accountResource="String_Node_Str";
      SASLAuthentication.supportSASLMechanism("String_Node_Str",0);
      SASLAuthentication.unregisterSASLMechanism("String_Node_Str");
      SASLAuthentication.registerSASLMechanism("String_Node_Str",SASLDigestMD5Mechanism.class);
      SASLAuthentication.supportSASLMechanism("String_Node_Str");
      try {
        connection.login(userID,password,accountResource);
      }
 catch (      XMPPException e1) {
        try {
          try {
            connection.disconnect();
          }
 catch (          Exception e) {
          }
          connection.connect();
          connection.addConnectionListener(new JabberConnectionListener());
          if (abortConnecting) {
            if (abortConnectingAndReconnect) {
              reregister(SecurityAuthority.CONNECTION_FAILED);
            }
 else {
              abortConnecting=false;
              connection.disconnect();
            }
            return;
          }
          fireRegistrationStateChanged(getRegistrationState(),RegistrationState.REGISTERING,RegistrationStateChangeEvent.REASON_NOT_SPECIFIED,null);
          connection.login(userID + "String_Node_Str" + serviceName,password,accountResource);
        }
 catch (        XMPPException e2) {
          throw e1;
        }
      }
      if (connection.isAuthenticated()) {
        connection.getRoster().setSubscriptionMode(Roster.SubscriptionMode.manual);
        fireRegistrationStateChanged(getRegistrationState(),RegistrationState.REGISTERED,RegistrationStateChangeEvent.REASON_NOT_SPECIFIED,null);
      }
 else {
        fireRegistrationStateChanged(getRegistrationState(),RegistrationState.UNREGISTERED,RegistrationStateChangeEvent.REASON_NOT_SPECIFIED,null);
      }
    }
 catch (    NumberFormatException ex) {
      throw new OperationFailedException("String_Node_Str",OperationFailedException.INVALID_ACCOUNT_PROPERTIES,ex);
    }
  }
  if (getRegistrationState() == RegistrationState.REGISTERED) {
    discoveryManager=ServiceDiscoveryManager.getInstanceFor(connection);
    ServiceDiscoveryManager.setIdentityName("String_Node_Str");
    ServiceDiscoveryManager.setIdentityType("String_Node_Str");
    Iterator<String> it=supportedFeatures.iterator();
    discoveryManager.removeFeature("String_Node_Str");
    while (it.hasNext()) {
      String feature=it.next();
      if (!discoveryManager.includesFeature(feature))       discoveryManager.addFeature(feature);
    }
  }
}

Fixed version (ground truth):
/** 
 * Connects and logins to the server
 * @param authority SecurityAuthority
 * @param reasonCode the authentication reason code. Indicates the reason ofthis authentication.
 * @throws XMPPException if we cannot connect to the server - network problem
 * @throws OperationFailedException if login parametersas server port are not correct
 */
private synchronized void connectAndLogin(SecurityAuthority authority,int reasonCode) throws XMPPException, OperationFailedException {
synchronized (initializationLock) {
    String password=JabberActivator.getProtocolProviderFactory().loadPassword(getAccountID());
    if (password == null) {
      UserCredentials credentials=new UserCredentials();
      credentials.setUserName(getAccountID().getUserID());
      credentials=authority.obtainCredentials(ProtocolNames.JABBER,credentials,reasonCode);
      if (credentials == null) {
        fireRegistrationStateChanged(getRegistrationState(),RegistrationState.UNREGISTERED,RegistrationStateChangeEvent.REASON_USER_REQUEST,"String_Node_Str");
        return;
      }
      char[] pass=credentials.getPassword();
      if (pass == null) {
        fireRegistrationStateChanged(getRegistrationState(),RegistrationState.UNREGISTERED,RegistrationStateChangeEvent.REASON_USER_REQUEST,"String_Node_Str");
        return;
      }
      password=new String(pass);
      if (credentials.isPasswordPersistent()) {
        JabberActivator.getProtocolProviderFactory().storePassword(getAccountID(),password);
      }
    }
    try {
      String userID=StringUtils.parseName(getAccountID().getUserID());
      String serviceName=StringUtils.parseServer(getAccountID().getUserID());
      String serverAddress=getAccountID().getAccountPropertyString(ProtocolProviderFactory.SERVER_ADDRESS);
      String serverPort=getAccountID().getAccountPropertyString(ProtocolProviderFactory.SERVER_PORT);
      String accountResource=getAccountID().getAccountPropertyString(ProtocolProviderFactory.RESOURCE);
      try {
        InetSocketAddress srvAddress=NetworkUtils.getSRVRecord("String_Node_Str","String_Node_Str",serviceName);
        if (srvAddress != null)         serverAddress=srvAddress.getHostName();
      }
 catch (      ParseException ex1) {
        logger.error("String_Node_Str" + ex1.getMessage());
      }
      Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
      org.jivesoftware.smack.proxy.ProxyInfo proxy=null;
      String globalProxyType=JabberActivator.getConfigurationService().getString(ProxyInfo.CONNECTON_PROXY_TYPE_PROPERTY_NAME);
      if (globalProxyType == null || globalProxyType.equals(ProxyInfo.ProxyType.NONE.name())) {
        proxy=org.jivesoftware.smack.proxy.ProxyInfo.forNoProxy();
      }
 else {
        String globalProxyAddress=JabberActivator.getConfigurationService().getString(ProxyInfo.CONNECTON_PROXY_ADDRESS_PROPERTY_NAME);
        String globalProxyPortStr=JabberActivator.getConfigurationService().getString(ProxyInfo.CONNECTON_PROXY_PORT_PROPERTY_NAME);
        int globalProxyPort;
        try {
          globalProxyPort=Integer.parseInt(globalProxyPortStr);
        }
 catch (        NumberFormatException ex) {
          throw new OperationFailedException("String_Node_Str",OperationFailedException.INVALID_ACCOUNT_PROPERTIES,ex);
        }
        String globalProxyUsername=JabberActivator.getConfigurationService().getString(ProxyInfo.CONNECTON_PROXY_USERNAME_PROPERTY_NAME);
        String globalProxyPassword=JabberActivator.getConfigurationService().getString(ProxyInfo.CONNECTON_PROXY_PASSWORD_PROPERTY_NAME);
        if (globalProxyAddress == null || globalProxyAddress.length() <= 0) {
          throw new OperationFailedException("String_Node_Str",OperationFailedException.INVALID_ACCOUNT_PROPERTIES);
        }
        if (globalProxyType.equals(ProxyInfo.ProxyType.HTTP.name())) {
          proxy=org.jivesoftware.smack.proxy.ProxyInfo.forHttpProxy(globalProxyAddress,globalProxyPort,globalProxyUsername,globalProxyPassword);
        }
 else         if (globalProxyType.equals(ProxyInfo.ProxyType.SOCKS4.name())) {
          proxy=org.jivesoftware.smack.proxy.ProxyInfo.forSocks4Proxy(globalProxyAddress,globalProxyPort,globalProxyUsername,globalProxyPassword);
        }
 else         if (globalProxyType.equals(ProxyInfo.ProxyType.SOCKS5.name())) {
          proxy=org.jivesoftware.smack.proxy.ProxyInfo.forSocks5Proxy(globalProxyAddress,globalProxyPort,globalProxyUsername,globalProxyPassword);
        }
      }
      ConnectionConfiguration confConn=new ConnectionConfiguration(serverAddress,Integer.parseInt(serverPort),serviceName,proxy);
      confConn.setReconnectionAllowed(false);
      connection=new XMPPConnection(confConn);
      try {
        CertificateVerificationService gvs=getCertificateVerificationService();
        if (gvs != null)         connection.setCustomTrustManager(new HostTrustManager(gvs.getTrustManager(serverAddress,Integer.parseInt(serverPort))));
      }
 catch (      GeneralSecurityException e) {
        logger.error("String_Node_Str",e);
      }
      connection.connect();
      connection.addConnectionListener(new JabberConnectionListener());
      if (abortConnecting) {
        abortConnecting=false;
        connection.disconnect();
        return;
      }
      fireRegistrationStateChanged(getRegistrationState(),RegistrationState.REGISTERING,RegistrationStateChangeEvent.REASON_NOT_SPECIFIED,null);
      if (accountResource == null || accountResource.equals("String_Node_Str"))       accountResource="String_Node_Str";
      SASLAuthentication.supportSASLMechanism("String_Node_Str",0);
      SASLAuthentication.unregisterSASLMechanism("String_Node_Str");
      SASLAuthentication.registerSASLMechanism("String_Node_Str",SASLDigestMD5Mechanism.class);
      SASLAuthentication.supportSASLMechanism("String_Node_Str");
      try {
        connection.login(userID,password,accountResource);
      }
 catch (      XMPPException e1) {
        try {
          try {
            connection.disconnect();
          }
 catch (          Exception e) {
          }
          connection.connect();
          connection.addConnectionListener(new JabberConnectionListener());
          if (abortConnecting) {
            abortConnecting=false;
            connection.disconnect();
            return;
          }
          fireRegistrationStateChanged(getRegistrationState(),RegistrationState.REGISTERING,RegistrationStateChangeEvent.REASON_NOT_SPECIFIED,null);
          connection.login(userID + "String_Node_Str" + serviceName,password,accountResource);
        }
 catch (        XMPPException e2) {
          throw e1;
        }
      }
      if (connection.isAuthenticated()) {
        connection.getRoster().setSubscriptionMode(Roster.SubscriptionMode.manual);
        fireRegistrationStateChanged(getRegistrationState(),RegistrationState.REGISTERED,RegistrationStateChangeEvent.REASON_NOT_SPECIFIED,null);
      }
 else {
        fireRegistrationStateChanged(getRegistrationState(),RegistrationState.UNREGISTERED,RegistrationStateChangeEvent.REASON_NOT_SPECIFIED,null);
      }
    }
 catch (    NumberFormatException ex) {
      throw new OperationFailedException("String_Node_Str",OperationFailedException.INVALID_ACCOUNT_PROPERTIES,ex);
    }
  }
  if (getRegistrationState() == RegistrationState.REGISTERED) {
    discoveryManager=ServiceDiscoveryManager.getInstanceFor(connection);
    ServiceDiscoveryManager.setIdentityName("String_Node_Str");
    ServiceDiscoveryManager.setIdentityType("String_Node_Str");
    Iterator<String> it=supportedFeatures.iterator();
    discoveryManager.removeFeature("String_Node_Str");
    while (it.hasNext()) {
      String feature=it.next();
      if (!discoveryManager.includesFeature(feature))       discoveryManager.addFeature(feature);
    }
  }
}