[PATCH 2/7] regedit: Use a helper function to allocate memory and check for a valid pointer

Hugh McMaster hugh.mcmaster at outlook.com
Wed Jul 26 08:16:14 CDT 2017


Signed-off-by: Hugh McMaster <hugh.mcmaster at outlook.com>
---
 programs/regedit/regproc.c | 70 +++++++++++++++++++++++++---------------------
 1 file changed, 38 insertions(+), 32 deletions(-)

diff --git a/programs/regedit/regproc.c b/programs/regedit/regproc.c
index e0393ccbd2..dd254bd923 100644
--- a/programs/regedit/regproc.c
+++ b/programs/regedit/regproc.c
@@ -28,8 +28,11 @@
 #include <io.h>
 #include <windows.h>
 #include <wine/unicode.h>
+#include <wine/debug.h>
 #include "regproc.h"
 
+WINE_DEFAULT_DEBUG_CHANNEL(regedit);
+
 #define REG_VAL_BUF_SIZE        4096
 
 extern const WCHAR* reg_class_namesW[];
@@ -41,6 +44,19 @@ static HKEY reg_class_keys[] = {
 
 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*A))
 
+static void *heap_alloc(size_t size)
+{
+    void *buf;
+
+    if (!(buf = HeapAlloc(GetProcessHeap(), 0, size)))
+    {
+        ERR("Out of memory!\n");
+        exit(1);
+    }
+
+    return buf;
+}
+
 /******************************************************************************
  * Allocates memory and converts input from multibyte to wide chars
  * Returned string must be freed by the caller
@@ -52,8 +68,7 @@ static WCHAR* GetWideString(const char* strA)
         WCHAR* strW;
         int len = MultiByteToWideChar(CP_ACP, 0, strA, -1, NULL, 0);
 
-        strW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
-        CHECK_ENOUGH_MEMORY(strW);
+        strW = heap_alloc(len * sizeof(WCHAR));
         MultiByteToWideChar(CP_ACP, 0, strA, -1, strW, len);
         return strW;
     }
@@ -71,8 +86,7 @@ static WCHAR* GetWideStringN(const char* strA, int chars, DWORD *len)
         WCHAR* strW;
         *len = MultiByteToWideChar(CP_ACP, 0, strA, chars, NULL, 0);
 
-        strW = HeapAlloc(GetProcessHeap(), 0, *len * sizeof(WCHAR));
-        CHECK_ENOUGH_MEMORY(strW);
+        strW = heap_alloc(*len * sizeof(WCHAR));
         MultiByteToWideChar(CP_ACP, 0, strA, chars, strW, *len);
         return strW;
     }
@@ -91,8 +105,7 @@ char* GetMultiByteString(const WCHAR* strW)
         char* strA;
         int len = WideCharToMultiByte(CP_ACP, 0, strW, -1, NULL, 0, NULL, NULL);
 
-        strA = HeapAlloc(GetProcessHeap(), 0, len);
-        CHECK_ENOUGH_MEMORY(strA);
+        strA = heap_alloc(len);
         WideCharToMultiByte(CP_ACP, 0, strW, -1, strA, len, NULL, NULL);
         return strA;
     }
@@ -110,8 +123,7 @@ static char* GetMultiByteStringN(const WCHAR* strW, int chars, DWORD* len)
         char* strA;
         *len = WideCharToMultiByte(CP_ACP, 0, strW, chars, NULL, 0, NULL, NULL);
 
-        strA = HeapAlloc(GetProcessHeap(), 0, *len);
-        CHECK_ENOUGH_MEMORY(strA);
+        strA = heap_alloc(*len);
         WideCharToMultiByte(CP_ACP, 0, strW, chars, strA, *len, NULL, NULL);
         return strA;
     }
@@ -476,8 +488,7 @@ static LONG open_key(struct parser *parser, WCHAR *path)
 
     if (res == ERROR_SUCCESS)
     {
-        parser->key_name = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(path) + 1) * sizeof(WCHAR));
-        CHECK_ENOUGH_MEMORY(parser->key_name);
+        parser->key_name = heap_alloc((lstrlenW(path) + 1) * sizeof(WCHAR));
         lstrcpyW(parser->key_name, path);
     }
     else
@@ -564,8 +575,7 @@ static WCHAR *header_state(struct parser *parser, WCHAR *pos)
 
     if (!parser->is_unicode)
     {
-        header = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(line) + 3) * sizeof(WCHAR));
-        CHECK_ENOUGH_MEMORY(header);
+        header = heap_alloc((lstrlenW(line) + 3) * sizeof(WCHAR));
         header[0] = parser->two_wchars[0];
         header[1] = parser->two_wchars[1];
         lstrcpyW(header + 2, line);
@@ -722,8 +732,7 @@ static WCHAR *quoted_value_name_state(struct parser *parser, WCHAR *pos)
         goto invalid;
 
     /* copy the value name in case we need to parse multiple lines and the buffer is overwritten */
