Fix segfaults in Discord_RegisterW in MinGW builds (#105)

* Add MinGW and WinXP support, remove Win SDK dep when using MinGW

* Remove Win SDK dependency when compiled with MinGW

* Remap the Win SDK-depended functions to string.h substitutes

* Remap missing WinAPI call RegSetKeyValueW to a substitute function

* Remove warnings by pragma when using MinGW

* Fix segfaults in Discord_RegisterW in MinGW builds
This commit is contained in:
Dmitry 2018-02-02 21:40:33 +04:00 committed by Mason Sciotti
parent eee5085e9b
commit c7b4e6b2fc

View file

@ -7,7 +7,7 @@
#include <windows.h> #include <windows.h>
#include <psapi.h> #include <psapi.h>
#include <cwchar> #include <cwchar>
#include <stdio.h> #include <cstdio>
/** /**
* Updated fixes for MinGW and WinXP * Updated fixes for MinGW and WinXP
@ -20,11 +20,16 @@
*/ */
#ifdef __MINGW32__ #ifdef __MINGW32__
/// strsafe.h fixes /// strsafe.h fixes
#define StringCbPrintfW snwprintf HRESULT StringCbPrintfW(LPWSTR pszDest, size_t cbDest, LPCWSTR pszFormat, ...)
LPWSTR StringCbCopyW(LPWSTR a, size_t l, LPCWSTR b)
{ {
a[l-1] = 0; HRESULT ret;
return wcsncpy(a, b, l - 1); // does not set the last byte to 0 on overflow, so it's set to 0 above va_list va;
va_start(va, pszFormat);
cbDest /= 2; // Size is divided by 2 to convert from bytes to wide characters - causes segfault othervise
ret = vsnwprintf(pszDest, cbDest, pszFormat, va);
pszDest[cbDest - 1] = 0; // Terminate the string in case a buffer overflow; -1 will be returned
va_end(va);
return ret;
} }
#else #else
#include <strsafe.h> #include <strsafe.h>
@ -40,15 +45,15 @@ LPWSTR StringCbCopyW(LPWSTR a, size_t l, LPCWSTR b)
#define RegSetKeyValueW regset #define RegSetKeyValueW regset
LSTATUS regset(HKEY hkey, LPCWSTR subkey, LPCWSTR name, DWORD type, const void *data, DWORD len) LSTATUS regset(HKEY hkey, LPCWSTR subkey, LPCWSTR name, DWORD type, const void *data, DWORD len)
{ {
HKEY hsubkey = NULL; HKEY htkey = hkey, hsubkey = nullptr;
LSTATUS ret; LSTATUS ret;
if (subkey && subkey[0]) /* need to create the subkey */ if (subkey && subkey[0])
{ {
if ((ret = RegCreateKeyW( hkey, subkey, &hsubkey )) != ERROR_SUCCESS) return ret; if((ret = RegCreateKeyExW(hkey, subkey, 0, 0, 0, KEY_ALL_ACCESS, 0, &hsubkey, 0)) != ERROR_SUCCESS) return ret;
hkey = hsubkey; htkey = hsubkey;
} }
ret = RegSetValueExW( hkey, name, 0, type, (const BYTE*)data, len ); ret = RegSetValueExW(htkey, name, 0, type, (const BYTE*)data, len);
if (hsubkey) RegCloseKey( hsubkey ); if (hsubkey && hsubkey != hkey) RegCloseKey(hsubkey);
return ret; return ret;
} }
@ -66,7 +71,8 @@ void Discord_RegisterW(const wchar_t* applicationId, const wchar_t* command)
StringCbPrintfW(openCommand, sizeof(openCommand), L"%s", command); StringCbPrintfW(openCommand, sizeof(openCommand), L"%s", command);
} }
else { else {
StringCbCopyW(openCommand, sizeof(openCommand), exeFilePath); //StringCbCopyW(openCommand, sizeof(openCommand), exeFilePath);
StringCbPrintfW(openCommand, sizeof(openCommand), L"%s", exeFilePath);
} }
wchar_t protocolName[64]; wchar_t protocolName[64];