Memory issues fixing and hardening

This commit is contained in:
METANEOCORTEX\Kotti 2022-02-09 02:02:36 +01:00
parent b6ef650d99
commit e993abcdd4
24 changed files with 261 additions and 128 deletions

View File

@ -151,7 +151,7 @@
<Optimization>Disabled</Optimization>
<PrecompiledHeader>Use</PrecompiledHeader>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<WarningLevel>Level4</WarningLevel>
<LanguageStandard>stdcpplatest</LanguageStandard>
<TreatWarningAsError>true</TreatWarningAsError>
@ -186,7 +186,7 @@
<Optimization>Disabled</Optimization>
<PrecompiledHeader>Use</PrecompiledHeader>
<PreprocessorDefinitions>WIN64;WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<WarningLevel>Level4</WarningLevel>
<LanguageStandard>stdcpplatest</LanguageStandard>
<TreatWarningAsError>true</TreatWarningAsError>
@ -221,7 +221,7 @@
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<PrecompiledHeader>Use</PrecompiledHeader>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<WarningLevel>Level4</WarningLevel>
<LanguageStandard>stdcpplatest</LanguageStandard>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
@ -303,7 +303,7 @@
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<PrecompiledHeader>Use</PrecompiledHeader>
<PreprocessorDefinitions>WIN64;WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<WarningLevel>Level4</WarningLevel>
<LanguageStandard>stdcpplatest</LanguageStandard>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>

View File

@ -16,6 +16,7 @@
#define _WIN32_WINNT 0x601
#include <windows.h>
#include <commctrl.h>
#include <process.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <strsafe.h>
@ -147,19 +148,16 @@ BOOL DirList_Destroy(HWND hwnd)
//
BOOL DirList_StartIconThread(HWND hwnd)
{
DWORD dwtid;
LPDLDATA lpdl = (LPVOID)GetProp(hwnd,pDirListProp);
DirList_TerminateIconThread(hwnd);
ResetEvent(lpdl->hExitThread);
//ResetEvent(lpdl->hTerminatedThread);
ResetEvent(lpdl->hTerminatedThread);
CreateThread(NULL,0,DirList_IconThread,(LPVOID)lpdl,0,&dwtid);
_beginthread(DirList_IconThread, 0, (void*)lpdl);
return TRUE;
}
@ -189,7 +187,6 @@ BOOL DirList_TerminateIconThread(HWND hwnd)
SetEvent(lpdl->hTerminatedThread);
return TRUE;
}
@ -418,8 +415,8 @@ DWORD WINAPI DirList_IconThread(LPVOID lpParam)
// Exit immediately if DirList_Fill() hasn't been called
if (!lpdl->lpsf) {
SetEvent(lpdl->hTerminatedThread);
ExitThread(0);
//return(0);
_endthread();
return(0);
}
hwnd = lpdl->hwnd;
@ -499,9 +496,8 @@ DWORD WINAPI DirList_IconThread(LPVOID lpParam)
CoUninitialize();
SetEvent(lpdl->hTerminatedThread);
ExitThread(0);
//return(0);
_endthread();
return(0);
}

View File

@ -22,6 +22,14 @@
#define NTDDI_VERSION 0x06010000 /*NTDDI_WIN7*/
#endif
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
#define DBG_NEW new (_NORMAL_BLOCK, __FILE__, __LINE__)
// Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the
// allocations to be of _CLIENT_BLOCK type
#else
#define DBG_NEW new
#endif
#define VC_EXTRALEAN 1
#define WIN32_LEAN_AND_MEAN 1
//#define NOMINMAX 1
@ -1530,8 +1538,8 @@ void LoadSettings()
}
Settings.PrintZoom = clampi(iPrintZoom, SC_MIN_ZOOM_LEVEL, SC_MAX_ZOOM_LEVEL);
WCHAR localeInfo[3];
GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_IMEASURE, localeInfo, 3);
WCHAR localeInfo[SMALL_BUFFER];
GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_IMEASURE, localeInfo, COUNTOF(localeInfo));
LONG const _margin = (localeInfo[0] == L'0') ? 2000L : 1000L; // Metric system. L'1' is US System
Defaults.PrintMargin.left = _margin;
Settings.PrintMargin.left = clampi(IniSectionGetInt(IniSecSettings, L"PrintMarginLeft", Defaults.PrintMargin.left), 0, 40000);
@ -2660,7 +2668,6 @@ void MRU_Save(LPMRULIST pmru)
}
}
}
CloseSettingsFile(true, bOpenedByMe);
}
}
@ -2698,7 +2705,6 @@ bool MRU_MergeSave(LPMRULIST pmru, bool bAddFiles, bool bRelativePath, bool bUne
}
MRU_Save(pmruBase);
MRU_Destroy(pmruBase);
pmruBase = NULL;
CloseSettingsFile(bOpenedByMe, bOpenedByMe);
@ -2709,6 +2715,7 @@ bool MRU_MergeSave(LPMRULIST pmru, bool bAddFiles, bool bRelativePath, bool bUne
return false;
}
// ////////////////////////////////////////////////////////////////////////////
// Some C++ Extentions for Notepad3
// ////////////////////////////////////////////////////////////////////////////

View File

