From 9e185670eae30627a6e384f46b0db30bd6448eb7 Mon Sep 17 00:00:00 2001 From: Rainer Kottenhoff Date: Sat, 2 May 2020 22:24:38 +0200 Subject: [PATCH] + fix: .ini-file acquire lock for invalid file path --- Versions/build.txt | 2 +- res/Notepad3.exe.manifest.conf | 2 +- src/Config/Config.cpp | 72 +++++++++++++++------------------- src/Helpers.c | 42 ++++++++++++-------- src/Helpers.h | 2 +- src/Notepad3.c | 23 ++++++----- src/VersionEx.h | 2 +- 7 files changed, 73 insertions(+), 72 deletions(-) diff --git a/Versions/build.txt b/Versions/build.txt index 0cfbf0888..00750edc0 100644 --- a/Versions/build.txt +++ b/Versions/build.txt @@ -1 +1 @@ -2 +3 diff --git a/res/Notepad3.exe.manifest.conf b/res/Notepad3.exe.manifest.conf index a6899bf65..9a18a1962 100644 --- a/res/Notepad3.exe.manifest.conf +++ b/res/Notepad3.exe.manifest.conf @@ -3,7 +3,7 @@ Notepad3 BETA diff --git a/src/Config/Config.cpp b/src/Config/Config.cpp index f7a9424ef..c6c61fb6f 100644 --- a/src/Config/Config.cpp +++ b/src/Config/Config.cpp @@ -784,24 +784,16 @@ static bool _CheckIniFile(LPWSTR lpszFile, LPCWSTR lpszModule) // ============================================================================ -static bool _CheckIniFileRedirect(LPWSTR lpszAppName, LPWSTR lpszKeyName, LPWSTR lpszFile, LPCWSTR lpszModule) +static bool _HandleIniFileRedirect(LPWSTR lpszAppName, LPWSTR lpszKeyName, LPWSTR lpszFile, LPCWSTR lpszModule) { - WCHAR tch[MAX_PATH] = { L'\0' }; - if (IniFileGetString(lpszFile, lpszAppName, lpszKeyName, L"", tch, COUNTOF(tch))) + WCHAR wchPath[MAX_PATH] = { L'\0' }; + if (PathFileExists(lpszFile) && IniFileGetString(lpszFile, lpszAppName, lpszKeyName, L"", wchPath, COUNTOF(wchPath))) { - if (_CheckIniFile(tch, lpszModule)) { - StringCchCopy(lpszFile, MAX_PATH, tch); - return true; + if (!_CheckIniFile(wchPath, lpszModule)) { + PathCanonicalizeEx(wchPath, COUNTOF(wchPath)); } - WCHAR tchFileExpanded[MAX_PATH] = { L'\0' }; - ExpandEnvironmentStrings(tch, tchFileExpanded, COUNTOF(tchFileExpanded)); - if (PathIsRelative(tchFileExpanded)) { - StringCchCopy(lpszFile, MAX_PATH, lpszModule); - StringCchCopy(PathFindFileName(lpszFile), MAX_PATH, tchFileExpanded); - return true; - } - StringCchCopy(lpszFile, MAX_PATH, tchFileExpanded); - return true; + StringCchCopy(lpszFile, MAX_PATH, wchPath); + return true; // try to use redirection path } return false; } @@ -811,52 +803,50 @@ static bool _CheckIniFileRedirect(LPWSTR lpszAppName, LPWSTR lpszKeyName, LPWST extern "C" bool FindIniFile() { bool bFound = false; - WCHAR tchPath[MAX_PATH] = { L'\0' }; - WCHAR tchModule[MAX_PATH] = { L'\0' }; + WCHAR tchModule[MAX_PATH] = { L'\0' }; GetModuleFileName(NULL, tchModule, COUNTOF(tchModule)); PathCanonicalizeEx(tchModule, COUNTOF(tchModule)); // set env path to module dir - StringCchCopy(tchPath, COUNTOF(tchPath), tchModule); - PathCchRemoveFileSpec(tchPath, COUNTOF(tchPath)); - SetEnvironmentVariable(NOTEPAD3_MODULE_DIR_ENV_VAR, tchPath); + WCHAR wchIniFilePath[MAX_PATH] = { L'\0' }; + StringCchCopy(wchIniFilePath, COUNTOF(wchIniFilePath), tchModule); + PathCchRemoveFileSpec(wchIniFilePath, COUNTOF(wchIniFilePath)); - if (StrIsNotEmpty(Globals.IniFile)) { + SetEnvironmentVariable(NOTEPAD3_MODULE_DIR_ENV_VAR, wchIniFilePath); + + if (StrIsNotEmpty(Globals.IniFile)) + { if (StringCchCompareXI(Globals.IniFile, L"*?") == 0) { return bFound; } - if (!_CheckIniFile(Globals.IniFile, tchModule)) { - ExpandEnvironmentStringsEx(Globals.IniFile, COUNTOF(Globals.IniFile)); - if (PathIsRelative(Globals.IniFile)) { - StringCchCopy(tchPath, COUNTOF(tchPath), tchModule); - PathCchRemoveFileSpec(tchPath, COUNTOF(tchPath)); - PathCchAppend(tchPath, COUNTOF(tchPath), Globals.IniFile); - StringCchCopy(Globals.IniFile, COUNTOF(Globals.IniFile), tchPath); - } - } + + PathCanonicalizeEx(Globals.IniFile, COUNTOF(Globals.IniFile)); + bFound = _CheckIniFile(wchIniFilePath, tchModule); } - else { - StringCchCopy(tchPath, COUNTOF(tchPath), PathFindFileName(tchModule)); - PathCchRenameExtension(tchPath, COUNTOF(tchPath), L".ini"); - - bFound = _CheckIniFile(tchPath, tchModule); + else + { + StringCchCopy(wchIniFilePath, COUNTOF(wchIniFilePath), PathFindFileName(tchModule)); + PathCchRenameExtension(wchIniFilePath, COUNTOF(wchIniFilePath), L".ini"); + bFound = _CheckIniFile(wchIniFilePath, tchModule); if (!bFound) { - StringCchCopy(tchPath, COUNTOF(tchPath), L"Notepad3.ini"); - bFound = _CheckIniFile(tchPath, tchModule); + StringCchCopy(wchIniFilePath, COUNTOF(wchIniFilePath), _W(SAPPNAME) L".ini"); + bFound = _CheckIniFile(wchIniFilePath, tchModule); } if (bFound) { // allow two redirections: administrator -> user -> custom - if (_CheckIniFileRedirect(_W(SAPPNAME), _W(SAPPNAME) L".ini", tchPath, tchModule)) + if (_HandleIniFileRedirect(_W(SAPPNAME), _W(SAPPNAME) L".ini", wchIniFilePath, tchModule)) // 1st { - _CheckIniFileRedirect(_W(SAPPNAME), _W(SAPPNAME) L".ini", tchPath, tchModule); + _HandleIniFileRedirect(_W(SAPPNAME), _W(SAPPNAME) L".ini", wchIniFilePath, tchModule); // 2nd + bFound = _CheckIniFile(wchIniFilePath, tchModule); } - StringCchCopy(Globals.IniFile, COUNTOF(Globals.IniFile), tchPath); + StringCchCopy(Globals.IniFile, COUNTOF(Globals.IniFile), wchIniFilePath); } - else { + else // force default name + { StringCchCopy(Globals.IniFile, COUNTOF(Globals.IniFile), tchModule); PathCchRenameExtension(Globals.IniFile, COUNTOF(Globals.IniFile), L".ini"); } diff --git a/src/Helpers.c b/src/Helpers.c index 0712edcfe..d01f21f62 100644 --- a/src/Helpers.c +++ b/src/Helpers.c @@ -798,8 +798,8 @@ bool GetKnownFolderPath(REFKNOWNFOLDERID rfid, LPWSTR lpOutPath, size_t cchCount void PathGetAppDirectory(LPWSTR lpszDest, DWORD cchDest) { GetModuleFileName(NULL, lpszDest, cchDest); - PathCanonicalizeEx(lpszDest, cchDest); PathCchRemoveFileSpec(lpszDest, (size_t)cchDest); + PathCanonicalizeEx(lpszDest, cchDest); } @@ -884,20 +884,20 @@ void PathAbsoluteFromApp(LPWSTR lpszSrc,LPWSTR lpszDest,int cchDest,bool bExpand } } - if (bExpandEnv) - ExpandEnvironmentStringsEx(wchPath,COUNTOF(wchPath)); - + if (bExpandEnv) { + ExpandEnvironmentStringsEx(wchPath, COUNTOF(wchPath)); + } if (PathIsRelative(wchPath)) { PathGetAppDirectory(wchResult, COUNTOF(wchResult)); PathCchAppend(wchResult,COUNTOF(wchResult),wchPath); } - else - StringCchCopyN(wchResult,COUNTOF(wchResult),wchPath,COUNTOF(wchPath)); - + else { + StringCchCopyN(wchResult, COUNTOF(wchResult), wchPath, COUNTOF(wchPath)); + } PathCanonicalizeEx(wchResult,MAX_PATH); - if (PathGetDriveNumber(wchResult) != -1) - CharUpperBuff(wchResult,1); - + if (PathGetDriveNumber(wchResult) != -1) { + CharUpperBuff(wchResult, 1); + } if (lpszDest == NULL || lpszSrc == lpszDest) StringCchCopyN(lpszSrc,((cchDest == 0) ? MAX_PATH : cchDest),wchResult,COUNTOF(wchResult)); else @@ -975,7 +975,6 @@ bool PathGetLnkPath(LPCWSTR pszLnkFile,LPWSTR pszResPath,int cchResPath) } if (bSucceeded) { - ExpandEnvironmentStringsEx(pszResPath,cchResPath); PathCanonicalizeEx(pszResPath,cchResPath); } @@ -1321,7 +1320,7 @@ void PathFixBackslashes(LPWSTR lpsz) // void ExpandEnvironmentStringsEx(LPWSTR lpSrc, DWORD dwSrc) { - WCHAR szBuf[HUGE_BUFFER]; + WCHAR szBuf[XXXL_BUFFER]; if (ExpandEnvironmentStrings(lpSrc, szBuf, COUNTOF(szBuf))) { StringCchCopyN(lpSrc, dwSrc, szBuf, COUNTOF(szBuf)); } @@ -1333,12 +1332,22 @@ void ExpandEnvironmentStringsEx(LPWSTR lpSrc, DWORD dwSrc) // PathCanonicalizeEx() // // -void PathCanonicalizeEx(LPWSTR lpszPath, DWORD cchBuffer) +bool PathCanonicalizeEx(LPWSTR lpszPath, DWORD cchPath) { - WCHAR szDst[MAX_PATH] = { L'\0' }; - if (PathCchCanonicalize(szDst, MAX_PATH, lpszPath) == S_OK) { - StringCchCopy(lpszPath, cchBuffer, szDst); + WCHAR filePath[MAX_PATH] = { L'\0' }; + StringCchCopyN(filePath, COUNTOF(filePath), lpszPath, cchPath); + + ExpandEnvironmentStringsEx(filePath, COUNTOF(filePath)); + + if (PathIsRelative(filePath)) + { + WCHAR tchModule[MAX_PATH] = { L'\0' }; + GetModuleFileName(NULL, tchModule, COUNTOF(tchModule)); + PathCchRemoveFileSpec(tchModule, COUNTOF(tchModule)); + PathCchAppend(tchModule, COUNTOF(tchModule), lpszPath); + StringCchCopyN(filePath, COUNTOF(filePath), tchModule, COUNTOF(tchModule)); } + return (PathCchCanonicalize(lpszPath, cchPath, filePath) == S_OK); } @@ -1410,7 +1419,6 @@ void PathGetDisplayName(LPWSTR lpszDestPath, DWORD cchDestBuffer, LPCWSTR lpszSo DWORD NormalizePathEx(LPWSTR lpszPath, DWORD cchBuffer, bool bRealPath, bool bSearchPathIfRelative) { WCHAR tmpFilePath[MAX_PATH] = { L'\0' }; - StringCchCopyN(tmpFilePath, COUNTOF(tmpFilePath), lpszPath, cchBuffer); ExpandEnvironmentStringsEx(tmpFilePath, COUNTOF(tmpFilePath)); diff --git a/src/Helpers.h b/src/Helpers.h index c7ccefc2b..a67b7336f 100644 --- a/src/Helpers.h +++ b/src/Helpers.h @@ -273,7 +273,7 @@ bool PathCreateDeskLnk(LPCWSTR pszDocument); bool PathCreateFavLnk(LPCWSTR pszName,LPCWSTR pszTarget,LPCWSTR pszDir); void ExpandEnvironmentStringsEx(LPWSTR lpSrc, DWORD dwSrc); -void PathCanonicalizeEx(LPWSTR lpszPath, DWORD cchBuffer); +bool PathCanonicalizeEx(LPWSTR lpszPath, DWORD cchBuffer); DWORD GetLongPathNameEx(LPWSTR lpszPath, DWORD cchBuffer); void PathGetDisplayName(LPWSTR lpszDestPath, DWORD cchDestBuffer, LPCWSTR lpszSourcePath); DWORD NormalizePathEx(LPWSTR lpszPath, DWORD cchBuffer, bool bRealPath, bool bSearchPathIfRelative); diff --git a/src/Notepad3.c b/src/Notepad3.c index a5f43d425..0587c4ed1 100644 --- a/src/Notepad3.c +++ b/src/Notepad3.c @@ -6619,7 +6619,6 @@ bool HandleHotSpotURLClicked(const DocPos position, const HYPERLINK_OPS operatio bool bHandled = false; if (SciCall_IndicatorValueAt(INDIC_NP3_HYPERLINK, position)) { - DocPos const firstPos = SciCall_IndicatorStart(INDIC_NP3_HYPERLINK, position); DocPos const lastPos = SciCall_IndicatorEnd(INDIC_NP3_HYPERLINK, position); DocPos const length = min_p(lastPos - firstPos, INTERNET_MAX_URL_LENGTH); @@ -9506,10 +9505,12 @@ bool FileLoad(bool bDontSave, bool bNew, bool bReload, bool bUnknownLexer = s_flagLexerSpecified; - BeginWaitCursor(true,L"Styling..."); - - if (fSuccess) + if (fSuccess) { + BeginWaitCursor(true, L"Styling..."); + + SciCall_GotoPos(0); + if (!s_IsThisAnElevatedRelaunch) { Flags.bPreserveFileModTime = DefaultFlags.bPreserveFileModTime; } @@ -9570,11 +9571,12 @@ bool FileLoad(bool bDontSave, bool bNew, bool bReload, _END_UNDO_ACTION_; SciCall_ScrollToEnd(); } - // set historic caret/selection pos - else if ((iCaretPos >= 0) && (iAnchorPos >= 0)) - { - EditSetSelectionEx(iAnchorPos, iCaretPos, 0, 0); - } + } + + // set historic caret/selection pos + if ((iCaretPos >= 0) && (iAnchorPos >= 0) && (SciCall_GetCurrentPos() == 0)) + { + EditSetSelectionEx(iAnchorPos, iCaretPos, -1, -1); } //bReadOnly = false; @@ -9631,12 +9633,13 @@ bool FileLoad(bool bDontSave, bool bNew, bool bReload, Globals.fvCurFile.bTabsAsSpaces = (fioStatus.indentCount[I_TAB_LN] < fioStatus.indentCount[I_SPC_LN]) ? true : false; SciCall_SetUseTabs(!Globals.fvCurFile.bTabsAsSpaces); } + + EndWaitCursor(); } else if (!(Flags.bLargeFileLoaded || fioStatus.bUnknownExt)) { InfoBoxLng(MB_ICONWARNING, NULL, IDS_MUI_ERR_LOADFILE, PathFindFileName(szFileName)); } - EndWaitCursor(); UpdateToolbar(); UpdateStatusbar(true); UpdateMarginWidth(); diff --git a/src/VersionEx.h b/src/VersionEx.h index a4cd9283a..d4c3b4d95 100644 --- a/src/VersionEx.h +++ b/src/VersionEx.h @@ -9,7 +9,7 @@ #define VERSION_MAJOR 5 #define VERSION_MINOR 20 #define VERSION_REV 502 -#define VERSION_BUILD 2 +#define VERSION_BUILD 3 #define SCINTILLA_VER 433 #define ONIGURUMA_REGEX_VER 6.9.5 #define UCHARDET_VER 2018.09.27