mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8202181: Correctly specify size of hostname buffer in Unix Inet*AddressImpl_getLocalHostName implementations
Reviewed-by: stuefe, bpb, vtewari, chegar
This commit is contained in:
parent
97571a7c4e
commit
2ac4c89501
3 changed files with 19 additions and 28 deletions
|
@ -41,11 +41,6 @@
|
|||
extern jobjectArray lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6);
|
||||
#endif
|
||||
|
||||
/* 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; \
|
||||
|
@ -66,10 +61,10 @@ Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
|
|||
char hostname[NI_MAXHOST + 1];
|
||||
|
||||
hostname[0] = '\0';
|
||||
if (gethostname(hostname, NI_MAXHOST) != 0) {
|
||||
if (gethostname(hostname, sizeof(hostname)) != 0) {
|
||||
strcpy(hostname, "localhost");
|
||||
#if defined(__solaris__)
|
||||
} else {
|
||||
#if defined(__solaris__)
|
||||
// try to resolve hostname via nameservice
|
||||
// if it is known but getnameinfo fails, hostname will still be the
|
||||
// value from gethostname
|
||||
|
@ -82,17 +77,15 @@ Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
|
|||
hints.ai_family = AF_INET;
|
||||
|
||||
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
|
||||
}
|
||||
return (*env)->NewStringUTF(env, hostname);
|
||||
}
|
||||
|
||||
|
@ -248,7 +241,7 @@ Java_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
|
|||
sa.sin_family = AF_INET;
|
||||
|
||||
if (getnameinfo((struct sockaddr *)&sa, sizeof(struct sockaddr_in),
|
||||
host, NI_MAXHOST, NULL, 0, NI_NAMEREQD)) {
|
||||
host, sizeof(host), NULL, 0, NI_NAMEREQD)) {
|
||||
JNU_ThrowByName(env, "java/net/UnknownHostException", NULL);
|
||||
} else {
|
||||
ret = (*env)->NewStringUTF(env, host);
|
||||
|
|
|
@ -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 @@ Java_java_net_Inet6AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
|
|||
char hostname[NI_MAXHOST + 1];
|
||||
|
||||
hostname[0] = '\0';
|
||||
if (gethostname(hostname, NI_MAXHOST) != 0) {
|
||||
if (gethostname(hostname, sizeof(hostname)) != 0) {
|
||||
strcpy(hostname, "localhost");
|
||||
#if defined(__solaris__)
|
||||
} else {
|
||||
#if defined(__solaris__)
|
||||
// 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 @@ Java_java_net_Inet6AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
|
|||
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
|
||||
}
|
||||
return (*env)->NewStringUTF(env, hostname);
|
||||
}
|
||||
|
||||
|
@ -103,7 +96,7 @@ __private_extern__ jobjectArray
|
|||
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 @@ lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
|
|||
* 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 @@ Java_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
|
|||
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);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1997, 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
|
||||
|
@ -38,6 +38,11 @@
|
|||
#define NET_NSEC_PER_SEC 1000000000
|
||||
#define NET_NSEC_PER_USEC 1000
|
||||
|
||||
/* in case NI_MAXHOST is not defined in netdb.h */
|
||||
#ifndef NI_MAXHOST
|
||||
#define NI_MAXHOST 1025
|
||||
#endif
|
||||
|
||||
/* Defines SO_REUSEPORT */
|
||||
#ifndef SO_REUSEPORT
|
||||
#ifdef __linux__
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue