api: log out-of-memory errors too

It's not likely the write to log will succeed in low memory condition
thou.

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2020-10-15 12:38:05 +02:00 committed by Jason A. Donenfeld
parent 2a77f20277
commit 1f87c307f6
3 changed files with 24 additions and 36 deletions

View File

@ -36,28 +36,19 @@ AdapterGetDrvInfoDetail(
{
HANDLE Heap = GetProcessHeap();
DWORD Size = sizeof(SP_DRVINFO_DETAIL_DATA_W) + 0x100;
DWORD Result;
for (;;)
{
*DrvInfoDetailData = HeapAlloc(Heap, 0, Size);
if (!*DrvInfoDetailData)
{
Result = ERROR_OUTOFMEMORY;
goto out;
}
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
(*DrvInfoDetailData)->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA_W);
if (SetupDiGetDriverInfoDetailW(DevInfo, DevInfoData, DrvInfoData, *DrvInfoDetailData, Size, &Size))
return ERROR_SUCCESS;
Result = GetLastError();
DWORD Result = GetLastError();
HeapFree(Heap, 0, *DrvInfoDetailData);
if (Result != ERROR_INSUFFICIENT_BUFFER)
{
LOG_ERROR(L"Failed", Result);
goto out;
}
return LOG_ERROR(L"Failed", Result);
}
out:
return Result;
}
/**
@ -95,7 +86,7 @@ GetDeviceRegistryProperty(
{
*Buf = HeapAlloc(Heap, 0, *BufLen);
if (!*Buf)
return ERROR_OUTOFMEMORY;
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
if (SetupDiGetDeviceRegistryPropertyW(DevInfo, DevInfoData, Property, ValueType, *Buf, *BufLen, BufLen))
return ERROR_SUCCESS;
DWORD Result = GetLastError();
@ -233,7 +224,7 @@ GetDeviceObject(_In_opt_z_ const WCHAR *InstanceId, _Out_ HANDLE *Handle)
}
WCHAR *Interfaces = HeapAlloc(Heap, 0, InterfacesLen * sizeof(WCHAR));
if (!Interfaces)
return ERROR_OUTOFMEMORY;
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
Result = CM_Get_Device_Interface_ListW(
(GUID *)&GUID_DEVINTERFACE_NET,
(DEVINSTID_W)InstanceId,
@ -283,7 +274,7 @@ ForceCloseWintunAdapterHandle(_In_ HDEVINFO DevInfo, _In_ SP_DEVINFO_DATA *DevIn
HANDLE Heap = GetProcessHeap();
WCHAR *InstanceId = HeapAlloc(Heap, HEAP_ZERO_MEMORY, sizeof(*InstanceId) * RequiredBytes);
if (!InstanceId)
return ERROR_OUTOFMEMORY;
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
if (!SetupDiGetDeviceInstanceIdW(DevInfo, DevInfoData, InstanceId, RequiredBytes, &RequiredBytes))
{
Result = LOG_LAST_ERROR(L"Failed to get device instance ID");
@ -327,7 +318,7 @@ AdapterDisableAllOurs(_In_ HDEVINFO DevInfo, _Inout_ SP_DEVINFO_DATA_LIST **Disa
{
SP_DEVINFO_DATA_LIST *DeviceNode = HeapAlloc(Heap, 0, sizeof(SP_DEVINFO_DATA_LIST));
if (!DeviceNode)
return ERROR_OUTOFMEMORY;
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
DeviceNode->Data.cbSize = sizeof(SP_DEVINFO_DATA);
if (!SetupDiEnumDeviceInfo(DevInfo, EnumIndex, &DeviceNode->Data))
{
@ -681,6 +672,7 @@ CreateAdapterData(
*Adapter = HeapAlloc(Heap, 0, sizeof(WINTUN_ADAPTER));
if (!*Adapter)
{
LOG(WINTUN_LOG_ERR, L"Out of memory");
Result = ERROR_OUTOFMEMORY;
goto cleanupKey;
}

View File

@ -13,26 +13,22 @@ static BCRYPT_ALG_HANDLE AlgProvider;
static WCHAR *
NormalizeStringAlloc(_In_ NORM_FORM NormForm, _In_z_ const WCHAR *Source)
{
WCHAR *Result = NULL;
HANDLE Heap = GetProcessHeap();
int Len = NormalizeString(NormForm, Source, -1, NULL, 0);
for (int i = 0; i < 10; ++i)
for (;;)
{
if (Result)
HeapFree(Heap, 0, Result);
Result = HeapAlloc(Heap, 0, sizeof(WCHAR) * Len);
if (!Result)
return NULL;
Len = NormalizeString(NormForm, Source, -1, Result, Len);
WCHAR *Str = HeapAlloc(Heap, 0, sizeof(WCHAR) * Len);
if (!Str)
return LOG(WINTUN_LOG_ERR, L"Out of memory"), NULL;
Len = NormalizeString(NormForm, Source, -1, Str, Len);
if (Len > 0)
return Result;
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
break;
return Str;
DWORD Result = GetLastError();
HeapFree(Heap, 0, Str);
if (Result != ERROR_INSUFFICIENT_BUFFER)
return LOG_ERROR(L"Failed", Result), NULL;
Len = -Len;
}
if (Result)
HeapFree(Heap, 0, Result);
return NULL;
}
static void

View File

@ -110,7 +110,7 @@ RegistryGetString(_Inout_ WCHAR **Buf, _In_ DWORD Len, _In_ DWORD ValueType)
/* String is missing zero-terminator. */
WCHAR *BufZ = HeapAlloc(Heap, 0, ((size_t)Len + 1) * sizeof(WCHAR));
if (!BufZ)
return ERROR_OUTOFMEMORY;
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
wmemcpy(BufZ, *Buf, Len);
BufZ[Len] = 0;
HeapFree(Heap, 0, *Buf);
@ -130,7 +130,7 @@ RegistryGetString(_Inout_ WCHAR **Buf, _In_ DWORD Len, _In_ DWORD ValueType)
{
WCHAR *Expanded = HeapAlloc(Heap, 0, Len * sizeof(WCHAR));
if (!Expanded)
return ERROR_OUTOFMEMORY;
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
DWORD Result = ExpandEnvironmentStringsW(*Buf, Expanded, Len);
if (!Result)
{
@ -178,7 +178,7 @@ RegistryGetMultiString(_Inout_ WCHAR **Buf, _In_ DWORD Len, _In_ DWORD ValueType
/* Missing string and list terminators. */
WCHAR *BufZ = HeapAlloc(Heap, 0, ((size_t)Len + 2) * sizeof(WCHAR));
if (!BufZ)
return ERROR_OUTOFMEMORY;
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
wmemcpy(BufZ, *Buf, Len);
BufZ[Len] = 0;
BufZ[Len + 1] = 0;
@ -191,7 +191,7 @@ RegistryGetMultiString(_Inout_ WCHAR **Buf, _In_ DWORD Len, _In_ DWORD ValueType
/* Missing list terminator. */
WCHAR *BufZ = HeapAlloc(Heap, 0, ((size_t)Len + 1) * sizeof(WCHAR));
if (!BufZ)
return ERROR_OUTOFMEMORY;
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
wmemcpy(BufZ, *Buf, Len);
BufZ[Len] = 0;
HeapFree(Heap, 0, *Buf);
@ -210,7 +210,7 @@ RegistryGetMultiString(_Inout_ WCHAR **Buf, _In_ DWORD Len, _In_ DWORD ValueType
Len = (DWORD)wcslen(*Buf) + 1;
WCHAR *BufZ = HeapAlloc(Heap, 0, ((size_t)Len + 1) * sizeof(WCHAR));
if (!BufZ)
return ERROR_OUTOFMEMORY;
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
wmemcpy(BufZ, *Buf, Len);
BufZ[Len] = 0;
HeapFree(Heap, 0, *Buf);
@ -249,7 +249,7 @@ RegistryQuery(
{
*Buf = HeapAlloc(Heap, 0, *BufLen);
if (!*Buf)
return ERROR_OUTOFMEMORY;
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
LSTATUS Result = RegQueryValueExW(Key, Name, NULL, ValueType, (BYTE *)*Buf, BufLen);
if (Result == ERROR_SUCCESS)
return ERROR_SUCCESS;