Reduce stack memory usage to eliminate the compiling warning

Follow 36aeb8f681

Close #16874
This commit is contained in:
Don Ho 2025-07-27 13:47:40 +02:00
parent 36aeb8f681
commit 07a49ca0f1
4 changed files with 67 additions and 45 deletions

View File

@ -201,7 +201,7 @@ void CSHA1::Final()
Transform(m_state, m_buffer); Transform(m_state, m_buffer);
#endif #endif
} }
/*
#ifdef SHA1_UTILITY_FUNCTIONS #ifdef SHA1_UTILITY_FUNCTIONS
bool CSHA1::ReportHash(wchar_t* tszReport, REPORT_TYPE rtReportType) const bool CSHA1::ReportHash(wchar_t* tszReport, REPORT_TYPE rtReportType) const
{ {
@ -247,7 +247,7 @@ bool CSHA1::ReportHashStl(std::basic_string<wchar_t>& strOut, REPORT_TYPE rtRepo
return bResult; return bResult;
} }
#endif #endif
*/
bool CSHA1::GetHash(UINT_8* pbDest20) const bool CSHA1::GetHash(UINT_8* pbDest20) const
{ {
if(pbDest20 == NULL) return false; if(pbDest20 == NULL) return false;

View File

@ -227,7 +227,7 @@ public:
// Finalize hash; call it before using ReportHash(Stl) // Finalize hash; call it before using ReportHash(Stl)
void Final(); void Final();
/*
#ifdef SHA1_UTILITY_FUNCTIONS #ifdef SHA1_UTILITY_FUNCTIONS
bool ReportHash(wchar_t* tszReport, REPORT_TYPE rtReportType = REPORT_HEX) const; bool ReportHash(wchar_t* tszReport, REPORT_TYPE rtReportType = REPORT_HEX) const;
#endif #endif
@ -236,7 +236,7 @@ public:
bool ReportHashStl(std::basic_string<wchar_t>& strOut, REPORT_TYPE rtReportType = bool ReportHashStl(std::basic_string<wchar_t>& strOut, REPORT_TYPE rtReportType =
REPORT_HEX) const; REPORT_HEX) const;
#endif #endif
*/
// Get the raw message digest (20 bytes) // Get the raw message digest (20 bytes)
bool GetHash(UINT_8* pbDest20) const; bool GetHash(UINT_8* pbDest20) const;

View File

@ -1018,41 +1018,49 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa
case NPPM_GETCURRENTWORD: case NPPM_GETCURRENTWORD:
case NPPM_GETCURRENTLINESTR: case NPPM_GETCURRENTLINESTR:
{ {
const int strSize = CURRENTWORD_MAXLENGTH; 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)
wchar_t str[strSize] = { '\0' }; // command line length limit imposed by the ShellExecute/Ex functions."
// https://devblogs.microsoft.com/oldnewthing/20031210-00/?p=41553
auto str = std::make_unique<wchar_t[]>(strSize);
std::fill_n(str.get(), strSize, L'\0');
wchar_t *pTchar = reinterpret_cast<wchar_t *>(lParam); wchar_t *pTchar = reinterpret_cast<wchar_t *>(lParam);
if (message == NPPM_GETCURRENTWORD) if (message == NPPM_GETCURRENTWORD)
_pEditView->getGenericSelectedText(str, strSize); _pEditView->getGenericSelectedText(str.get(), strSize);
else if (message == NPPM_GETCURRENTLINESTR) 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. // 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. // 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; lstrcpyn(pTchar, str.get(), static_cast<int32_t>(wParam));
} result = TRUE;
else //buffer large enough, perform safe copy
{
lstrcpyn(pTchar, str, static_cast<int32_t>(wParam));
return TRUE;
} }
} }
lstrcpy(pTchar, str); return result;
return TRUE;
} }
case NPPM_GETFILENAMEATCURSOR: // wParam = buffer length, lParam = (wchar_t*)buffer case NPPM_GETFILENAMEATCURSOR: // wParam = buffer length, lParam = (wchar_t*)buffer
{ {
constexpr int strSize = CURRENTWORD_MAXLENGTH; constexpr int strSize = CURRENTWORD_MAXLENGTH;
wchar_t str[strSize]{}; auto str = std::make_unique<wchar_t[]>(strSize);
std::fill_n(str.get(), strSize, L'\0');
int hasSlash = 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; hasSlash = FALSE;
for (int i = 0; str[i] != 0; i++) for (int i = 0; str[i] != 0; i++)
if (CharacterIs(str[i], L"\\/")) 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 start = 0;
intptr_t end = 0; intptr_t end = 0;
const wchar_t *delimiters; const wchar_t *delimiters;
wchar_t strLine[strSize]{}; auto strLine = std::make_unique<wchar_t[]>(strSize);
std::fill_n(strLine.get(), strSize, L'\0');
size_t lineNumber = 0; size_t lineNumber = 0;
intptr_t col = 0; intptr_t col = 0;
lineNumber = _pEditView->getCurrentLineNumber(); lineNumber = _pEditView->getCurrentLineNumber();
col = _pEditView->execute(SCI_GETCURRENTPOS) - _pEditView->execute(SCI_POSITIONFROMLINE, lineNumber); col = _pEditView->execute(SCI_GETCURRENTPOS) - _pEditView->execute(SCI_POSITIONFROMLINE, lineNumber);
_pEditView->getLine(lineNumber, strLine, strSize); _pEditView->getLine(lineNumber, strLine.get(), strSize);
// find the start // find the start
start = col; start = col;
@ -1085,17 +1095,17 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa
delimiters = L" \t:()[]<>\"\r\n"; delimiters = L" \t:()[]<>\"\r\n";
while ((strLine[end] != 0) && (CharacterIs(strLine[end], delimiters) == FALSE)) end++; while ((strLine[end] != 0) && (CharacterIs(strLine[end], delimiters) == FALSE)) end++;
lstrcpyn(str, &strLine[start], static_cast<int>(end - start + 1)); lstrcpyn(str.get(), &strLine[start], static_cast<int>(end - start + 1));
} }
if (lstrlen(str) >= int(wParam)) //buffer too small if (lstrlen(str.get()) >= int(wParam)) //buffer too small
{ {
return FALSE; return FALSE;
} }
else //buffer large enough, perform safe copy else //buffer large enough, perform safe copy
{ {
wchar_t* pTchar = reinterpret_cast<wchar_t*>(lParam); wchar_t* pTchar = reinterpret_cast<wchar_t*>(lParam);
lstrcpyn(pTchar, str, static_cast<int32_t>(wParam)); lstrcpyn(pTchar, str.get(), static_cast<int32_t>(wParam));
return TRUE; return TRUE;
} }
} }

