src/java.base/unix/native/libnet/Inet6AddressImpl.c
changeset 49985 44e581f54d08
parent 47216 71c04702a3d5
child 51151 d6b131d2bc8b
--- a/src/java.base/unix/native/libnet/Inet6AddressImpl.c	Thu May 03 15:17:27 2018 +0200
+++ b/src/java.base/unix/native/libnet/Inet6AddressImpl.c	Fri May 04 14:37:58 2018 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -42,11 +42,6 @@
 #include "java_net_Inet4AddressImpl.h"
 #include "java_net_Inet6AddressImpl.h"
 
-/* the initial size of our hostent buffers */
-#ifndef NI_MAXHOST
-#define NI_MAXHOST 1025
-#endif
-
 #define SET_NONBLOCKING(fd) {       \
     int flags = fcntl(fd, F_GETFL); \
     flags |= O_NONBLOCK;            \
@@ -67,10 +62,10 @@
     char hostname[NI_MAXHOST + 1];
 
     hostname[0] = '\0';
-    if (gethostname(hostname, NI_MAXHOST) != 0) {
+    if (gethostname(hostname, sizeof(hostname)) != 0) {
         strcpy(hostname, "localhost");
+    } else {
 #if defined(__solaris__)
-    } else {
         // try to resolve hostname via nameservice
         // if it is known but getnameinfo fails, hostname will still be the
         // value from gethostname
@@ -83,17 +78,15 @@
         hints.ai_family = AF_UNSPEC;
 
         if (getaddrinfo(hostname, NULL, &hints, &res) == 0) {
-            getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST,
+            getnameinfo(res->ai_addr, res->ai_addrlen, hostname, sizeof(hostname),
                         NULL, 0, NI_NAMEREQD);
             freeaddrinfo(res);
         }
-    }
 #else
-    } else {
         // make sure string is null-terminated
         hostname[NI_MAXHOST] = '\0';
+#endif
     }
-#endif
     return (*env)->NewStringUTF(env, hostname);
 }
 
@@ -103,7 +96,7 @@
 lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
 {
     jobjectArray result = NULL;
-    char myhostname[NI_MAXHOST+1];
+    char myhostname[NI_MAXHOST + 1];
     struct ifaddrs *ifa = NULL;
     int familyOrder = 0;
     int count = 0, i, j;
@@ -120,7 +113,7 @@
      * the name (if the name actually matches something in DNS etc.
      */
     myhostname[0] = '\0';
-    if (gethostname(myhostname, NI_MAXHOST) == -1) {
+    if (gethostname(myhostname, sizeof(myhostname)) == -1) {
         /* Something went wrong, maybe networking is not setup? */
         return NULL;
     }
@@ -445,7 +438,7 @@
         len = sizeof(struct sockaddr_in6);
     }
 
-    if (getnameinfo(&sa.sa, len, host, NI_MAXHOST, NULL, 0, NI_NAMEREQD)) {
+    if (getnameinfo(&sa.sa, len, host, sizeof(host), NULL, 0, NI_NAMEREQD)) {
         JNU_ThrowByName(env, "java/net/UnknownHostException", NULL);
     } else {
         ret = (*env)->NewStringUTF(env, host);