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

View File

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