diff --git a/PowerEditor/src/MISC/sha1/sha1.cpp b/PowerEditor/src/MISC/sha1/sha1.cpp index 1c6a9e119..7b4afb3f4 100644 --- a/PowerEditor/src/MISC/sha1/sha1.cpp +++ b/PowerEditor/src/MISC/sha1/sha1.cpp @@ -201,7 +201,7 @@ void CSHA1::Final() Transform(m_state, m_buffer); #endif } - +/* #ifdef SHA1_UTILITY_FUNCTIONS bool CSHA1::ReportHash(wchar_t* tszReport, REPORT_TYPE rtReportType) const { @@ -247,7 +247,7 @@ bool CSHA1::ReportHashStl(std::basic_string& strOut, REPORT_TYPE rtRepo return bResult; } #endif - +*/ bool CSHA1::GetHash(UINT_8* pbDest20) const { if(pbDest20 == NULL) return false; diff --git a/PowerEditor/src/MISC/sha1/sha1.h b/PowerEditor/src/MISC/sha1/sha1.h index 14820f3f6..a1b73fbe8 100644 --- a/PowerEditor/src/MISC/sha1/sha1.h +++ b/PowerEditor/src/MISC/sha1/sha1.h @@ -227,7 +227,7 @@ public: // Finalize hash; call it before using ReportHash(Stl) void Final(); - +/* #ifdef SHA1_UTILITY_FUNCTIONS bool ReportHash(wchar_t* tszReport, REPORT_TYPE rtReportType = REPORT_HEX) const; #endif @@ -236,7 +236,7 @@ public: bool ReportHashStl(std::basic_string& strOut, REPORT_TYPE rtReportType = REPORT_HEX) const; #endif - +*/ // Get the raw message digest (20 bytes) bool GetHash(UINT_8* pbDest20) const; diff --git a/PowerEditor/src/NppBigSwitch.cpp b/PowerEditor/src/NppBigSwitch.cpp index 4d2d77e2d..f5614ee89 100644 --- a/PowerEditor/src/NppBigSwitch.cpp +++ b/PowerEditor/src/NppBigSwitch.cpp @@ -1018,41 +1018,49 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa case NPPM_GETCURRENTWORD: case NPPM_GETCURRENTLINESTR: { - const int strSize = CURRENTWORD_MAXLENGTH; - wchar_t str[strSize] = { '\0' }; + const int strSize = CURRENTWORD_MAXLENGTH; // "If you are using the ShellExecute/Ex function, then you become subject to the INTERNET_MAX_URL_LENGTH (around 2048) + // command line length limit imposed by the ShellExecute/Ex functions." + // https://devblogs.microsoft.com/oldnewthing/20031210-00/?p=41553 + + auto str = std::make_unique(strSize); + std::fill_n(str.get(), strSize, L'\0'); + wchar_t *pTchar = reinterpret_cast(lParam); if (message == NPPM_GETCURRENTWORD) - _pEditView->getGenericSelectedText(str, strSize); + _pEditView->getGenericSelectedText(str.get(), strSize); else if (message == NPPM_GETCURRENTLINESTR) - _pEditView->getLine(_pEditView->getCurrentLineNumber(), str, strSize); + _pEditView->getLine(_pEditView->getCurrentLineNumber(), str.get(), strSize); // For the compatibility reason, if wParam is 0, then we assume the size of wstring buffer (lParam) is large enough. // otherwise we check if the wstring buffer size is enough for the wstring to copy. - if (wParam != 0) + BOOL result = FALSE; + if (wParam == 0) { - if (lstrlen(str) >= int(wParam)) //buffer too small + lstrcpy(pTchar, str.get()); // compatibility mode - don't check the size + result = TRUE; + } + else + { + if (lstrlen(str.get()) < int(wParam)) // buffer large enough, perform safe copy { - return FALSE; - } - else //buffer large enough, perform safe copy - { - lstrcpyn(pTchar, str, static_cast(wParam)); - return TRUE; + lstrcpyn(pTchar, str.get(), static_cast(wParam)); + result = TRUE; } } - lstrcpy(pTchar, str); - return TRUE; + return result; } case NPPM_GETFILENAMEATCURSOR: // wParam = buffer length, lParam = (wchar_t*)buffer { constexpr int strSize = CURRENTWORD_MAXLENGTH; - wchar_t str[strSize]{}; + auto str = std::make_unique(strSize); + std::fill_n(str.get(), strSize, L'\0'); + int hasSlash = 0; - _pEditView->getGenericSelectedText(str, strSize); // this is either the selected text, or the word under the cursor if there is no selection + _pEditView->getGenericSelectedText(str.get(), strSize); // this is either the selected text, or the word under the cursor if there is no selection hasSlash = FALSE; for (int i = 0; str[i] != 0; i++) if (CharacterIs(str[i], L"\\/")) @@ -1064,13 +1072,15 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa intptr_t start = 0; intptr_t end = 0; const wchar_t *delimiters; - wchar_t strLine[strSize]{}; + auto strLine = std::make_unique(strSize); + std::fill_n(strLine.get(), strSize, L'\0'); + size_t lineNumber = 0; intptr_t col = 0; lineNumber = _pEditView->getCurrentLineNumber(); col = _pEditView->execute(SCI_GETCURRENTPOS) - _pEditView->execute(SCI_POSITIONFROMLINE, lineNumber); - _pEditView->getLine(lineNumber, strLine, strSize); + _pEditView->getLine(lineNumber, strLine.get(), strSize); // find the start start = col; @@ -1085,17 +1095,17 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa delimiters = L" \t:()[]<>\"\r\n"; while ((strLine[end] != 0) && (CharacterIs(strLine[end], delimiters) == FALSE)) end++; - lstrcpyn(str, &strLine[start], static_cast(end - start + 1)); + lstrcpyn(str.get(), &strLine[start], static_cast(end - start + 1)); } - if (lstrlen(str) >= int(wParam)) //buffer too small + if (lstrlen(str.get()) >= int(wParam)) //buffer too small { return FALSE; } else //buffer large enough, perform safe copy { wchar_t* pTchar = reinterpret_cast(lParam); - lstrcpyn(pTchar, str, static_cast(wParam)); + lstrcpyn(pTchar, str.get(), static_cast(wParam)); return TRUE; } } diff --git a/PowerEditor/src/NppCommands.cpp b/PowerEditor/src/NppCommands.cpp index 1897913c0..f91eb16e7 100644 --- a/PowerEditor/src/NppCommands.cpp +++ b/PowerEditor/src/NppCommands.cpp @@ -146,10 +146,15 @@ void Notepad_plus::command(int id) case IDM_FILE_CONTAININGFOLDERASWORKSPACE: { - wchar_t currentFile[CURRENTWORD_MAXLENGTH] = { '\0' }; - wchar_t currentDir[CURRENTWORD_MAXLENGTH] = { '\0' }; - ::SendMessage(_pPublicInterface->getHSelf(), NPPM_GETFULLCURRENTPATH, CURRENTWORD_MAXLENGTH, reinterpret_cast(currentFile)); - ::SendMessage(_pPublicInterface->getHSelf(), NPPM_GETCURRENTDIRECTORY, CURRENTWORD_MAXLENGTH, reinterpret_cast(currentDir)); + const int strSize = CURRENTWORD_MAXLENGTH; + auto currentFile = std::make_unique(strSize); + std::fill_n(currentFile.get(), strSize, L'\0'); + + auto currentDir = std::make_unique(strSize); + std::fill_n(currentDir.get(), strSize, L'\0'); + + ::SendMessage(_pPublicInterface->getHSelf(), NPPM_GETFULLCURRENTPATH, CURRENTWORD_MAXLENGTH, reinterpret_cast(currentFile.get())); + ::SendMessage(_pPublicInterface->getHSelf(), NPPM_GETCURRENTDIRECTORY, CURRENTWORD_MAXLENGTH, reinterpret_cast(currentDir.get())); if (!_pFileBrowser) { @@ -157,9 +162,9 @@ void Notepad_plus::command(int id) } vector folders; - folders.push_back(currentDir); + folders.push_back(currentDir.get()); - launchFileBrowser(folders, currentFile); + launchFileBrowser(folders, currentFile.get()); } break; @@ -430,8 +435,11 @@ void Notepad_plus::command(int id) if (!textLen) return; - char *pBinText = new char[textLen + 1]; - _pEditView->getSelectedText(pBinText, textLen + 1); + const size_t strSize = textLen + 1; + auto pBinText = std::make_unique(strSize); + std::fill_n(pBinText.get(), strSize, '\0'); + + _pEditView->getSelectedText(pBinText.get(), textLen + 1); // Open the clipboard and empty it. if (!::OpenClipboard(NULL)) @@ -458,11 +466,9 @@ void Notepad_plus::command(int id) ::CloseClipboard(); return; } - memcpy(lpucharCopy, pBinText, textLen * sizeof(unsigned char)); + memcpy(lpucharCopy, pBinText.get(), textLen * sizeof(unsigned char)); lpucharCopy[textLen] = 0; // null character - delete[] pBinText; - ::GlobalUnlock(hglbCopy); // Place the handle on the clipboard. @@ -581,8 +587,12 @@ void Notepad_plus::command(int id) return; HWND hwnd = _pPublicInterface->getHSelf(); - wchar_t currentWord[CURRENTWORD_MAXLENGTH] = { '\0' }; - ::SendMessage(hwnd, NPPM_GETFILENAMEATCURSOR, CURRENTWORD_MAXLENGTH, reinterpret_cast(currentWord)); + + const int strSize = CURRENTWORD_MAXLENGTH; + auto currentWord = std::make_unique(strSize); + std::fill_n(currentWord.get(), strSize, L'\0'); + + ::SendMessage(hwnd, NPPM_GETFILENAMEATCURSOR, CURRENTWORD_MAXLENGTH, reinterpret_cast(currentWord.get())); wchar_t cmd2Exec[CURRENTWORD_MAXLENGTH] = { '\0' }; if (id == IDM_EDIT_OPENINFOLDER) @@ -595,27 +605,29 @@ void Notepad_plus::command(int id) } // Full file path: could be a folder or a file - if (doesPathExist(currentWord)) + if (doesPathExist(currentWord.get())) { wstring fullFilePath = id == IDM_EDIT_OPENINFOLDER ? L"/select," : L""; fullFilePath += L"\""; - fullFilePath += currentWord; + fullFilePath += currentWord.get(); fullFilePath += L"\""; if (id == IDM_EDIT_OPENINFOLDER || - (id == IDM_EDIT_OPENASFILE && !doesDirectoryExist(currentWord))) + (id == IDM_EDIT_OPENASFILE && !doesDirectoryExist(currentWord.get()))) ::ShellExecute(hwnd, L"open", cmd2Exec, fullFilePath.c_str(), L".", SW_SHOW); } else // Relative file path - need concatenate with current full file path { - wchar_t currentDir[CURRENTWORD_MAXLENGTH] = { '\0' }; - ::SendMessage(hwnd, NPPM_GETCURRENTDIRECTORY, CURRENTWORD_MAXLENGTH, reinterpret_cast(currentDir)); + auto currentDir = std::make_unique(strSize); + std::fill_n(currentDir.get(), strSize, L'\0'); + + ::SendMessage(hwnd, NPPM_GETCURRENTDIRECTORY, CURRENTWORD_MAXLENGTH, reinterpret_cast(currentDir.get())); wstring fullFilePath = id == IDM_EDIT_OPENINFOLDER ? L"/select," : L""; fullFilePath += L"\""; - fullFilePath += currentDir; + fullFilePath += currentDir.get(); fullFilePath += L"\\"; - fullFilePath += currentWord; + fullFilePath += currentWord.get(); if ((id == IDM_EDIT_OPENASFILE && (!doesFileExist(fullFilePath.c_str() + 1)))) // + 1 for skipping the 1st char '"'