diff --git a/src/windows/native/java/net/NetworkInterface_winXP.c b/src/windows/native/java/net/NetworkInterface_winXP.c index 7e3e79d1f4927bdc06b037a9c06cd1c0886afdec..4042d16d4eff741b3d5976ffad6a672fa1964fbe 100644 --- a/src/windows/native/java/net/NetworkInterface_winXP.c +++ b/src/windows/native/java/net/NetworkInterface_winXP.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, 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 @@ -28,6 +28,7 @@ #include /* needed for htonl */ #include #include +#include #include "java_net_NetworkInterface.h" #include "jni_util.h" @@ -70,7 +71,7 @@ void printnifs (netif *netifPP, char *str) { #endif -static int bufsize = 1024; +static int bufsize = 4096; /* * return an array of IP_ADAPTER_ADDRESSES containing one element @@ -94,7 +95,12 @@ static int getAdapters (JNIEnv *env, IP_ADAPTER_ADDRESSES **adapters) { ret = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapterInfo, &len); if (ret == ERROR_BUFFER_OVERFLOW) { - IP_ADAPTER_ADDRESSES * newAdapterInfo = (IP_ADAPTER_ADDRESSES *) realloc (adapterInfo, len); + IP_ADAPTER_ADDRESSES * newAdapterInfo = NULL; + if (len < (ULONG_MAX - bufsize)) { + len = len + bufsize; + } + newAdapterInfo = + (IP_ADAPTER_ADDRESSES *) realloc (adapterInfo, len); if (newAdapterInfo == NULL) { free(adapterInfo); JNU_ThrowByName(env, "java/lang/OutOfMemoryError", "Native heap allocation failure"); @@ -103,7 +109,6 @@ static int getAdapters (JNIEnv *env, IP_ADAPTER_ADDRESSES **adapters) { adapterInfo = newAdapterInfo; - bufsize = len; ret = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapterInfo, &len); } @@ -137,7 +142,12 @@ IP_ADAPTER_ADDRESSES *getAdapter (JNIEnv *env, jint index) { flags |= GAA_FLAG_INCLUDE_PREFIX; val = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapterInfo, &len); if (val == ERROR_BUFFER_OVERFLOW) { - IP_ADAPTER_ADDRESSES * newAdapterInfo = (IP_ADAPTER_ADDRESSES *) realloc (adapterInfo, len); + IP_ADAPTER_ADDRESSES * newAdapterInfo = NULL; + if (len < (ULONG_MAX - bufsize)) { + len = len + bufsize; + } + newAdapterInfo = + (IP_ADAPTER_ADDRESSES *) realloc (adapterInfo, len); if (newAdapterInfo == NULL) { free(adapterInfo); JNU_ThrowByName(env, "java/lang/OutOfMemoryError", "Native heap allocation failure"); @@ -146,7 +156,6 @@ IP_ADAPTER_ADDRESSES *getAdapter (JNIEnv *env, jint index) { adapterInfo = newAdapterInfo; - bufsize = len; val = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapterInfo, &len); }