@ -255,8 +255,8 @@
# include <iostream>
#endif // SI_SUPPORT_IOSTREAMS
#ifdef _DEBUG
# ifndef assert
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
#ifndef assert
# include <cassert>
# endif
# define SI_ASSERT(x) assert(x)

View File

@ -14,6 +14,14 @@
* *
*******************************************************************************/
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
#define DBG_NEW new (_NORMAL_BLOCK, __FILE__, __LINE__)
// Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the
// allocations to be of _CLIENT_BLOCK type
#else
#define DBG_NEW new
#endif
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>

View File

@ -17,6 +17,7 @@
#include <windowsx.h>
#include <commctrl.h>
#include <process.h>
#include <shlobj.h>
#include <shellapi.h>
#include <shlwapi.h>
@ -2137,15 +2138,7 @@ bool AddToFavDlg(HWND hwnd, const HPATHL hTargetPth)
// FileMRUDlgProc()
//
//
typedef struct tagIconThreadInfo {
HWND hwnd; // HWND of ListView Control
HANDLE hThread; // Thread Handle
HANDLE hExitThread; // Flag is set when Icon Thread should terminate
HANDLE hTerminatedThread; // Flag is set when Icon Thread has terminated
} ICONTHREADINFO, *LPICONTHREADINFO;
DWORD WINAPI FileMRUIconThread(LPVOID lpParam)
unsigned int WINAPI FileMRUIconThread(LPVOID lpParam)
{
BackgroundWorker *worker = (BackgroundWorker *)lpParam;
@ -2218,6 +2211,7 @@ DWORD WINAPI FileMRUIconThread(LPVOID lpParam)
}
CoUninitialize();
BackgroundWorker_End(0);
return 0;
}
@ -2489,7 +2483,7 @@ CASE_WM_CTLCOLOR_SET:
DialogEnableControl(hwnd, IDOK, (cnt > 0));
DialogEnableControl(hwnd, IDC_REMOVE, (cnt > 0));
worker->workerThread = CreateThread(NULL, 0, FileMRUIconThread, (LPVOID)worker, 0, NULL);
BackgroundWorker_Start(worker, FileMRUIconThread, worker);
}
break;

View File

@ -45,8 +45,8 @@ typedef struct tagDLDATA { // dl
//==== Property Name ==========================================================
static const WCHAR *pDirListProp = L"DirListData";
static const WCHAR *pDirListProp = L"DirListData";
//=============================================================================
@ -137,7 +137,7 @@ void DirList_StartIconThread(HWND hwnd)
LPDLDATA lpdl = (LPDLDATA)GetProp(hwnd, pDirListProp);
BackgroundWorker_Cancel(&lpdl->worker);
lpdl->worker.workerThread = CreateThread(NULL, 0, DirList_IconThread, (LPVOID)lpdl, 0, NULL);
BackgroundWorker_Start(&lpdl->worker, DirList_IconThread, lpdl);
}
@ -333,7 +333,7 @@ int DirList_Fill(HWND hwnd,LPCWSTR lpszDir,DWORD grfFlags,LPCWSTR lpszFileSpec,
//
// Thread to extract file icons in the background
//
DWORD WINAPI DirList_IconThread(LPVOID lpParam)
unsigned int WINAPI DirList_IconThread(LPVOID lpParam)
{
LPDLDATA lpdl = (LPDLDATA)lpParam;
BackgroundWorker *worker = &lpdl->worker;
@ -432,6 +432,7 @@ DWORD WINAPI DirList_IconThread(LPVOID lpParam)
}
CoUninitialize();
BackgroundWorker_End(0);
return 0;
}

View File

