From 55099cb4b7caf969222ff62e805426341f6a5e02 Mon Sep 17 00:00:00 2001 From: Don Ho Date: Sat, 14 Mar 2015 22:55:03 +0000 Subject: [PATCH] [BUG_FIXED] (Author: Alexander Riccio) Fix several bugs. git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository/trunk@1352 f5eea248-9336-0410-98b8-ebc06183d4e3 --- PowerEditor/src/NppCommands.cpp | 6 ++--- PowerEditor/src/NppIO.cpp | 34 ++++++++++++++++++++--- PowerEditor/src/NppNotification.cpp | 42 ++++++++++++++--------------- PowerEditor/src/Parameters.cpp | 42 ++++++++++++++++++++++++----- PowerEditor/src/Parameters.h | 3 ++- PowerEditor/src/winmain.cpp | 29 +++++++++++++++++--- 6 files changed, 118 insertions(+), 38 deletions(-) diff --git a/PowerEditor/src/NppCommands.cpp b/PowerEditor/src/NppCommands.cpp index 9baee30b..1e34d619 100644 --- a/PowerEditor/src/NppCommands.cpp +++ b/PowerEditor/src/NppCommands.cpp @@ -339,7 +339,7 @@ void Notepad_plus::command(int id) case IDM_EDIT_BEGINENDSELECT: { - ::CheckMenuItem(_mainMenuHandle, IDM_EDIT_BEGINENDSELECT, MF_BYCOMMAND | _pEditView->beginEndSelectedIsStarted()?MF_UNCHECKED:MF_CHECKED); + ::CheckMenuItem(_mainMenuHandle, IDM_EDIT_BEGINENDSELECT, MF_BYCOMMAND | (_pEditView->beginEndSelectedIsStarted() ? MF_UNCHECKED : MF_CHECKED)); _pEditView->beginOrEndSelect(); } break; @@ -1539,7 +1539,7 @@ void Notepad_plus::command(int id) characterNumber += TEXT("\r"); TCHAR fileLenStr[64]; - generic_sprintf(fileLenStr, TEXT("%d"), (size_t)fileLen); + generic_sprintf(fileLenStr, TEXT("%I64u"), static_cast( fileLen ) ); characterNumber += fileLenLabel; characterNumber += fileLenStr; characterNumber += TEXT("\r"); @@ -1580,7 +1580,7 @@ void Notepad_plus::command(int id) characterNumber += nbWordStr; characterNumber += TEXT("\r"); - generic_sprintf(nbLineStr, TEXT("%d"), nbLine); + generic_sprintf(nbLineStr, TEXT("%d"), static_cast( nbLine ) ); characterNumber += nbLineLabel; characterNumber += nbLineStr; characterNumber += TEXT("\r"); diff --git a/PowerEditor/src/NppIO.cpp b/PowerEditor/src/NppIO.cpp index e4cd7617..3029d343 100644 --- a/PowerEditor/src/NppIO.cpp +++ b/PowerEditor/src/NppIO.cpp @@ -37,11 +37,37 @@ BufferID Notepad_plus::doOpen(const TCHAR *fileName, bool isRecursive, bool isReadOnly, int encoding, const TCHAR *backupFileName, time_t fileNameTimestamp) { - NppParameters *pNppParam = NppParameters::getInstance(); - TCHAR longFileName[MAX_PATH]; + + const rsize_t longFileNameBufferSize = MAX_PATH; - ::GetFullPathName(fileName, MAX_PATH, longFileName, NULL); - ::GetLongPathName(longFileName, longFileName, MAX_PATH); + //If [GetFullPathName] succeeds, the return value is the length, in TCHARs, of the string copied to lpBuffer, not including the terminating null character. + //If the lpBuffer buffer is too small to contain the path, the return value [of GetFullPathName] is the size, in TCHARs, of the buffer that is required to hold the path and the terminating null character. + //If [GetFullPathName] fails for any other reason, the return value is zero. + + NppParameters *pNppParam = NppParameters::getInstance(); + TCHAR longFileName[longFileNameBufferSize]; + + const DWORD getFullPathNameResult = ::GetFullPathName(fileName, longFileNameBufferSize, longFileName, NULL); + if ( getFullPathNameResult == 0 ) + { + return BUFFER_INVALID; + } + if ( getFullPathNameResult > longFileNameBufferSize ) + { + return BUFFER_INVALID; + } + assert( _tcslen( longFileName ) == getFullPathNameResult ); + + const DWORD getLongPathNameResult = ::GetLongPathName(longFileName, longFileName, longFileNameBufferSize); + if ( getLongPathNameResult == 0 ) + { + return BUFFER_INVALID; + } + if ( getLongPathNameResult > longFileNameBufferSize ) + { + return BUFFER_INVALID; + } + assert( _tcslen( longFileName ) == getLongPathNameResult ); bool isSnapshotMode = backupFileName != NULL && PathFileExists(backupFileName); if (isSnapshotMode && !PathFileExists(longFileName)) // UNTITLED diff --git a/PowerEditor/src/NppNotification.cpp b/PowerEditor/src/NppNotification.cpp index 2f8fea9c..c2ba62d6 100644 --- a/PowerEditor/src/NppNotification.cpp +++ b/PowerEditor/src/NppNotification.cpp @@ -190,8 +190,9 @@ BOOL Notepad_plus::notify(SCNotification *notification) fileNamesData.cbData = long(quotFileName.length() + 1)*(sizeof(TCHAR)); HWND hWinParent = ::GetParent(hWin); - TCHAR className[MAX_PATH]; - ::GetClassName(hWinParent,className, sizeof(className)); + const rsize_t classNameBufferSize = MAX_PATH; + TCHAR className[classNameBufferSize]; + ::GetClassName(hWinParent,className, classNameBufferSize); if (lstrcmp(className, _pPublicInterface->getClassName()) == 0 && hWinParent != _pPublicInterface->getHSelf()) // another Notepad++ { int index = _pDocTab->getCurrentTabIndex(); @@ -481,7 +482,7 @@ BOOL Notepad_plus::notify(SCNotification *notification) case SCN_DOUBLECLICK : { - if(notification->modifiers == SCMOD_CTRL) + if (notification->modifiers == SCMOD_CTRL) { const NppGUI & nppGUI = NppParameters::getInstance()->getNppGUI(); @@ -500,7 +501,7 @@ BOOL Notepad_plus::notify(SCNotification *notification) char *buf; int length; - if(nppGUI._delimiterSelectionOnEntireDocument) + if (nppGUI._delimiterSelectionOnEntireDocument) { // Get entire document. length = notifyView->execute(SCI_GETLENGTH); @@ -526,19 +527,18 @@ BOOL Notepad_plus::notify(SCNotification *notification) int leftmost_position = -1; int rightmost_position = -1; - if(nppGUI._rightmostDelimiter == nppGUI._leftmostDelimiter) + if (nppGUI._rightmostDelimiter == nppGUI._leftmostDelimiter) { // If the delimiters are the same (e.g. they are both a quotation mark), choose the ones // which are closest to the clicked position. - - for(unsigned int i = position_of_click; i >= 0; --i) + for (int i = position_of_click; i >= 0; --i) { - if(bufstring.at(i) == nppGUI._leftmostDelimiter) + if (bufstring.at(i) == nppGUI._leftmostDelimiter) { // Respect escaped quotation marks. - if(nppGUI._leftmostDelimiter == '"') + if (nppGUI._leftmostDelimiter == '"') { - if(! (i > 0 && bufstring.at(i - 1) == '\\')) + if (! (i > 0 && bufstring.at(i - 1) == '\\')) { leftmost_position = i; break; @@ -552,18 +552,18 @@ BOOL Notepad_plus::notify(SCNotification *notification) } } - if(leftmost_position == -1) + if (leftmost_position == -1) break; // Scan for right delimiter. - for(unsigned int i = position_of_click; i < bufstring.length(); ++i) + for (unsigned int i = position_of_click; i < bufstring.length(); ++i) { - if(bufstring.at(i) == nppGUI._rightmostDelimiter) + if (bufstring.at(i) == nppGUI._rightmostDelimiter) { // Respect escaped quotation marks. - if(nppGUI._rightmostDelimiter == '"') + if (nppGUI._rightmostDelimiter == '"') { - if(! (i > 0 && bufstring.at(i - 1) == '\\')) + if (! (i > 0 && bufstring.at(i - 1) == '\\')) { rightmost_position = i; break; @@ -588,11 +588,11 @@ BOOL Notepad_plus::notify(SCNotification *notification) std::stack leftmost_delimiter_positions; - for(unsigned int i = 0; i < bufstring.length(); ++i) + for (unsigned int i = 0; i < bufstring.length(); ++i) { - if(bufstring.at(i) == nppGUI._leftmostDelimiter) + if (bufstring.at(i) == nppGUI._leftmostDelimiter) leftmost_delimiter_positions.push(i); - else if(bufstring.at(i) == nppGUI._rightmostDelimiter && ! leftmost_delimiter_positions.empty()) + else if (bufstring.at(i) == nppGUI._rightmostDelimiter && ! leftmost_delimiter_positions.empty()) { unsigned int matching_leftmost = leftmost_delimiter_positions.top(); leftmost_delimiter_positions.pop(); @@ -602,7 +602,7 @@ BOOL Notepad_plus::notify(SCNotification *notification) // Note: cast of leftmost_position to unsigned int is safe, since if leftmost_position is not -1 then it is guaranteed to be positive. // If it was possible, leftmost_position and rightmost_position should be of type optional. - if( matching_leftmost <= position_of_click && i >= position_of_click && (leftmost_position == -1 || matching_leftmost > (unsigned int)leftmost_position) ) + if ( matching_leftmost <= position_of_click && i >= position_of_click && (leftmost_position == -1 || matching_leftmost > (unsigned int)leftmost_position) ) { leftmost_position = matching_leftmost; rightmost_position = i; @@ -612,9 +612,9 @@ BOOL Notepad_plus::notify(SCNotification *notification) } // Set selection to the position we found (if any). - if(rightmost_position != -1 && leftmost_position != -1) + if (rightmost_position != -1 && leftmost_position != -1) { - if(nppGUI._delimiterSelectionOnEntireDocument) + if (nppGUI._delimiterSelectionOnEntireDocument) { notifyView->execute(SCI_SETCURRENTPOS, rightmost_position); notifyView->execute(SCI_SETANCHOR, leftmost_position + 1); diff --git a/PowerEditor/src/Parameters.cpp b/PowerEditor/src/Parameters.cpp index 85f4c86d..2cfea798 100644 --- a/PowerEditor/src/Parameters.cpp +++ b/PowerEditor/src/Parameters.cpp @@ -926,7 +926,13 @@ generic_string NppParameters::getCloudSettingsPath(CloudChoice cloudChoice) generic_string settingsPath4dropbox = TEXT(""); ITEMIDLIST *pidl; - SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidl); + static_assert( SUCCEEDED( S_OK ), "bad HRESULT test!" ); + + const HRESULT specialFolderLocationResult_1 = SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidl); + if ( !SUCCEEDED( specialFolderLocationResult_1 ) ) + { + return cloudSettingsPath; + } TCHAR tmp[MAX_PATH]; SHGetPathFromIDList(pidl, tmp); generic_string dropboxInfoDB = tmp; @@ -1010,7 +1016,11 @@ generic_string NppParameters::getCloudSettingsPath(CloudChoice cloudChoice) // TODO: check if google drive is present // ITEMIDLIST *pidl2; - SHGetSpecialFolderLocation(NULL, CSIDL_LOCAL_APPDATA, &pidl2); + const HRESULT specialFolderLocationResult_2 = SHGetSpecialFolderLocation(NULL, CSIDL_LOCAL_APPDATA, &pidl2); + if ( !SUCCEEDED( specialFolderLocationResult_2 ) ) + { + return TEXT( "" ); + } TCHAR tmp2[MAX_PATH]; SHGetPathFromIDList(pidl2, tmp2); generic_string googleDriveInfoDB = tmp2; @@ -1118,7 +1128,12 @@ generic_string NppParameters::getSettingsFolder() else { ITEMIDLIST *pidl; - SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidl); + static_assert( SUCCEEDED( S_OK ), "Bad HRESULT code check!!" ); + const HRESULT specialLocationResult = SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidl); + if ( !SUCCEEDED( specialLocationResult ) ) + { + return TEXT( "" ); + } TCHAR tmp[MAX_PATH]; SHGetPathFromIDList(pidl, tmp); generic_string settingsFolderPath = tmp; @@ -1148,7 +1163,12 @@ bool NppParameters::load() if (_winVersion >= WV_VISTA) { ITEMIDLIST *pidl; - SHGetSpecialFolderLocation(NULL, CSIDL_PROGRAM_FILES, &pidl); + static_assert( SUCCEEDED( S_OK ), "Bad HRESULT code check!!" ); + const HRESULT specialLocationResult = SHGetSpecialFolderLocation(NULL, CSIDL_PROGRAM_FILES, &pidl); + if ( !SUCCEEDED( specialLocationResult ) ) + { + return false; + } TCHAR progPath[MAX_PATH]; SHGetPathFromIDList(pidl, progPath); TCHAR nppDirLocation[MAX_PATH]; @@ -1167,7 +1187,12 @@ bool NppParameters::load() else { ITEMIDLIST *pidl; - SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidl); + static_assert( SUCCEEDED( S_OK ), "Bad HRESULT code check!!" ); + const HRESULT specialLocationResult = SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidl); + if ( !SUCCEEDED( specialLocationResult ) ) + { + return false; + } TCHAR tmp[MAX_PATH]; SHGetPathFromIDList(pidl, tmp); _userPath = tmp; @@ -2603,6 +2628,11 @@ bool NppParameters::exportUDLToFile(int langIndex2export, generic_string fileNam bool b = false; + if ( langIndex2export >= NB_MAX_USER_LANG ) + { + return false; + } + insertUserLang2Tree(newRoot2export, _userLangArray[langIndex2export]); b = pNewXmlUserLangDoc->SaveFile(); @@ -4503,7 +4533,7 @@ void NppParameters::feedGUIParameters(TiXmlNode *node) if (path && path[0]) { lstrcpyn(_nppGUI._defaultDir, path, MAX_PATH); - ::ExpandEnvironmentStrings(_nppGUI._defaultDir, _nppGUI._defaultDirExp, 500); + ::ExpandEnvironmentStrings(_nppGUI._defaultDir, _nppGUI._defaultDirExp, MAX_PATH); } } else if (!lstrcmp(nm, TEXT("titleBar"))) diff --git a/PowerEditor/src/Parameters.h b/PowerEditor/src/Parameters.h index 5ef9a53c..d63f3183 100644 --- a/PowerEditor/src/Parameters.h +++ b/PowerEditor/src/Parameters.h @@ -663,7 +663,7 @@ public: generic_string toString() { // Return Notepad++ date format : YYYYMMDD TCHAR dateStr[8+1]; - wsprintf(dateStr, TEXT("%04d%02d%02d"), _year, _month, _day); + wsprintf(dateStr, TEXT("%04u%02u%02u"), _year, _month, _day); return dateStr; }; @@ -828,6 +828,7 @@ struct NppGUI bool _shortTitlebar; OpenSaveDirSetting _openSaveDir; + TCHAR _defaultDir[MAX_PATH]; TCHAR _defaultDirExp[MAX_PATH]; //expanded environment variables generic_string _themeName; diff --git a/PowerEditor/src/winmain.cpp b/PowerEditor/src/winmain.cpp index 2d0ece85..6362d186 100644 --- a/PowerEditor/src/winmain.cpp +++ b/PowerEditor/src/winmain.cpp @@ -36,9 +36,32 @@ typedef std::vector ParamVector; -bool checkSingleFile(const TCHAR * commandLine) { - TCHAR fullpath[MAX_PATH]; - ::GetFullPathName(commandLine, MAX_PATH, fullpath, NULL); +bool checkSingleFile( _In_z_ PCTSTR const commandLine) { + const rsize_t strLen = _tcslen( commandLine ); + if ( strLen == 0 ) { + return false; + } + const rsize_t fullpathBufSize = MAX_PATH; + TCHAR fullpath[ fullpathBufSize ] = { 0 }; + + //If [GetFullPathName] succeeds, the return value is the length, in TCHARs, of the string copied to lpBuffer, not including the terminating null character. + //If the lpBuffer buffer is too small to contain the path, the return value [of GetFullPathName] is the size, in TCHARs, of the buffer that is required to hold the path and the terminating null character. + //If [GetFullPathName] fails for any other reason, the return value is zero. To get extended error information, call GetLastError. + + const DWORD fullpathResult = ::GetFullPathName(commandLine, fullpathBufSize, fullpath, NULL); + if ( fullpathResult == 0 ) + { + MessageBoxA( NULL, "GetFullPathName failed with some unexpected error!", "checkSingleFile failed!!", MB_OK ); + MessageBox( NULL, commandLine, TEXT( "path that failed:" ), MB_OK ); + return false; + } + if ( fullpathResult > fullpathBufSize ) + { + MessageBoxA( NULL, "the buffer passed to GetFullPathName was too small!", "checkSingleFile failed!!", MB_OK ); + MessageBox( NULL, commandLine, TEXT( "path that failed:" ), MB_OK ); + return false; + } + if (::PathFileExists(fullpath)) { return true; }