View File

@ -146,10 +146,15 @@ void Notepad_plus::command(int id)
case IDM_FILE_CONTAININGFOLDERASWORKSPACE: case IDM_FILE_CONTAININGFOLDERASWORKSPACE:
{ {
wchar_t currentFile[CURRENTWORD_MAXLENGTH] = { '\0' }; const int strSize = CURRENTWORD_MAXLENGTH;
wchar_t currentDir[CURRENTWORD_MAXLENGTH] = { '\0' }; auto currentFile = std::make_unique<wchar_t[]>(strSize);
::SendMessage(_pPublicInterface->getHSelf(), NPPM_GETFULLCURRENTPATH, CURRENTWORD_MAXLENGTH, reinterpret_cast<LPARAM>(currentFile)); std::fill_n(currentFile.get(), strSize, L'\0');
::SendMessage(_pPublicInterface->getHSelf(), NPPM_GETCURRENTDIRECTORY, CURRENTWORD_MAXLENGTH, reinterpret_cast<LPARAM>(currentDir));
auto currentDir = std::make_unique<wchar_t[]>(strSize);
std::fill_n(currentDir.get(), strSize, L'\0');
::SendMessage(_pPublicInterface->getHSelf(), NPPM_GETFULLCURRENTPATH, CURRENTWORD_MAXLENGTH, reinterpret_cast<LPARAM>(currentFile.get()));
::SendMessage(_pPublicInterface->getHSelf(), NPPM_GETCURRENTDIRECTORY, CURRENTWORD_MAXLENGTH, reinterpret_cast<LPARAM>(currentDir.get()));
if (!_pFileBrowser) if (!_pFileBrowser)
{ {
@ -157,9 +162,9 @@ void Notepad_plus::command(int id)
} }
vector<wstring> folders; vector<wstring> folders;
folders.push_back(currentDir); folders.push_back(currentDir.get());
launchFileBrowser(folders, currentFile); launchFileBrowser(folders, currentFile.get());
} }
break; break;
@ -430,8 +435,11 @@ void Notepad_plus::command(int id)
if (!textLen) if (!textLen)
return; return;
char *pBinText = new char[textLen + 1]; const size_t strSize = textLen + 1;
_pEditView->getSelectedText(pBinText, textLen + 1); auto pBinText = std::make_unique<char[]>(strSize);
std::fill_n(pBinText.get(), strSize, '\0');
_pEditView->getSelectedText(pBinText.get(), textLen + 1);
// Open the clipboard and empty it. // Open the clipboard and empty it.
if (!::OpenClipboard(NULL)) if (!::OpenClipboard(NULL))
@ -458,11 +466,9 @@ void Notepad_plus::command(int id)
::CloseClipboard(); ::CloseClipboard();
return; return;
} }
memcpy(lpucharCopy, pBinText, textLen * sizeof(unsigned char)); memcpy(lpucharCopy, pBinText.get(), textLen * sizeof(unsigned char));
lpucharCopy[textLen] = 0; // null character lpucharCopy[textLen] = 0; // null character
delete[] pBinText;
::GlobalUnlock(hglbCopy); ::GlobalUnlock(hglbCopy);
// Place the handle on the clipboard. // Place the handle on the clipboard.
@ -581,8 +587,12 @@ void Notepad_plus::command(int id)
return; return;
HWND hwnd = _pPublicInterface->getHSelf(); HWND hwnd = _pPublicInterface->getHSelf();
wchar_t currentWord[CURRENTWORD_MAXLENGTH] = { '\0' };
::SendMessage(hwnd, NPPM_GETFILENAMEATCURSOR, CURRENTWORD_MAXLENGTH, reinterpret_cast<LPARAM>(currentWord)); const int strSize = CURRENTWORD_MAXLENGTH;
auto currentWord = std::make_unique<wchar_t[]>(strSize);
std::fill_n(currentWord.get(), strSize, L'\0');
::SendMessage(hwnd, NPPM_GETFILENAMEATCURSOR, CURRENTWORD_MAXLENGTH, reinterpret_cast<LPARAM>(currentWord.get()));
wchar_t cmd2Exec[CURRENTWORD_MAXLENGTH] = { '\0' }; wchar_t cmd2Exec[CURRENTWORD_MAXLENGTH] = { '\0' };
if (id == IDM_EDIT_OPENINFOLDER) 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 // 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""; wstring fullFilePath = id == IDM_EDIT_OPENINFOLDER ? L"/select," : L"";
fullFilePath += L"\""; fullFilePath += L"\"";
fullFilePath += currentWord; fullFilePath += currentWord.get();
fullFilePath += L"\""; fullFilePath += L"\"";
if (id == IDM_EDIT_OPENINFOLDER || 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); ::ShellExecute(hwnd, L"open", cmd2Exec, fullFilePath.c_str(), L".", SW_SHOW);
} }
else // Relative file path - need concatenate with current full file path else // Relative file path - need concatenate with current full file path
{ {
wchar_t currentDir[CURRENTWORD_MAXLENGTH] = { '\0' }; auto currentDir = std::make_unique<wchar_t[]>(strSize);
::SendMessage(hwnd, NPPM_GETCURRENTDIRECTORY, CURRENTWORD_MAXLENGTH, reinterpret_cast<LPARAM>(currentDir)); std::fill_n(currentDir.get(), strSize, L'\0');
::SendMessage(hwnd, NPPM_GETCURRENTDIRECTORY, CURRENTWORD_MAXLENGTH, reinterpret_cast<LPARAM>(currentDir.get()));
wstring fullFilePath = id == IDM_EDIT_OPENINFOLDER ? L"/select," : L""; wstring fullFilePath = id == IDM_EDIT_OPENINFOLDER ? L"/select," : L"";
fullFilePath += L"\""; fullFilePath += L"\"";
fullFilePath += currentDir; fullFilePath += currentDir.get();
fullFilePath += L"\\"; fullFilePath += L"\\";
fullFilePath += currentWord; fullFilePath += currentWord.get();
if ((id == IDM_EDIT_OPENASFILE && if ((id == IDM_EDIT_OPENASFILE &&
(!doesFileExist(fullFilePath.c_str() + 1)))) // + 1 for skipping the 1st char '"' (!doesFileExist(fullFilePath.c_str() + 1)))) // + 1 for skipping the 1st char '"'