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