[0.4.14][DBGHELP] Accept NULL symbol path as reset-to-default CORE-17073 (#3257)

This is a minimized version of the not yet merged patch of (#3257).
I will commit only into the RC but not into master, because
author Thomas Faber is most likely interested in upstream integration.

It fixes Heap Corruption when starting KernRate, which I could confirm
with system-wide DPH beforehand.
This commit is contained in:
Joachim Henze 2021-07-10 12:24:23 +02:00
parent 3ee039917c
commit d3e301e28b

View file

@ -174,6 +174,37 @@ struct cpu* cpu_find(DWORD machine)
return NULL;
}
static WCHAR *make_default_search_path(void)
{
WCHAR* search_path;
unsigned size;
unsigned len;
static const WCHAR sym_path[] = {'_','N','T','_','S','Y','M','B','O','L','_','P','A','T','H',0};
static const WCHAR alt_sym_path[] = {'_','N','T','_','A','L','T','E','R','N','A','T','E','_','S','Y','M','B','O','L','_','P','A','T','H',0};
size = 1;
search_path = HeapAlloc(GetProcessHeap(), 0, (size + 1) * sizeof(WCHAR));
search_path[0] = '.';
search_path[1] = 0;
len = GetEnvironmentVariableW(sym_path, NULL, 0);
if (len)
{
search_path = HeapReAlloc(GetProcessHeap(), 0, search_path, (size + 1 + len + 1) * sizeof(WCHAR));
search_path[size] = ';';
GetEnvironmentVariableW(sym_path, search_path + size + 1, len);
size += 1 + len;
}
len = GetEnvironmentVariableW(alt_sym_path, NULL, 0);
if (len)
{
search_path = HeapReAlloc(GetProcessHeap(), 0, search_path, (size + 1 + len + 1) * sizeof(WCHAR));
search_path[size] = ';';
GetEnvironmentVariableW(alt_sym_path, search_path + size + 1, len);
}
return search_path;
}
/******************************************************************
* SymSetSearchPathW (DBGHELP.@)
*
@ -202,12 +233,18 @@ BOOL WINAPI SymSetSearchPath(HANDLE hProcess, PCSTR searchPath)
unsigned len;
WCHAR* sp;
len = MultiByteToWideChar(CP_ACP, 0, searchPath, -1, NULL, 0);
if ((sp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
if (searchPath)
{
len = MultiByteToWideChar(CP_ACP, 0, searchPath, -1, NULL, 0);
sp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
if (!sp) return FALSE;
MultiByteToWideChar(CP_ACP, 0, searchPath, -1, sp, len);
}
ret = SymSetSearchPathW(hProcess, sp);
ret = SymSetSearchPathW(hProcess, sp);
if (searchPath)
{
HeapFree(GetProcessHeap(), 0, sp);
}
return ret;
@ -334,31 +371,7 @@ BOOL WINAPI SymInitializeW(HANDLE hProcess, PCWSTR UserSearchPath, BOOL fInvadeP
}
else
{
unsigned size;
unsigned len;
static const WCHAR sym_path[] = {'_','N','T','_','S','Y','M','B','O','L','_','P','A','T','H',0};
static const WCHAR alt_sym_path[] = {'_','N','T','_','A','L','T','E','R','N','A','T','E','_','S','Y','M','B','O','L','_','P','A','T','H',0};
pcs->search_path = HeapAlloc(GetProcessHeap(), 0, (len = MAX_PATH) * sizeof(WCHAR));
while ((size = GetCurrentDirectoryW(len, pcs->search_path)) >= len)
pcs->search_path = HeapReAlloc(GetProcessHeap(), 0, pcs->search_path, (len *= 2) * sizeof(WCHAR));
pcs->search_path = HeapReAlloc(GetProcessHeap(), 0, pcs->search_path, (size + 1) * sizeof(WCHAR));
len = GetEnvironmentVariableW(sym_path, NULL, 0);
if (len)
{
pcs->search_path = HeapReAlloc(GetProcessHeap(), 0, pcs->search_path, (size + 1 + len + 1) * sizeof(WCHAR));
pcs->search_path[size] = ';';
GetEnvironmentVariableW(sym_path, pcs->search_path + size + 1, len);
size += 1 + len;
}
len = GetEnvironmentVariableW(alt_sym_path, NULL, 0);
if (len)
{
pcs->search_path = HeapReAlloc(GetProcessHeap(), 0, pcs->search_path, (size + 1 + len + 1) * sizeof(WCHAR));
pcs->search_path[size] = ';';
GetEnvironmentVariableW(alt_sym_path, pcs->search_path + size + 1, len);
}
pcs->search_path = make_default_search_path();
}
pcs->lmodules = NULL;