From 1f87c307f6c7bcda951a2d0d9fadc50fbc558391 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Thu, 15 Oct 2020 12:38:05 +0200 Subject: 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 --- api/namespace.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'api/namespace.c') 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 -- cgit v1.2.3-59-g8ed1b