reg: Use a helper function to allocate memory and die on failure.

Signed-off-by: Hugh McMaster <hugh.mcmaster@outlook.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Hugh McMaster 2017-08-01 12:30:51 +00:00 committed by Alexandre Julliard
parent ec1b32624b
commit a6e28cc3b4
1 changed files with 33 additions and 53 deletions

View File

@ -17,9 +17,10 @@
*/
#include <windows.h>
#include <errno.h>
#include <stdlib.h>
#include <wine/unicode.h>
#include <wine/debug.h>
#include <errno.h>
#include "reg.h"
#define ARRAY_SIZE(A) (sizeof(A)/sizeof(*A))
@ -78,6 +79,17 @@ type_rels[] =
{REG_MULTI_SZ, type_multi_sz},
};
static void *heap_xalloc(size_t size)
{
void *buf = HeapAlloc(GetProcessHeap(), 0, size);
if (!buf)
{
ERR("Out of memory!\n");
exit(1);
}
return buf;
}
static void output_writeconsole(const WCHAR *str, DWORD wlen)
{
DWORD count, ret;
@ -93,8 +105,7 @@ static void output_writeconsole(const WCHAR *str, DWORD wlen)
* one in that case.
*/
len = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, NULL, 0, NULL, NULL);
msgA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
if (!msgA) return;
msgA = heap_xalloc(len);
WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, msgA, len, NULL, NULL);
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
@ -240,7 +251,7 @@ static LPBYTE get_regdata(const WCHAR *data, DWORD reg_type, WCHAR separator, DW
case REG_EXPAND_SZ:
{
*reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
out_data = heap_xalloc(*reg_count);
lstrcpyW((LPWSTR)out_data,data);
break;
}
@ -256,7 +267,7 @@ static LPBYTE get_regdata(const WCHAR *data, DWORD reg_type, WCHAR separator, DW
break;
}
*reg_count = sizeof(DWORD);
out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
out_data = heap_xalloc(*reg_count);
((LPDWORD)out_data)[0] = val;
break;
}
@ -265,7 +276,7 @@ static LPBYTE get_regdata(const WCHAR *data, DWORD reg_type, WCHAR separator, DW
BYTE hex0, hex1;
int i = 0, destByteIndex = 0, datalen = lstrlenW(data);
*reg_count = ((datalen + datalen % 2) / 2) * sizeof(BYTE);
out_data = HeapAlloc(GetProcessHeap(), 0, *reg_count);
out_data = heap_xalloc(*reg_count);
if(datalen % 2)
{
hex1 = hexchar_to_byte(data[i++]);
@ -292,7 +303,7 @@ static LPBYTE get_regdata(const WCHAR *data, DWORD reg_type, WCHAR separator, DW
case REG_MULTI_SZ:
{
int i, destindex, len = strlenW(data);
WCHAR *buffer = HeapAlloc(GetProcessHeap(), 0, (len + 2) * sizeof(WCHAR));
WCHAR *buffer = heap_xalloc((len + 2) * sizeof(WCHAR));
for (i = 0, destindex = 0; i < len; i++, destindex++)
{
@ -462,7 +473,7 @@ static int reg_delete(HKEY root, WCHAR *path, WCHAR *key_name, WCHAR *value_name
return 1;
}
maxValue++;
szValue = HeapAlloc(GetProcessHeap(),0,maxValue*sizeof(WCHAR));
szValue = heap_xalloc(maxValue * sizeof(WCHAR));
while (1)
{
@ -507,7 +518,7 @@ static WCHAR *reg_data_to_wchar(DWORD type, const BYTE *src, DWORD size_bytes)
{
case REG_SZ:
case REG_EXPAND_SZ:
buffer = HeapAlloc(GetProcessHeap(), 0, size_bytes);
buffer = heap_xalloc(size_bytes);
strcpyW(buffer, (WCHAR *)src);
break;
case REG_NONE:
@ -516,7 +527,7 @@ static WCHAR *reg_data_to_wchar(DWORD type, const BYTE *src, DWORD size_bytes)
WCHAR *ptr;
WCHAR fmt[] = {'%','0','2','X',0};
buffer = HeapAlloc(GetProcessHeap(), 0, (size_bytes * 2 + 1) * sizeof(WCHAR));
buffer = heap_xalloc((size_bytes * 2 + 1) * sizeof(WCHAR));
ptr = buffer;
for (i = 0; i < size_bytes; i++)
ptr += sprintfW(ptr, fmt, src[i]);
@ -529,7 +540,7 @@ static WCHAR *reg_data_to_wchar(DWORD type, const BYTE *src, DWORD size_bytes)
const int zero_x_dword = 10;
WCHAR fmt[] = {'0','x','%','x',0};
buffer = HeapAlloc(GetProcessHeap(), 0, (zero_x_dword + 1) * sizeof(WCHAR));
buffer = heap_xalloc((zero_x_dword + 1) * sizeof(WCHAR));
sprintfW(buffer, fmt, *(DWORD *)src);
break;
}
@ -542,13 +553,13 @@ static WCHAR *reg_data_to_wchar(DWORD type, const BYTE *src, DWORD size_bytes)
if (size_bytes <= two_wchars)
{
buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR));
buffer = heap_xalloc(sizeof(WCHAR));
*buffer = 0;
return buffer;
}
tmp_size = size_bytes - two_wchars; /* exclude both null terminators */
buffer = HeapAlloc(GetProcessHeap(), 0, tmp_size * 2 + sizeof(WCHAR));
buffer = heap_xalloc(tmp_size * 2 + sizeof(WCHAR));
len = tmp_size / sizeof(WCHAR);
for (i = 0, destindex = 0; i < len; i++, destindex++)
@ -615,13 +626,9 @@ static WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name,
WCHAR *subkey_path;
WCHAR fmt[] = {'%','s','\\','%','s',0};
subkey_path = HeapAlloc(GetProcessHeap(), 0, (path_len + subkey_len + 2) * sizeof(WCHAR));
if (!subkey_path)
{
ERR("Failed to allocate memory for subkey_path\n");
return NULL;
}
subkey_path = heap_xalloc((path_len + subkey_len + 2) * sizeof(WCHAR));
sprintfW(subkey_path, fmt, path, subkey_name);
return subkey_path;
}
@ -641,12 +648,7 @@ static int query_value(HKEY key, WCHAR *value_name, WCHAR *path, BOOL recurse)
WCHAR *subkey_name, *subkey_path;
HKEY subkey;
data = HeapAlloc(GetProcessHeap(), 0, max_data_bytes);
if (!data)
{
ERR("Failed to allocate memory for data\n");
return 1;
}
data = heap_xalloc(max_data_bytes);
for (;;)
{
@ -685,12 +687,7 @@ static int query_value(HKEY key, WCHAR *value_name, WCHAR *path, BOOL recurse)
return 0;
}
subkey_name = HeapAlloc(GetProcessHeap(), 0, MAX_SUBKEY_LEN * sizeof(WCHAR));
if (!subkey_name)
{
ERR("Failed to allocate memory for subkey_name\n");
return 1;
}
subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
path_len = strlenW(path);
@ -733,20 +730,8 @@ static int query_all(HKEY key, WCHAR *path, BOOL recurse)
output_string(fmt, path);
value_name = HeapAlloc(GetProcessHeap(), 0, max_value_len * sizeof(WCHAR));
if (!value_name)
{
ERR("Failed to allocate memory for value_name\n");
return 1;
}
data = HeapAlloc(GetProcessHeap(), 0, max_data_bytes);
if (!data)
{
HeapFree(GetProcessHeap(), 0, value_name);
ERR("Failed to allocate memory for data\n");
return 1;
}
value_name = heap_xalloc(max_value_len * sizeof(WCHAR));
data = heap_xalloc(max_data_bytes);
i = 0;
for (;;)
@ -781,12 +766,7 @@ static int query_all(HKEY key, WCHAR *path, BOOL recurse)
if (i || recurse)
output_string(newlineW);
subkey_name = HeapAlloc(GetProcessHeap(), 0, MAX_SUBKEY_LEN * sizeof(WCHAR));
if (!subkey_name)
{
ERR("Failed to allocate memory for subkey_name\n");
return 1;
}
subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
path_len = strlenW(path);
@ -866,13 +846,13 @@ static WCHAR *get_long_key(HKEY root, WCHAR *path)
if (!path)
{
long_key = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
long_key = heap_xalloc((len + 1) * sizeof(WCHAR));
strcpyW(long_key, root_rels[i].long_name);
return long_key;
}
len += strlenW(path) + 1; /* add one for the backslash */
long_key = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
long_key = heap_xalloc((len + 1) * sizeof(WCHAR));
sprintfW(long_key, fmt, root_rels[i].long_name, path);
return long_key;
}