Skip to content

Commit

Permalink
Fix memory leak in check_mac_addr routine
Browse files Browse the repository at this point in the history
The IP_ADAPTER_INFO buffer was allocated and never released.
  • Loading branch information
hfiref0x committed Jan 22, 2019
1 parent 5e35a06 commit 3c95815
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions al-khaser/Shared/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ BOOL is_DirectoryExists(TCHAR* szPath)
BOOL check_mac_addr(const TCHAR* szMac)
{
BOOL bResult = FALSE;
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapterInfo, pAdapterInfoPtr;
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);

pAdapterInfo = (PIP_ADAPTER_INFO)MALLOC(sizeof(IP_ADAPTER_INFO));
Expand All @@ -79,38 +79,46 @@ BOOL check_mac_addr(const TCHAR* szMac)
return -1;
}

DWORD dwResult = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);

// Make an initial call to GetAdaptersInfo to get the necessary size into the ulOutBufLen variable
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
if (dwResult == ERROR_BUFFER_OVERFLOW)
{
FREE(pAdapterInfo);
pAdapterInfo = (PIP_ADAPTER_INFO)MALLOC(ulOutBufLen);
if (pAdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
return 1;
}

// Now, we can call GetAdaptersInfo
dwResult = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
}

// Now, we can call GetAdaptersInfo
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_SUCCESS)
if (dwResult == ERROR_SUCCESS)
{
// Convert the given mac address to an array of multibyte chars so we can compare.
CHAR szMacMultiBytes[4];
for (int i = 0; i < 4; i++) {
szMacMultiBytes[i] = (CHAR)szMac[i];
}

while (pAdapterInfo)
pAdapterInfoPtr = pAdapterInfo;

while (pAdapterInfoPtr)
{

if (pAdapterInfo->AddressLength == 6 && !memcmp(szMacMultiBytes, pAdapterInfo->Address, 3))
if (pAdapterInfoPtr->AddressLength == 6 && !memcmp(szMacMultiBytes, pAdapterInfoPtr->Address, 3))
{
bResult = TRUE;
break;
}
pAdapterInfo = pAdapterInfo->Next;
pAdapterInfoPtr = pAdapterInfoPtr->Next;
}
}

FREE(pAdapterInfo);

return bResult;
}

Expand Down

0 comments on commit 3c95815

Please sign in to comment.