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

View File

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

View File

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