[PATCH 1/3] wintrust: Refactor CryptCATOpen error handling.

Rémi Bernon rbernon at codeweavers.com
Wed Dec 16 07:41:40 CST 2020


Make it simpler, handle all errors in the same place.

Signed-off-by: Rémi Bernon <rbernon at codeweavers.com>
---
 dlls/wintrust/crypt.c | 118 +++++++++++++-----------------------------
 1 file changed, 36 insertions(+), 82 deletions(-)

diff --git a/dlls/wintrust/crypt.c b/dlls/wintrust/crypt.c
index be869ad8277..4fbf75f7e0c 100644
--- a/dlls/wintrust/crypt.c
+++ b/dlls/wintrust/crypt.c
@@ -904,10 +904,10 @@ BOOL WINAPI CryptCATPersistStore(HANDLE catalog)
 HANDLE WINAPI CryptCATOpen(WCHAR *filename, DWORD flags, HCRYPTPROV hProv,
                            DWORD dwPublicVersion, DWORD dwEncodingType)
 {
-    HANDLE file, hmsg;
-    BYTE *buffer = NULL;
-    DWORD size, open_mode = OPEN_ALWAYS;
-    struct cryptcat *cc;
+    HANDLE file, hmsg = NULL;
+    BYTE *buffer = NULL, *p;
+    DWORD i, sum = 0, size, open_mode = OPEN_ALWAYS;
+    struct cryptcat *cc = NULL;
 
     TRACE("filename %s, flags %#x, provider %#lx, version %#x, type %#x\n",
           debugstr_w(filename), flags, hProv, dwPublicVersion, dwEncodingType);
@@ -929,92 +929,46 @@ HANDLE WINAPI CryptCATOpen(WCHAR *filename, DWORD flags, HCRYPTPROV hProv,
     if (file == INVALID_HANDLE_VALUE) return INVALID_HANDLE_VALUE;
 
     size = GetFileSize(file, NULL);
-    if (!(buffer = HeapAlloc(GetProcessHeap(), 0, size)))
-    {
-        CloseHandle(file);
-        SetLastError(ERROR_OUTOFMEMORY);
-        return INVALID_HANDLE_VALUE;
-    }
-    if (!(hmsg = CryptMsgOpenToDecode(dwEncodingType, 0, 0, hProv, NULL, NULL)))
-    {
-        CloseHandle(file);
-        HeapFree(GetProcessHeap(), 0, buffer);
-        return INVALID_HANDLE_VALUE;
-    }
-    if (!ReadFile(file, buffer, size, &size, NULL) || !CryptMsgUpdate(hmsg, buffer, size, TRUE))
-    {
-        CloseHandle(file);
-        HeapFree(GetProcessHeap(), 0, buffer);
-        CryptMsgClose(hmsg);
-        return INVALID_HANDLE_VALUE;
-    }
-    HeapFree(GetProcessHeap(), 0, buffer);
-    CloseHandle(file);
+    if (!(buffer = HeapAlloc(GetProcessHeap(), 0, size))) goto failed_alloc;
+    if (!(hmsg = CryptMsgOpenToDecode(dwEncodingType, 0, 0, hProv, NULL, NULL))) goto failed;
+    if (!ReadFile(file, buffer, size, &size, NULL) || !CryptMsgUpdate(hmsg, buffer, size, TRUE)) goto failed;
 
     size = sizeof(DWORD);
-    if (!(cc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cc))))
-    {
-        CryptMsgClose(hmsg);
-        SetLastError(ERROR_OUTOFMEMORY);
-        return INVALID_HANDLE_VALUE;
-    }
+    if (!(cc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cc)))) goto failed_alloc;
 
     cc->msg = hmsg;
     cc->encoding = dwEncodingType;
-    if (CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_COUNT_PARAM, 0, &cc->attr_count, &size))
-    {
-        DWORD i, sum = 0;
-        BYTE *p;
+    if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_COUNT_PARAM, 0, &cc->attr_count, &size)) goto failed;
 
-        for (i = 0; i < cc->attr_count; i++)
-        {
-            if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, NULL, &size))
-            {
-                CryptMsgClose(hmsg);
-                HeapFree(GetProcessHeap(), 0, cc);
-                return INVALID_HANDLE_VALUE;
-            }
-            sum += size;
-        }
-        if (!(cc->attr = HeapAlloc(GetProcessHeap(), 0, sizeof(*cc->attr) * cc->attr_count + sum)))
-        {
-            CryptMsgClose(hmsg);
-            HeapFree(GetProcessHeap(), 0, cc);
-            SetLastError(ERROR_OUTOFMEMORY);
-            return INVALID_HANDLE_VALUE;
-        }
-        p = (BYTE *)(cc->attr + cc->attr_count);
-        for (i = 0; i < cc->attr_count; i++)
-        {
-            if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, NULL, &size))
-            {
-                CryptMsgClose(hmsg);
-                HeapFree(GetProcessHeap(), 0, cc->attr);
-                HeapFree(GetProcessHeap(), 0, cc);
-                return INVALID_HANDLE_VALUE;
-            }
-            if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, p, &size))
-            {
-                CryptMsgClose(hmsg);
-                HeapFree(GetProcessHeap(), 0, cc->attr);
-                HeapFree(GetProcessHeap(), 0, cc);
-                return INVALID_HANDLE_VALUE;
-            }
-            p += size;
-        }
-        cc->inner = decode_inner_content(hmsg, dwEncodingType, &cc->inner_len);
-        if (!cc->inner || !CryptSIPRetrieveSubjectGuid(filename, NULL, &cc->subject))
-        {
-            CryptMsgClose(hmsg);
-            HeapFree(GetProcessHeap(), 0, cc->attr);
-            HeapFree(GetProcessHeap(), 0, cc->inner);
-            HeapFree(GetProcessHeap(), 0, cc);
-            return INVALID_HANDLE_VALUE;
-        }
-        cc->magic = CRYPTCAT_MAGIC;
-        return cc;
+    for (i = 0; i < cc->attr_count; i++)
+    {
+        if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, NULL, &size)) goto failed;
+        sum += size;
+    }
+    if (!(cc->attr = HeapAlloc(GetProcessHeap(), 0, sizeof(*cc->attr) * cc->attr_count + sum))) goto failed_alloc;
+    p = (BYTE *)(cc->attr + cc->attr_count);
+    for (i = 0; i < cc->attr_count; i++)
+    {
+        if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, NULL, &size)) goto failed;
+        if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, p, &size)) goto failed;
+        p += size;
     }
+    cc->inner = decode_inner_content(hmsg, dwEncodingType, &cc->inner_len);
+    if (!cc->inner || !CryptSIPRetrieveSubjectGuid(filename, NULL, &cc->subject)) goto failed;
+    cc->magic = CRYPTCAT_MAGIC;
+    HeapFree(GetProcessHeap(), 0, buffer);
+    CloseHandle(file);
+    return cc;
+
+failed_alloc:
+    SetLastError(ERROR_OUTOFMEMORY);
+failed:
+    if (cc) HeapFree(GetProcessHeap(), 0, cc->inner);
+    if (cc) HeapFree(GetProcessHeap(), 0, cc->attr);
     HeapFree(GetProcessHeap(), 0, cc);
+    HeapFree(GetProcessHeap(), 0, buffer);
+    CryptMsgClose(hmsg);
+    CloseHandle(file);
     return INVALID_HANDLE_VALUE;
 }
 
-- 
2.29.2




More information about the wine-devel mailing list