Fix OOB exception when setHostAddress(InetAddress) is used.

Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
  at java.lang.String.charAt(String.java:658)
  at org.jivesoftware.smack.util.dns.HostAddress.<init>(HostAddress.java:48)
  at org.jivesoftware.smack.util.dns.HostAddress.<init>(HostAddress.java:62)
This commit is contained in:
Florian Schmaus 2017-01-04 15:35:47 +01:00
parent 157ff138a4
commit e9bbe9a475
3 changed files with 12 additions and 9 deletions

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright © 2013-2016 Florian Schmaus * Copyright © 2013-2017 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,7 +25,7 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.jivesoftware.smack.SmackException.ConnectionException; import org.jivesoftware.smack.SmackException.ConnectionException;
import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.StringUtils;
public class HostAddress { public class HostAddress {
private final String fqdn; private final String fqdn;
@ -34,18 +34,17 @@ public class HostAddress {
private final List<InetAddress> inetAddresses; private final List<InetAddress> inetAddresses;
/** /**
* Creates a new HostAddress with the given FQDN. The port will be set to the default XMPP client port: 5222 * Creates a new HostAddress with the given FQDN.
* *
* @param fqdn Fully qualified domain name. * @param fqdn the optional fully qualified domain name (FQDN).
* @param port The port to connect on. * @param port The port to connect on.
* @throws IllegalArgumentException If the fqdn is null or port is out of valid range (0 - 65535). * @throws IllegalArgumentException If the port is out of valid range (0 - 65535).
*/ */
public HostAddress(String fqdn, int port, List<InetAddress> inetAddresses) { public HostAddress(String fqdn, int port, List<InetAddress> inetAddresses) {
Objects.requireNonNull(fqdn, "FQDN is null");
if (port < 0 || port > 65535) if (port < 0 || port > 65535)
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Port must be a 16-bit unsiged integer (i.e. between 0-65535. Port was: " + port); "Port must be a 16-bit unsiged integer (i.e. between 0-65535. Port was: " + port);
if (fqdn.charAt(fqdn.length() - 1) == '.') { if (StringUtils.isNotEmpty(fqdn) && fqdn.charAt(fqdn.length() - 1) == '.') {
this.fqdn = fqdn.substring(0, fqdn.length() - 1); this.fqdn = fqdn.substring(0, fqdn.length() - 1);
} }
else { else {
@ -59,7 +58,7 @@ public class HostAddress {
} }
public HostAddress(int port, InetAddress hostAddress) { public HostAddress(int port, InetAddress hostAddress) {
this("", port, Collections.singletonList(hostAddress)); this(null, port, Collections.singletonList(hostAddress));
} }
/** /**

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2013-2016 Florian Schmaus * Copyright 2013-2017 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,6 +19,8 @@ package org.jivesoftware.smack.util.dns;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.util.StringUtils;
/** /**
* A DNS SRV RR. * A DNS SRV RR.
* *
@ -43,6 +45,7 @@ public class SRVRecord extends HostAddress implements Comparable<SRVRecord> {
*/ */
public SRVRecord(String fqdn, int port, int priority, int weight, List<InetAddress> inetAddresses) { public SRVRecord(String fqdn, int port, int priority, int weight, List<InetAddress> inetAddresses) {
super(fqdn, port, inetAddresses); super(fqdn, port, inetAddresses);
StringUtils.requireNotNullOrEmpty(fqdn, "The FQDN must not be null");
if (weight < 0 || weight > 65535) if (weight < 0 || weight > 65535)
throw new IllegalArgumentException( throw new IllegalArgumentException(
"DNS SRV records weight must be a 16-bit unsiged integer (i.e. between 0-65535. Weight was: " "DNS SRV records weight must be a 16-bit unsiged integer (i.e. between 0-65535. Weight was: "

View File

@ -593,6 +593,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
} }
failedAddresses.add(hostAddress); failedAddresses.add(hostAddress);
} else { } else {
StringUtils.requireNotNullOrEmpty(host, "Host of HostAddress " + hostAddress + " must not be null when using a Proxy");
final String hostAndPort = host + " at port " + port; final String hostAndPort = host + " at port " + port;
LOGGER.finer("Trying to establish TCP connection via Proxy to " + hostAndPort); LOGGER.finer("Trying to establish TCP connection via Proxy to " + hostAndPort);
try { try {