diff --git a/api/adapter.c b/api/adapter.c index 288bda1..c7ac205 100644 --- a/api/adapter.c +++ b/api/adapter.c @@ -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; } diff --git a/api/namespace.c b/api/namespace.c index 944b24b..3d7d563 100644 --- a/api/namespace.c +++ b/api/namespace.c @@ -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 diff --git a/api/registry.c b/api/registry.c index 30a152c..e752469 100644 --- a/api/registry.c +++ b/api/registry.c @@ -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;