@ -61,7 +61,7 @@ int DirList_Fill(HWND hwnd,LPCWSTR lpszDir,DWORD grfFlags,LPCWSTR lpszFileSpec,b
//==== DlIconThread() =========================================================
DWORD WINAPI DirList_IconThread(LPVOID lpParam);
unsigned int WINAPI DirList_IconThread(LPVOID lpParam);
//==== DlGetDispInfo() ========================================================

View File

@ -2223,10 +2223,13 @@ void EditChar2Hex(HWND hwnd)
size_t const alloc = (count + 1) * (2 + MAX_ESCAPE_HEX_DIGIT) + 1;
char* const ch = (char*)AllocMem(alloc, HEAP_ZERO_MEMORY);
WCHAR* const wch = (WCHAR*)AllocMem(alloc * sizeof(WCHAR), HEAP_ZERO_MEMORY);
if (!ch || !wch) {
return;
}
SciCall_GetSelText(ch);
int const nchars = (DocPos)MultiByteToWideChar(Encoding_SciCP, 0, ch, -1, wch, (int)alloc) - 1; // '\0'
memset(ch, 0, alloc);
SecureZeroMemory(ch, alloc);
for (int i = 0, j = 0; i < nchars; ++i) {
if (wch[i] <= 0xFF) {
@ -5838,7 +5841,7 @@ static DocPos _FindInTarget(LPCWSTR wchFind, int sFlags,
{
UNREFERENCED_PARAMETER(bForceNext);
static char* chFind = NULL; // for speed (realloc())
static char chFind[8192] = { '\0' }; // max find buffer
DocPos iPos = -1LL; // not found
@ -5857,9 +5860,7 @@ static DocPos _FindInTarget(LPCWSTR wchFind, int sFlags,
SciCall_SetSearchFlags(sFlags);
SciCall_SetTargetRange(start, stop);
int const len = WideCharToMultiByte(Encoding_SciCP, 0, wchFind, -1, NULL, 0, NULL, NULL);
chFind = ReAllocMem(chFind, len * sizeof(char), HEAP_ZERO_MEMORY);
WideCharToMultiByte(Encoding_SciCP, 0, wchFind, -1, chFind, len, NULL, NULL);
DocPos const len = (DocPos)WideCharToMultiByte(Encoding_SciCP, 0, wchFind, -1, chFind, COUNTOF(chFind), NULL, NULL);
iPos = SciCall_SearchInTarget(len - 1, chFind);
// handle next in case of zero-length-matches (regex) !
@ -7543,14 +7544,12 @@ void EditMarkAll(LPCWSTR wchFind, int sFlags, DocPos rangeStart, DocPos rangeEnd
DocPos iFindLength = 0;
static LPWSTR pwchText = NULL;
char chText[2048] = { L'\0'};
WCHAR wchText[2048] = { L'\0'};
if (StrIsEmpty(wchFind)) {
static char* pchText = NULL;
int const len = max_i(WideCharToMultiByte(Encoding_SciCP, 0, wchFind, -1, NULL, 0, NULL, NULL), 256);
pchText = ReAllocMem(pchText, len * sizeof(char), HEAP_ZERO_MEMORY);
WideCharToMultiByte(Encoding_SciCP, 0, wchFind, -1, pchText, len, NULL, NULL);
WideCharToMultiByte(Encoding_SciCP, 0, wchFind, -1, chText, COUNTOF(chText), NULL, NULL);
if (SciCall_IsSelectionEmpty()) {
// nothing selected, get word under caret if flagged
@ -7563,7 +7562,8 @@ void EditMarkAll(LPCWSTR wchFind, int sFlags, DocPos rangeStart, DocPos rangeEnd
}
iFindLength = (iWordEnd - iWordStart);
StringCchCopyNA(pchText, SizeOfMem(pchText)/sizeof(char), SciCall_GetRangePointer(iWordStart, iFindLength), iFindLength);
StringCchCopyNA(chText, COUNTOF(chText), SciCall_GetRangePointer(iWordStart, iFindLength), iFindLength);
} else {
__leave; // no pattern, no selection and no word mark chosen
}
@ -7576,21 +7576,27 @@ void EditMarkAll(LPCWSTR wchFind, int sFlags, DocPos rangeStart, DocPos rangeEnd
// get current selection
DocPos const iSelStart = SciCall_GetSelectionStart();
DocPos const iSelEnd = SciCall_GetSelectionEnd();
DocPos const iSelCount = (iSelEnd - iSelStart);
//DocPos const iSelCount = (iSelEnd - iSelStart);
// if multiple lines are selected exit
if (SciCall_LineFromPosition(iSelStart) != SciCall_LineFromPosition(iSelEnd)) {
__leave;
}
iFindLength = SciCall_GetSelText(pchText);
DocPosU const iSelLen = SciCall_GetSelText(NULL);
if (iSelLen >= COUNTOF(chText)) {
__leave;
}
iFindLength = SciCall_GetSelText(chText);
chText[iFindLength] = '\0';
// exit if selection is not a word and Match whole words only is enabled
if (sFlags & SCFIND_WHOLEWORD) {
DocPos iSelStart2 = 0;
DocPosU iSelStart2 = 0;
const char* delims = (Settings.AccelWordNavigation ? DelimCharsAccel : DelimChars);
while ((iSelStart2 <= iSelCount) && pchText[iSelStart2]) {
if (StrChrIA(delims, pchText[iSelStart2])) {
while ((iSelStart2 <= iSelLen) && chText[iSelStart2]) {
if (StrChrIA(delims, chText[iSelStart2])) {
__leave;
}
++iSelStart2;
@ -7598,17 +7604,13 @@ void EditMarkAll(LPCWSTR wchFind, int sFlags, DocPos rangeStart, DocPos rangeEnd
}
}
int const length = MultiByteToWideChar(Encoding_SciCP, 0, pchText, (int)iFindLength, NULL, 0);
pwchText = ReAllocMem(pwchText, max_i(length + 1, 256) * sizeof(WCHAR), HEAP_ZERO_MEMORY);
MultiByteToWideChar(Encoding_SciCP, 0, pchText, (int)iFindLength, pwchText, (int)(SizeOfMem(pwchText) / sizeof(WCHAR)));
MultiByteToWideChar(Encoding_SciCP, 0, chText, (int)iFindLength, wchText, (int)COUNTOF(wchText));
} else {
pwchText = ReAllocMem(pwchText, max_s(StringCchLen(wchFind, 0) + 1, 256) * sizeof(WCHAR), HEAP_ZERO_MEMORY);
StringCchCopy(pwchText, SizeOfMem(pwchText) / sizeof(WCHAR), wchFind);
StringCchCopy(wchText, COUNTOF(wchText), wchFind);
}
if (StrIsNotEmpty(pwchText)) {
if (StrIsNotEmpty(wchText)) {
if (bMultiSel) {
SciCall_ClearSelections();
@ -7620,7 +7622,7 @@ void EditMarkAll(LPCWSTR wchFind, int sFlags, DocPos rangeStart, DocPos rangeEnd
DocPos start = rangeStart;
DocPos end = rangeEnd;
DocPos iPos = _FindInTarget(pwchText, sFlags, &start, &end, false, FRMOD_NORM);
DocPos iPos = _FindInTarget(wchText, sFlags, &start, &end, false, FRMOD_NORM);
DocPosU count = 0;
while ((iPos >= 0LL) && (start <= rangeEnd)) {
@ -7640,7 +7642,7 @@ void EditMarkAll(LPCWSTR wchFind, int sFlags, DocPos rangeStart, DocPos rangeEnd
++count;
start = end;
end = rangeEnd;
iPos = _FindInTarget(pwchText, sFlags, &start, &end, true, FRMOD_NORM);
iPos = _FindInTarget(wchText, sFlags, &start, &end, true, FRMOD_NORM);
};
Globals.iMarkOccurrencesCount = count;

View File

@ -22,6 +22,15 @@
#if !defined(NTDDI_VERSION)
#define NTDDI_VERSION 0x06010000 /*NTDDI_WIN7*/
#endif
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
#define DBG_NEW new (_NORMAL_BLOCK, __FILE__, __LINE__)
// Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the
// allocations to be of _CLIENT_BLOCK type
#else
#define DBG_NEW new
#endif
#define VC_EXTRALEAN 1
#define WIN32_LEAN_AND_MEAN 1
#define NOMINMAX 1

View File

@ -600,9 +600,20 @@ void BackgroundWorker_Init(BackgroundWorker* worker, HWND hwnd, HPATHL hFilePath
worker->hFilePath = hFilePath;
}
void BackgroundWorker_Stop(BackgroundWorker *worker) {
void BackgroundWorker_Start(BackgroundWorker* worker, _beginthreadex_proc_type routine, LPVOID property)
{
//~worker->workerThread = CreateThread(NULL, 0, routine, property, 0, NULL); // MD(d) dll
worker->workerThread = (HANDLE)_beginthreadex(NULL, 0, routine, property, 0, NULL); // MT(d) static
}
void BackgroundWorker_End(unsigned int retcode)
{
_endthreadex(retcode);
}
static void _BackgroundWorker_Stop(BackgroundWorker* worker) {
SetEvent(worker->eventCancel);
HANDLE workerThread = worker->workerThread;
HANDLE const workerThread = worker->workerThread;
if (workerThread) {
worker->workerThread = NULL;
while (WaitForSingleObject(workerThread, 0) != WAIT_OBJECT_0) {
@ -616,13 +627,13 @@ void BackgroundWorker_Stop(BackgroundWorker *worker) {
}
}
void BackgroundWorker_Cancel(BackgroundWorker *worker) {
BackgroundWorker_Stop(worker);
void BackgroundWorker_Cancel(BackgroundWorker* worker) {
_BackgroundWorker_Stop(worker);
ResetEvent(worker->eventCancel);
}
void BackgroundWorker_Destroy(BackgroundWorker *worker) {
BackgroundWorker_Stop(worker);
void BackgroundWorker_Destroy(BackgroundWorker* worker) {
_BackgroundWorker_Stop(worker);
CloseHandle(worker->eventCancel);
}
@ -1152,8 +1163,8 @@ bool SplitFilePathLineNum(LPWSTR lpszPath, int* lineNum)
//
size_t FormatNumberStr(LPWSTR lpNumberStr, size_t cch, int fixedWidth)
{
static WCHAR szSep[5] = { L'\0' };
static WCHAR szGrp[11] = { L'\0' };
static WCHAR szSep[SMALL_BUFFER] = { L'\0' };
static WCHAR szGrp[SMALL_BUFFER] = { L'\0' };
static int iPlace[4] = {-1,-1,-1,-1};
if (StrIsEmpty(lpNumberStr)) {

View File

@ -19,9 +19,10 @@
#include "TypeDefs.h"
#include <heapapi.h>
#include <process.h>
#include <math.h>
#include <shlwapi.h>
#include <heapapi.h>
#include <VersionHelpers.h>
#include "Scintilla.h"
@ -55,11 +56,26 @@
// ============================================================================
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
#ifndef _DEBUG
#define _DEBUG 1
#endif
#ifndef DEBUG
#define DEBUG 1
#endif
inline void* reallocz(void* pMem, size_t numBytes) { void* pM = realloc(pMem, numBytes); if (pM) memset(pM, 0, numBytes); return pM; }
#define AllocMem(B, F) (((F)&HEAP_ZERO_MEMORY) ? calloc(1, B) : malloc(B))
#define ReAllocMem(M, B, F) ((M) ? ((_msize(M) >= (B)) ? (((F)&HEAP_ZERO_MEMORY) ? (memset(M, 0, B), (M)) : (M)) : (((F)&HEAP_ZERO_MEMORY) ? reallocz(M, B) : realloc(M, B))) : AllocMem(B, F))
#define FreeMem(M) ((M ? (free(M)) : NOOP), true)
#define SizeOfMem(M) ((M) ? _msize(M) : 0)
#else // RELASE VERSION
// direct heap allocation
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
#define DEFAULT_ALLOC_FLAGS (HEAP_GENERATE_EXCEPTIONS)
#define DEFAULT_ALLOC_FLAGS (HEAP_GENERATE_EXCEPTIONS | HEAP_TAIL_CHECKING_ENABLED | HEAP_FREE_CHECKING_ENABLED | HEAP_CREATE_HARDENED)
#else
#define DEFAULT_ALLOC_FLAGS (0)
#define DEFAULT_ALLOC_FLAGS (HEAP_CREATE_HARDENED)
#endif
inline LPVOID AllocMem(size_t numBytes, DWORD dwFlags)
@ -73,7 +89,7 @@ inline LPVOID ReAllocMem(LPVOID lpMem, size_t numBytes, DWORD dwFlags)
size_t const memSize = HeapSize(Globals.hndlProcessHeap, 0, lpMem);
if (memSize >= numBytes) {
if (dwFlags & HEAP_ZERO_MEMORY) {
ZeroMemory(lpMem, memSize);
SecureZeroMemory(lpMem, memSize);
}
return lpMem;
}
@ -82,16 +98,18 @@ inline LPVOID ReAllocMem(LPVOID lpMem, size_t numBytes, DWORD dwFlags)
return HeapAlloc(Globals.hndlProcessHeap, (dwFlags | DEFAULT_ALLOC_FLAGS), numBytes);
}
inline bool FreeMem(LPVOID lpMemory)
inline bool FreeMem(LPVOID lpMem)
{
return (lpMemory ? HeapFree(Globals.hndlProcessHeap, 0, lpMemory) : true);
return (lpMem ? HeapFree(Globals.hndlProcessHeap, 0, lpMem) : true);
}
inline size_t SizeOfMem(LPCVOID lpMemory)
inline size_t SizeOfMem(LPCVOID lpMem)
{
return (lpMemory ? HeapSize(Globals.hndlProcessHeap, 0, lpMemory) : 0);
return (lpMem ? HeapSize(Globals.hndlProcessHeap, 0, lpMem) : 0);
}
#endif
// ============================================================================
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
@ -381,10 +399,11 @@ typedef struct BackgroundWorker {
} BackgroundWorker;
void BackgroundWorker_Init(BackgroundWorker* worker, HWND hwnd, HPATHL hFilePath);
void BackgroundWorker_Start(BackgroundWorker* worker, _beginthreadex_proc_type routine, LPVOID property);
void BackgroundWorker_End(unsigned int retcode);
void BackgroundWorker_Cancel(BackgroundWorker *worker);
void BackgroundWorker_Destroy(BackgroundWorker *worker);
#define BackgroundWorker_Continue(worker) \
(WaitForSingleObject((worker)->eventCancel, 0) != WAIT_OBJECT_0)
__forceinline bool BackgroundWorker_Continue(BackgroundWorker* worker) { return (WaitForSingleObject(worker->eventCancel, 0) != WAIT_OBJECT_0); }
bool BitmapMergeAlpha(HBITMAP hbmp,COLORREF crDest);
bool BitmapAlphaBlend(HBITMAP hbmp,COLORREF crDest,BYTE alpha);

View File

@ -140,7 +140,7 @@ static void SetMuiLocaleAll(LPCWSTR pszLocaleStr) {
if (pszLocaleCur && (StringCchCompareXI(pszLocaleStr, pszLocaleCur) != 0)) {
//const _locale_t pCurLocale = _get_current_locale();
_wsetlocale(LC_ALL, L""); // system standard
#ifdef _DEBUG
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
WCHAR msg[128];
StringCchPrintf(msg, COUNTOF(msg), L"Can't set desired locale '%s', using '%s' instead!",
pszLocaleStr, pszLocaleCur ? pszLocaleCur : L"<default>");
@ -276,7 +276,7 @@ static unsigned _CheckAvailableLanguageDLLs()
if (IsValidLocaleName(MUI_LanguageDLLs[lng].LocaleName)) {
#ifdef _DEBUG
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
WCHAR wchLngLocalName[LOCALE_NAME_MAX_LENGTH + 1];
if (ResolveLocaleName(MUI_LanguageDLLs[lng].LocaleName, wchLngLocalName, COUNTOF(wchLngLocalName))) {
//~StringCchCopy(MUI_LanguageDLLs[lng].LocaleName, COUNTOF(MUI_LanguageDLLs[lng].LocaleName), wchLngLocalName); // put back resolved name
@ -378,7 +378,7 @@ unsigned LoadLanguageResources(LPCWSTR pLocaleName) {
// using SetProcessPreferredUILanguages is recommended for new applications (esp. multi-threaded applications)
SetProcessPreferredUILanguages(0, L"\0\0", &langCount); // clear
if (!SetProcessPreferredUILanguages(MUI_LANGUAGE_NAME, tchUserLangMultiStrg, &langCount) || (langCount == 0)) {
#ifdef _DEBUG
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
MsgBoxLastError(L"Trying to set preferred Language!", ERROR_RESOURCE_LANG_NOT_FOUND);
#endif
}

View File

@ -16,7 +16,12 @@
#include "Helpers.h"
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
#include <commctrl.h>
#include <uxtheme.h>
#include <shlobj.h>
@ -44,8 +49,8 @@
#include "DarkMode/DarkMode.h"
#include "StyleLexers/EditLexer.h"
#if defined(DEBUG) || defined(_DEBUG)
#if defined(WIN32) && !defined(_WIN64)
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
#if defined(WIN32) && !defined(_WIN64)
#pragma comment(linker, "/defaultlib:clang_rt.asan-i386.lib")
#endif // _WIN64
#endif // DEBUG
@ -254,50 +259,45 @@ static void InitUndoRedoSelection(void* elt)
}
}
static void DelUndoRedoSelection(void* elt)
{
UndoRedoSelection_t* selection = (UndoRedoSelection_t*)elt;
if (selection != NULL) {
if (selection != NULL)
{
selection->selMode_undo = SC_SEL_STREAM;
selection->selMode_redo = SC_SEL_STREAM;
if (selection->anchorPos_undo != NULL) {
utarray_clear(selection->anchorPos_undo);
utarray_free(selection->anchorPos_undo);
selection->anchorPos_undo = NULL;
}
if (selection->curPos_undo != NULL) {
utarray_clear(selection->curPos_undo);
utarray_free(selection->curPos_undo);
selection->curPos_undo = NULL;
}
if (selection->anchorVS_undo != NULL) {
utarray_clear(selection->anchorVS_undo);
utarray_free(selection->anchorVS_undo);
selection->anchorVS_undo = NULL;
}
if (selection->curVS_undo != NULL) {
utarray_clear(selection->curVS_undo);
utarray_free(selection->curVS_undo);
selection->curVS_undo = NULL;
}
if (selection->anchorPos_redo != NULL) {
utarray_clear(selection->anchorPos_redo);
utarray_free(selection->anchorPos_redo);
selection->anchorPos_redo = NULL;
}
if (selection->curPos_redo != NULL) {
utarray_clear(selection->curPos_redo);
utarray_free(selection->curPos_redo);
selection->curPos_redo = NULL;
}
if (selection->anchorVS_redo != NULL) {
utarray_clear(selection->anchorVS_redo);
utarray_free(selection->anchorVS_redo);
selection->anchorVS_redo = NULL;
}
if (selection->curVS_redo != NULL) {
utarray_clear(selection->curVS_redo);
utarray_free(selection->curVS_redo);
selection->curVS_redo = NULL;
}
@ -810,6 +810,8 @@ static void _InitGlobals()
s_pthArgFilePath = Path_Allocate(NULL);
// don't allow empty extensions settings => use default ext
Style_InitFileExtensions();
}
@ -836,8 +838,8 @@ static void _CleanUpResources(const HWND hwnd, bool bIsInitialized)
DL_DELETE(MessageQueue, pmqc);
FreeMem(pmqc);
}
if (UndoRedoSelectionUTArray != NULL) {
utarray_clear(UndoRedoSelectionUTArray);
utarray_free(UndoRedoSelectionUTArray);
UndoRedoSelectionUTArray = NULL;
}
@ -851,9 +853,9 @@ static void _CleanUpResources(const HWND hwnd, bool bIsInitialized)
s_hEventFileDeletedExt = INVALID_HANDLE_VALUE;
}
// --------------------------------------
// Save Settings is done elsewhere before
// --------------------------------------
// ---------------------------------------------
// Save Settings should be done elsewhere before
// ---------------------------------------------
if (Globals.hMainMenu) {
DestroyMenu(Globals.hMainMenu);
@ -867,9 +869,6 @@ static void _CleanUpResources(const HWND hwnd, bool bIsInitialized)
Scintilla_ReleaseResources();
OleUninitialize();
CoUninitialize();
if (bIsInitialized) {
//~UnregisterClass(s_ToolbarWndClassName, Globals.hInstance);
UnregisterClass(s_wchWndClass, Globals.hInstance);
@ -877,16 +876,19 @@ static void _CleanUpResources(const HWND hwnd, bool bIsInitialized)
ReleaseDarkMode();
OleUninitialize();
CoUninitialize();
// --- free allocated memory ---
if (s_lpOrigFileArg) {
FreeMem(s_lpOrigFileArg);
s_lpOrigFileArg = NULL;
}
if (_hOldInvalidParamHandler) {
_set_invalid_parameter_handler(_hOldInvalidParamHandler);
}
// --- free allocated memory ---
MRU_Destroy(Globals.pFileMRU);
MRU_Destroy(Globals.pMRUfind);
MRU_Destroy(Globals.pMRUreplace);
StrgDestroy(Settings2.FileDlgFilters);
StrgDestroy(Settings2.HyperlinkShellExURLCmdLnArgs);
@ -923,6 +925,54 @@ static void _CleanUpResources(const HWND hwnd, bool bIsInitialized)
ThemesItems_Release();
Path_Release(s_hpthRelaunchElevatedFile);
// ---------------------------------------------
if (Globals.hDlgIcon256) {
DestroyIcon(Globals.hDlgIcon256);
}
if (Globals.hDlgIcon128) {
DestroyIcon(Globals.hDlgIcon128);
}
if (Globals.hDlgIconBig) {
DestroyIcon(Globals.hDlgIconBig);
}
if (Globals.hDlgIconSmall) {
DestroyIcon(Globals.hDlgIconSmall);
}
if (Globals.hDlgIconPrefs256) {
DestroyIcon(Globals.hDlgIconPrefs256);
}
if (Globals.hDlgIconPrefs128) {
DestroyIcon(Globals.hDlgIconPrefs128);
}
if (Globals.hDlgIconPrefs64) {
DestroyIcon(Globals.hDlgIconPrefs64);
}
if (Globals.hIconMsgUser) {
DestroyIcon(Globals.hIconMsgUser);
}
if (Globals.hIconMsgInfo) {
DestroyIcon(Globals.hIconMsgInfo);
}
if (Globals.hIconMsgWarn) {
DestroyIcon(Globals.hIconMsgWarn);
}
if (Globals.hIconMsgError) {
DestroyIcon(Globals.hIconMsgError);
}
if (Globals.hIconMsgQuest) {
DestroyIcon(Globals.hIconMsgQuest);
}
if (Globals.hIconMsgShield) {
DestroyIcon(Globals.hIconMsgShield);
}
// install previous handler
if (_hOldInvalidParamHandler) {
_set_invalid_parameter_handler(_hOldInvalidParamHandler);
}
}
@ -942,7 +992,7 @@ void InvalidParameterHandler(const wchar_t* expression,
UNREFERENCED_PARAMETER(file);
UNREFERENCED_PARAMETER(line);
UNREFERENCED_PARAMETER(pReserved);
#ifdef _DEBUG
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
WCHAR msg[256];
StringCchPrintf(msg, COUNTOF(msg),
L"Invalid Parameter in function '%s()' - File:'%s' Line:%i !",
@ -960,7 +1010,11 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
{
_invalid_parameter_handler const hNewInvalidParamHandler = InvalidParameterHandler;
_hOldInvalidParamHandler= _set_invalid_parameter_handler(hNewInvalidParamHandler);
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_CRT_DF | _CRTDBG_LEAK_CHECK_DF);
_CrtSetReportMode(_CRT_ASSERT, 0); // Disable the message box for assertions.
#endif
_InitGlobals();
InitDarkMode();
@ -1039,16 +1093,19 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
HRSRC const hRes = FindResourceEx(hInstance, RT_RCDATA, MAKEINTRESOURCE(IDR_STD_DARKMODE_THEME),
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
HGLOBAL const hMem = LoadResource(hInstance, hRes);
DWORD const size = SizeofResource(hInstance, hRes);
const char * const resText = (const char *)LockResource(hMem);
Globals.pStdDarkModeIniStyles = (char *)AllocMem(((size_t)size + 1), 0);
if (Globals.pStdDarkModeIniStyles) {
memcpy_s(Globals.pStdDarkModeIniStyles, size + 1, resText, size);
Globals.pStdDarkModeIniStyles[size] = '\0'; // zero termination
if (hRes) {
HGLOBAL const hMem = LoadResource(hInstance, hRes);
if (hMem) {
const char* const resText = (const char*)LockResource(hMem);
DWORD const size = SizeofResource(hInstance, hRes);
Globals.pStdDarkModeIniStyles = (char*)AllocMem(((size_t)size + 1), 0);
if (Globals.pStdDarkModeIniStyles) {
CopyMemory(Globals.pStdDarkModeIniStyles, resText, size);
Globals.pStdDarkModeIniStyles[size] = '\0'; // zero termination
}
FreeResource(hMem);
}
}
FreeResource(hMem);
Style_ImportTheme(-1); // init (!)
Style_ImportTheme(Globals.uCurrentThemeIndex);
@ -1206,6 +1263,9 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
_CleanUpResources(hwnd, true);
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif
return (int)(msg.wParam);
}
@ -1683,7 +1743,6 @@ HWND InitInstance(const HINSTANCE hInstance, LPCWSTR pszCmdLine, int nCmdShow)
// undo / redo selections
if (UndoRedoSelectionUTArray != NULL) {
utarray_clear(UndoRedoSelectionUTArray);
utarray_free(UndoRedoSelectionUTArray);
UndoRedoSelectionUTArray = NULL;
}
@ -10021,8 +10080,8 @@ static int _SaveUndoSelection()
{
static DocPosU _s_iSelection = 0; // index
UndoRedoSelection_t sel = INIT_UNDOREDOSEL;
CopyUndoRedoSelection(&sel, NULL); // init
UndoRedoSelection_t sel = INIT_UNDOREDOSEL; // = InitUndoRedoSelection(&sel);
CopyUndoRedoSelection(&sel, NULL); // utarray_new()
DocPosU const numOfSel = SciCall_GetSelections();
@ -10094,6 +10153,9 @@ static int _SaveUndoSelection()
//~SciCall_AddUndoAction(token, UNDO_MAY_COALESCE);
SciCall_AddUndoAction(token, UNDO_NONE);
}
DelUndoRedoSelection(&sel); // utarray_free()
_s_iSelection = 0; // reset
return token;
@ -10346,7 +10408,8 @@ static int _UndoRedoActionMap(int token, const UndoRedoSelection_t** selection)
EndUndoAction(curToken);
}
utarray_clear(UndoRedoSelectionUTArray);
utarray_init(UndoRedoSelectionUTArray, &UndoRedoSelection_icd);
//utarray_free(UndoRedoSelectionUTArray);
//utarray_init(UndoRedoSelectionUTArray, &UndoRedoSelection_icd);
uiTokenCnt = 0U;
InterlockedExchange(&UndoActionToken, UNDOREDO_FREE);
return -1;

View File

@ -23,7 +23,7 @@ VS_VERSION_INFO VERSIONINFO
FILEVERSION VERSION_FILEVERSION_NUM
PRODUCTVERSION VERSION_FILEVERSION_NUM
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
FILEFLAGS VS_FF_PATCHED | VS_FF_DEBUG
#else
FILEFLAGS VS_FF_PATCHED

View File

@ -1371,7 +1371,7 @@ size_t PTHAPI Path_ToShortPathName(HPATHL hpth_in_out)
DWORD const _len = GetShortPathNameW(StrgGet(hstr_io), NULL, 0);
if (!_len) {
#ifdef DEBUG
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
MsgBoxLastError(L"Path_ToShortPathName()", 0);
#endif // DEBUG
return 0;

View File

@ -16,6 +16,14 @@
* *
*******************************************************************************/
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
#define DBG_NEW new (_NORMAL_BLOCK, __FILE__, __LINE__)
// Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the
// allocations to be of _CLIENT_BLOCK type
#else
#define DBG_NEW new
#endif
#if !defined(WINVER)
#define WINVER 0x601 /*_WIN32_WINNT_WIN7*/
#endif
@ -226,8 +234,8 @@ extern "C" bool EditPrint(HWND hwnd,LPCWSTR pszDocTitle,LPCWSTR pszPageFormat)
// thousandths of inches (HiEnglish) margin values
// from the Page Setup dialog to device units.
// (There are 2540 hundredths of a mm in an inch.)
WCHAR localeInfo[3];
GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_IMEASURE, localeInfo, 3);
WCHAR localeInfo[SMALL_BUFFER];
GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_IMEASURE, localeInfo, COUNTOF(localeInfo));
RECT rectSetup;
if (localeInfo[0] == L'0') { // Metric system. L'1' is US System

View File

@ -740,7 +740,7 @@ DeclareSciCallR0(IsSelectionRectangle, SELECTIONISRECTANGLE, bool);
// length of line w/o line-end chars (full use SciCall_LineLength()
#define Sci_GetNetLineLength(line) (SciCall_GetLineEndPosition(line) - SciCall_PositionFromLine(line))
//~#define Sci_GetDocEndPosition() (SciCall_GetTextLength() - 1)
//~#define Sci_GetDocEndPosition() SciCall_GetTextLength()
#define Sci_GetDocEndPosition() SciCall_PositionAfter(SciCall_GetTextLength() - 1)
#define Sci_ClampAlpha(alpha) clampi((alpha), SC_ALPHA_TRANSPARENT, SC_ALPHA_OPAQUE) //~SC_ALPHA_NOALPHA

View File

@ -467,6 +467,14 @@ static inline void AppendStyle(LPWSTR lpszStyleDest, size_t cchSizeDest, LPCWSTR
//=============================================================================
void Style_InitFileExtensions()
{
for (int iLexer = 0; iLexer < COUNTOF(g_pLexArray); iLexer++) {
if (StrIsEmpty(g_pLexArray[iLexer]->szExtensions)) {
StringCchCopy(g_pLexArray[iLexer]->szExtensions, COUNTOF(g_pLexArray[iLexer]->szExtensions), g_pLexArray[iLexer]->pszDefExt);
}
}
}
//=============================================================================
//

View File

@ -29,6 +29,7 @@
int Style_NumOfLexers(); // Number of Lexers in pLexArray
void Style_InitFileExtensions();
void Style_Prerequisites();
bool Style_Import(HWND hwnd);
bool Style_ImportTheme(const int iThemeIdx); // -1 => Factory Reset

View File

@ -23,6 +23,9 @@
#define NTDDI_VERSION 0x06010000 /*NTDDI_WIN7*/
#endif
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
#endif
// Want to use std::min and std::max so don't want Windows.h version of min and max
#if !defined(NOMINMAX)
#define NOMINMAX

View File

@ -36,7 +36,7 @@
#endif
#if defined(_DEBUG) || defined(DEBUG)
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
#pragma message("Debug Build: " _STRG(VERSION_FILEVERSION_LONG))
#else
#pragma message("Release Build: " _STRG(VERSION_FILEVERSION_LONG))

View File

@ -3,8 +3,11 @@
#define __CRYPTO_H__
#include <stdbool.h>
#ifdef _DEBUG
#define BUG1(a,b) { perror("a"); }
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
#define BUG1(a, b) \
{ \
perror("a"); \
}
#define BUG(a) { perror("a"); }
#else
#define BUG1(a,b) ((void)0);

View File

@ -12,7 +12,7 @@ No \\n's!",
hexadecimal: 0xdecaf,
leadingDecimalPoint: 1234567, .8675309, andTrailing: 8675309.,
positiveSign: +1,
trailingComma: "in objects", andIn: ["arrays",],
trailingComma: 'in objects', andIn: ['arrays',],
escSeq: "\uAFFE",
"backwardsCompatible": "with JSON",
Infinity: Infinity,