]> nmode's Git Repositories - signal-cli/commitdiff
Improve error handling for tcp address parsing
authorAsamK <asamk@gmx.de>
Thu, 16 Dec 2021 20:10:15 +0000 (21:10 +0100)
committerAsamK <asamk@gmx.de>
Thu, 16 Dec 2021 20:10:15 +0000 (21:10 +0100)
src/main/java/org/asamk/signal/util/IOUtils.java

index b4c4c6dd883f1ea24cc81df9ed99283e62078971..b772887b0af48687c39c49ea6115ef3d1d840c55 100644 (file)
@@ -98,16 +98,22 @@ public class IOUtils {
     public static InetSocketAddress parseInetSocketAddress(final String tcpAddress) throws UserErrorException {
         final var colonIndex = tcpAddress.lastIndexOf(':');
         if (colonIndex < 0) {
     public static InetSocketAddress parseInetSocketAddress(final String tcpAddress) throws UserErrorException {
         final var colonIndex = tcpAddress.lastIndexOf(':');
         if (colonIndex < 0) {
-            throw new UserErrorException("Invalid tcp bind address: " + tcpAddress);
+            throw new UserErrorException("Invalid tcp bind address (expected host:port): " + tcpAddress);
         }
         }
-        final String host = tcpAddress.substring(0, colonIndex);
+        final var host = tcpAddress.substring(0, colonIndex);
+        final var portString = tcpAddress.substring(colonIndex + 1);
+
         final int port;
         try {
         final int port;
         try {
-            port = Integer.parseInt(tcpAddress.substring(colonIndex + 1));
+            port = Integer.parseInt(portString);
         } catch (NumberFormatException e) {
         } catch (NumberFormatException e) {
-            throw new UserErrorException("Invalid tcp bind address: " + tcpAddress, e);
+            throw new UserErrorException("Invalid tcp port: " + portString, e);
+        }
+        final var socketAddress = new InetSocketAddress(host, port);
+        if (socketAddress.isUnresolved()) {
+            throw new UserErrorException("Invalid tcp bind address, invalid host: " + host);
         }
         }
-        return new InetSocketAddress(host, port);
+        return socketAddress;
     }
 
     public static UnixDomainPrincipal getUnixDomainPrincipal(final SocketChannel channel) throws IOException {
     }
 
     public static UnixDomainPrincipal getUnixDomainPrincipal(final SocketChannel channel) throws IOException {