-    parser->value_name = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(val_name) + 1) * sizeof(WCHAR));
-    CHECK_ENOUGH_MEMORY(parser->value_name);
+    parser->value_name = heap_alloc((lstrlenW(val_name) + 1) * sizeof(WCHAR));
     lstrcpyW(parser->value_name, val_name);
 
     set_state(parser, DATA_START);
@@ -834,8 +843,7 @@ static WCHAR *dword_data_state(struct parser *parser, WCHAR *pos)
 {
     WCHAR *line = pos;
 
-    parser->data = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
-    CHECK_ENOUGH_MEMORY(parser->data);
+    parser->data = heap_alloc(sizeof(DWORD));
 
     if (!convert_hex_to_dword(line, parser->data))
         goto invalid;
@@ -958,8 +966,7 @@ static WCHAR *get_lineA(FILE *fp)
     if (!size)
     {
         size = REG_VAL_BUF_SIZE;
-        buf = HeapAlloc(GetProcessHeap(), 0, size);
-        CHECK_ENOUGH_MEMORY(buf);
+        buf = heap_alloc(size);
         *buf = 0;
         next = buf;
     }
@@ -1016,8 +1023,7 @@ static WCHAR *get_lineW(FILE *fp)
     if (!size)
     {
         size = REG_VAL_BUF_SIZE;
-        buf = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
-        CHECK_ENOUGH_MEMORY(buf);
+        buf = heap_alloc(size * sizeof(WCHAR));
         *buf = 0;
         next = buf;
     }
@@ -1156,7 +1162,7 @@ static WCHAR *REGPROC_escape_string(WCHAR *str, size_t str_len, size_t *line_len
             escape_count++;
     }
 
-    buf = resize_buffer(NULL, (str_len + escape_count + 1) * sizeof(WCHAR));
+    buf = heap_alloc((str_len + escape_count + 1) * sizeof(WCHAR));
 
     for (i = 0, pos = 0; i < str_len; i++, pos++)
     {
@@ -1203,7 +1209,7 @@ static size_t export_value_name(FILE *fp, WCHAR *name, size_t len, BOOL unicode)
     if (name && *name)
     {
         WCHAR *str = REGPROC_escape_string(name, len, &line_len);
-        WCHAR *buf = resize_buffer(NULL, (line_len + 4) * sizeof(WCHAR));
+        WCHAR *buf = heap_alloc((line_len + 4) * sizeof(WCHAR));
         line_len = sprintfW(buf, quoted_fmt, str);
         REGPROC_write_line(fp, buf, unicode);
         HeapFree(GetProcessHeap(), 0, buf);
@@ -1226,7 +1232,7 @@ static void export_string_data(WCHAR **buf, WCHAR *data, size_t size)
 
     len = size / sizeof(WCHAR) - 1;
     str = REGPROC_escape_string(data, len, &line_len);
-    *buf = resize_buffer(NULL, (line_len + 3) * sizeof(WCHAR));
+    *buf = heap_alloc((line_len + 3) * sizeof(WCHAR));
     sprintfW(*buf, fmt, str);
     HeapFree(GetProcessHeap(), 0, str);
 }
@@ -1235,7 +1241,7 @@ static void export_dword_data(WCHAR **buf, DWORD *data)
 {
     static const WCHAR fmt[] = {'d','w','o','r','d',':','%','0','8','x',0};
 
-    *buf = resize_buffer(NULL, 15 * sizeof(WCHAR));
+    *buf = heap_alloc(15 * sizeof(WCHAR));
     sprintfW(*buf, fmt, *data);
 }
 
@@ -1252,7 +1258,7 @@ static size_t export_hex_data_type(FILE *fp, DWORD type, BOOL unicode)
     }
     else
     {
-        WCHAR *buf = resize_buffer(NULL, 15 * sizeof(WCHAR));
+        WCHAR *buf = heap_alloc(15 * sizeof(WCHAR));
         line_len = sprintfW(buf, hexp_fmt, type);
         REGPROC_write_line(fp, buf, unicode);
         HeapFree(GetProcessHeap(), 0, buf);
@@ -1276,7 +1282,7 @@ static void export_hex_data(FILE *fp, WCHAR **buf, DWORD type, DWORD line_len,
         data = GetMultiByteStringN(data, size / sizeof(WCHAR), &size);
 
     num_commas = size - 1;
-    *buf = resize_buffer(NULL, (size * 3) * sizeof(WCHAR));
+    *buf = heap_alloc(size * 3 * sizeof(WCHAR));
 
     for (i = 0, pos = 0; i < size; i++)
     {
@@ -1336,7 +1342,7 @@ static WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name,
     WCHAR *subkey_path;
     static const WCHAR fmt[] = {'%','s','\\','%','s',0};
 
-    subkey_path = resize_buffer(NULL, (path_len + subkey_len + 2) * sizeof(WCHAR));
+    subkey_path = heap_alloc((path_len + subkey_len + 2) * sizeof(WCHAR));
     sprintfW(subkey_path, fmt, path, subkey_name);
 
     return subkey_path;
@@ -1347,7 +1353,7 @@ static void export_key_name(FILE *fp, WCHAR *name, BOOL unicode)
     static const WCHAR fmt[] = {'\r','\n','[','%','s',']','\r','\n',0};
     WCHAR *buf;
 
-    buf = resize_buffer(NULL, (lstrlenW(name) + 7) * sizeof(WCHAR));
+    buf = heap_alloc((lstrlenW(name) + 7) * sizeof(WCHAR));
     sprintfW(buf, fmt, name);
     REGPROC_write_line(fp, buf, unicode);
     HeapFree(GetProcessHeap(), 0, buf);
@@ -1368,8 +1374,8 @@ static int export_registry_data(FILE *fp, HKEY key, WCHAR *path, BOOL unicode)
 
     export_key_name(fp, path, unicode);
 
-    value_name = resize_buffer(NULL, max_value_len * sizeof(WCHAR));
-    data = resize_buffer(NULL, max_data_bytes);
+    value_name = heap_alloc(max_value_len * sizeof(WCHAR));
+    data = heap_alloc(max_data_bytes);
 
     i = 0;
     for (;;)
@@ -1401,7 +1407,7 @@ static int export_registry_data(FILE *fp, HKEY key, WCHAR *path, BOOL unicode)
     HeapFree(GetProcessHeap(), 0, data);
     HeapFree(GetProcessHeap(), 0, value_name);
 
-    subkey_name = resize_buffer(NULL, MAX_SUBKEY_LEN * sizeof(WCHAR));
+    subkey_name = heap_alloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
 
     path_len = lstrlenW(path);
 
@@ -1521,7 +1527,7 @@ static BOOL export_all(WCHAR *file_name, WCHAR *path, BOOL unicode)
             return FALSE;
         }
 
-        class_name = resize_buffer(NULL, (lstrlenW(reg_class_namesW[i]) + 1) * sizeof(WCHAR));
+        class_name = heap_alloc((lstrlenW(reg_class_namesW[i]) + 1) * sizeof(WCHAR));
         lstrcpyW(class_name, reg_class_namesW[i]);
 
         export_registry_data(fp, classes[i], class_name, unicode);
-- 
2.13.2




More information about the wine-patches mailing list