iphlpapi: Don't allocate gobs of memory if interface table is empty.

This commit is contained in:
Juan Lang 2007-11-15 11:05:10 -08:00 committed by Alexandre Julliard
parent 7e584b4340
commit 9ec6e33e32
2 changed files with 16 additions and 9 deletions

View File

@ -205,11 +205,13 @@ InterfaceIndexTable *getInterfaceIndexTable(void)
if (indexes) { if (indexes) {
struct if_nameindex *p; struct if_nameindex *p;
DWORD size = sizeof(InterfaceIndexTable);
for (p = indexes, numInterfaces = 0; p && p->if_name; p++) for (p = indexes, numInterfaces = 0; p && p->if_name; p++)
numInterfaces++; numInterfaces++;
ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, if (numInterfaces > 1)
sizeof(InterfaceIndexTable) + (numInterfaces - 1) * sizeof(DWORD)); size += (numInterfaces - 1) * sizeof(DWORD);
ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
if (ret) { if (ret) {
for (p = indexes; p && p->if_name; p++) for (p = indexes; p && p->if_name; p++)
ret->indexes[ret->numIndexes++] = p->if_index; ret->indexes[ret->numIndexes++] = p->if_index;

View File

@ -1015,8 +1015,10 @@ DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
ret = ERROR_INVALID_PARAMETER; ret = ERROR_INVALID_PARAMETER;
else { else {
DWORD numInterfaces = getNumInterfaces(); DWORD numInterfaces = getNumInterfaces();
ULONG size = sizeof(MIB_IFTABLE) + (numInterfaces - 1) * sizeof(MIB_IFROW); ULONG size = sizeof(MIB_IFTABLE);
if (numInterfaces > 1)
size += (numInterfaces - 1) * sizeof(MIB_IFROW);
if (!pIfTable || *pdwSize < size) { if (!pIfTable || *pdwSize < size) {
*pdwSize = size; *pdwSize = size;
ret = ERROR_INSUFFICIENT_BUFFER; ret = ERROR_INSUFFICIENT_BUFFER;
@ -1025,8 +1027,9 @@ DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
InterfaceIndexTable *table = getInterfaceIndexTable(); InterfaceIndexTable *table = getInterfaceIndexTable();
if (table) { if (table) {
size = sizeof(MIB_IFTABLE) + (table->numIndexes - 1) * size = sizeof(MIB_IFTABLE);
sizeof(MIB_IFROW); if (table->numIndexes > 1)
size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
if (*pdwSize < size) { if (*pdwSize < size) {
*pdwSize = size; *pdwSize = size;
ret = ERROR_INSUFFICIENT_BUFFER; ret = ERROR_INSUFFICIENT_BUFFER;
@ -1082,9 +1085,10 @@ DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
ret = ERROR_INVALID_PARAMETER; ret = ERROR_INVALID_PARAMETER;
else { else {
DWORD numInterfaces = getNumInterfaces(); DWORD numInterfaces = getNumInterfaces();
ULONG size = sizeof(IP_INTERFACE_INFO) + (numInterfaces - 1) * ULONG size = sizeof(IP_INTERFACE_INFO);
sizeof(IP_ADAPTER_INDEX_MAP);
if (numInterfaces > 1)
size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
if (!pIfTable || *dwOutBufLen < size) { if (!pIfTable || *dwOutBufLen < size) {
*dwOutBufLen = size; *dwOutBufLen = size;
ret = ERROR_INSUFFICIENT_BUFFER; ret = ERROR_INSUFFICIENT_BUFFER;
@ -1093,8 +1097,9 @@ DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
InterfaceIndexTable *table = getInterfaceIndexTable(); InterfaceIndexTable *table = getInterfaceIndexTable();
if (table) { if (table) {
size = sizeof(IP_INTERFACE_INFO) + (table->numIndexes - 1) * size = sizeof(IP_INTERFACE_INFO);
sizeof(IP_ADAPTER_INDEX_MAP); if (table->numIndexes > 1)
size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
if (*dwOutBufLen < size) { if (*dwOutBufLen < size) {
*dwOutBufLen = size; *dwOutBufLen = size;
ret = ERROR_INSUFFICIENT_BUFFER; ret = ERROR_INSUFFICIENT_BUFFER;