8275319: java.net.NetworkInterface throws java.lang.Error instead of SocketException

Reviewed-by: alanb, dfuchs
This commit is contained in:
Daniel Jelinski 2021-10-20 14:06:08 +00:00 committed by Daniel Fuchs
parent a91a0a523a
commit 043cde22d4
2 changed files with 74 additions and 51 deletions

View File

@ -112,7 +112,7 @@ MIB_IFROW *getIF(jint index) {
return NULL;
count = GetIfTable(tableP, &size, TRUE);
if (count == ERROR_INSUFFICIENT_BUFFER || count == ERROR_BUFFER_OVERFLOW) {
if (count == ERROR_INSUFFICIENT_BUFFER) {
MIB_IFTABLE* newTableP = (MIB_IFTABLE *)realloc(tableP, size);
if (newTableP == NULL) {
free(tableP);
@ -187,7 +187,7 @@ int enumInterfaces(JNIEnv *env, netif **netifPP)
}
ret = GetIfTable(tableP, &size, TRUE);
if (ret == ERROR_INSUFFICIENT_BUFFER || ret == ERROR_BUFFER_OVERFLOW) {
if (ret == ERROR_INSUFFICIENT_BUFFER) {
MIB_IFTABLE * newTableP = (MIB_IFTABLE *)realloc(tableP, size);
if (newTableP == NULL) {
free(tableP);
@ -200,9 +200,19 @@ int enumInterfaces(JNIEnv *env, netif **netifPP)
if (ret != NO_ERROR) {
free(tableP);
JNU_ThrowByName(env, "java/lang/Error",
"IP Helper Library GetIfTable function failed");
switch (ret) {
case ERROR_INVALID_PARAMETER:
JNU_ThrowInternalError(env,
"IP Helper Library GetIfTable function failed: "
"invalid parameter");
break;
default:
SetLastError(ret);
JNU_ThrowByNameWithMessageAndLastError(env,
JNU_JAVANETPKG "SocketException",
"IP Helper Library GetIfTable function failed");
break;
}
// this different error code is to handle the case when we call
// GetIpAddrTable in pure IPv6 environment
return -2;
@ -308,8 +318,8 @@ int enumInterfaces(JNIEnv *env, netif **netifPP)
// it should not fail, because we have called it once before
if (MultiByteToWideChar(CP_OEMCP, 0, ifrowP->bDescr,
ifrowP->dwDescrLen, curr->displayName, wlen) == 0) {
JNU_ThrowByName(env, "java/lang/Error",
"Cannot get multibyte char for interface display name");
JNU_ThrowInternalError(env,
"Cannot get multibyte char for interface display name");
free_netif(netifP);
free(tableP);
free(curr->name);
@ -374,7 +384,7 @@ int lookupIPAddrTable(JNIEnv *env, MIB_IPADDRTABLE **tablePP)
}
ret = GetIpAddrTable(tableP, &size, FALSE);
if (ret == ERROR_INSUFFICIENT_BUFFER || ret == ERROR_BUFFER_OVERFLOW) {
if (ret == ERROR_INSUFFICIENT_BUFFER) {
MIB_IPADDRTABLE * newTableP = (MIB_IPADDRTABLE *)realloc(tableP, size);
if (newTableP == NULL) {
free(tableP);
@ -389,8 +399,19 @@ int lookupIPAddrTable(JNIEnv *env, MIB_IPADDRTABLE **tablePP)
if (tableP != NULL) {
free(tableP);
}
JNU_ThrowByName(env, "java/lang/Error",
"IP Helper Library GetIpAddrTable function failed");
switch (ret) {
case ERROR_INVALID_PARAMETER:
JNU_ThrowInternalError(env,
"IP Helper Library GetIpAddrTable function failed: "
"invalid parameter");
break;
default:
SetLastError(ret);
JNU_ThrowByNameWithMessageAndLastError(env,
JNU_JAVANETPKG "SocketException",
"IP Helper Library GetIpAddrTable function failed");
break;
}
// this different error code is to handle the case when we call
// GetIpAddrTable in pure IPv6 environment
return -2;

View File

@ -110,27 +110,29 @@ int getAdapters (JNIEnv *env, int flags, IP_ADAPTER_ADDRESSES **adapters) {
if (ret != ERROR_SUCCESS) {
free (adapterInfo);
if (ret == ERROR_INSUFFICIENT_BUFFER) {
JNU_ThrowByName(env, "java/lang/Error",
"IP Helper Library GetAdaptersAddresses function failed "
"with ERROR_INSUFFICIENT_BUFFER");
} else if (ret == ERROR_ADDRESS_NOT_ASSOCIATED ) {
JNU_ThrowByName(env, "java/lang/Error",
"IP Helper Library GetAdaptersAddresses function failed "
"with ERROR_ADDRESS_NOT_ASSOCIATED");
} else {
char error_msg_buf[100];
int _sr;
_sr = _snprintf_s(error_msg_buf, sizeof(error_msg_buf),
_TRUNCATE, "IP Helper Library GetAdaptersAddresses "
"function failed with error == %d", ret);
if (_sr != -1) {
JNU_ThrowByName(env, "java/lang/Error", error_msg_buf);
} else {
JNU_ThrowByName(env, "java/lang/Error",
"IP Helper Library GetAdaptersAddresses function failure");
}
switch (ret) {
case ERROR_INVALID_PARAMETER:
JNU_ThrowInternalError(env,
"IP Helper Library GetAdaptersAddresses function failed: "
"invalid parameter");
break;
case ERROR_NOT_ENOUGH_MEMORY:
JNU_ThrowOutOfMemoryError(env,
"IP Helper Library GetAdaptersAddresses function failed: "
"not enough memory");
break;
case ERROR_NO_DATA:
// not an error
*adapters = NULL;
return ERROR_SUCCESS;
default:
SetLastError(ret);
JNU_ThrowByNameWithMessageAndLastError(env,
JNU_JAVANETPKG "SocketException",
"IP Helper Library GetAdaptersAddresses function failed");
break;
}
return -1;
}
*adapters = adapterInfo;
@ -179,26 +181,26 @@ IP_ADAPTER_ADDRESSES *getAdapter (JNIEnv *env, jint index) {
if (val != ERROR_SUCCESS) {
free (adapterInfo);
if (val == ERROR_INSUFFICIENT_BUFFER) {
JNU_ThrowByName(env, "java/lang/Error",
"IP Helper Library GetAdaptersAddresses function failed "
"with ERROR_INSUFFICIENT_BUFFER");
} else if (val == ERROR_ADDRESS_NOT_ASSOCIATED ) {
JNU_ThrowByName(env, "java/lang/Error",
"IP Helper Library GetAdaptersAddresses function failed "
"with ERROR_ADDRESS_NOT_ASSOCIATED");
} else {
char error_msg_buf[100];
int _sr;
_sr = _snprintf_s(error_msg_buf, sizeof(error_msg_buf),
_TRUNCATE, "IP Helper Library GetAdaptersAddresses function failed "
"with error == %d", val);
if (_sr != -1) {
JNU_ThrowByName(env, "java/lang/Error", error_msg_buf);
} else {
JNU_ThrowByName(env, "java/lang/Error",
"IP Helper Library GetAdaptersAddresses function failure");
}
switch (val) {
case ERROR_INVALID_PARAMETER:
JNU_ThrowInternalError(env,
"IP Helper Library GetAdaptersAddresses function failed: "
"invalid parameter");
break;
case ERROR_NOT_ENOUGH_MEMORY:
JNU_ThrowOutOfMemoryError(env,
"IP Helper Library GetAdaptersAddresses function failed: "
"not enough memory");
break;
case ERROR_NO_DATA:
// not an error
break;
default:
SetLastError(val);
JNU_ThrowByNameWithMessageAndLastError(env,
JNU_JAVANETPKG "SocketException",
"IP Helper Library GetAdaptersAddresses function failed");
break;
}
return NULL;
}
@ -237,7 +239,7 @@ static int ipinflen = 2048;
*/
int getAllInterfacesAndAddresses (JNIEnv *env, netif **netifPP)
{
DWORD ret, flags;
int ret, flags;
MIB_IPADDRTABLE *tableP;
IP_ADAPTER_ADDRESSES *ptr, *adapters=NULL;
ULONG len=ipinflen, count=0;