Skip to content

Commit ea572ce

Browse files
committed
Code enhancement: Make returned value safer
A crash regression (Fixed by notepad-plus-plus@cf0a5c8) has been caused by the returned null pointer. This commit makes the returned value safer: use empty string instead. Close notepad-plus-plus#17212
1 parent 1f3b2eb commit ea572ce

File tree

7 files changed

+24
-30
lines changed

7 files changed

+24
-30
lines changed

PowerEditor/src/NppBigSwitch.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -971,8 +971,7 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa
971971
if (message == NPPM_GETCURRENTWORD)
972972
{
973973
auto txtW = _pEditView->getSelectedTextToWChar();
974-
if (txtW)
975-
wcscpy_s(str.get(), strSize, txtW);
974+
wcscpy_s(str.get(), strSize, txtW.c_str());
976975
}
977976
else if (message == NPPM_GETCURRENTLINESTR)
978977
{
@@ -1008,8 +1007,7 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa
10081007
int hasSlash = 0;
10091008

10101009
auto txtW = _pEditView->getSelectedTextToWChar(); // this is either the selected text, or the word under the cursor if there is no selection
1011-
if (txtW)
1012-
wcscpy_s(str.get(), strSize, txtW);
1010+
wcscpy_s(str.get(), strSize, txtW.c_str());
10131011

10141012
hasSlash = FALSE;
10151013
for (int i = 0; str[i] != 0; i++)

PowerEditor/src/NppCommands.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,8 +1394,8 @@ void Notepad_plus::command(int id)
13941394
}
13951395

13961396
auto str = _pEditView->getSelectedTextToWChar(false);
1397-
if (str) // the selected text is not empty, then use it
1398-
_incrementFindDlg.setSearchText(str, _pEditView->getCurrentBuffer()->getUnicodeMode() != uni8Bit);
1397+
if (!str.empty()) // the selected text is not empty, then use it
1398+
_incrementFindDlg.setSearchText(str.c_str(), _pEditView->getCurrentBuffer()->getUnicodeMode() != uni8Bit);
13991399

14001400
_incrementFindDlg.display();
14011401
}
@@ -1497,7 +1497,7 @@ void Notepad_plus::command(int id)
14971497
case IDM_SEARCH_VOLATILE_FINDPREV :
14981498
{
14991499
auto str = _pEditView->getSelectedTextToWChar();
1500-
if (!str) return;
1500+
if (str.empty()) return;
15011501

15021502
FindOption op;
15031503
op._isMatchCase = false;
@@ -1507,7 +1507,7 @@ void Notepad_plus::command(int id)
15071507
op._whichDirection = (id == IDM_SEARCH_VOLATILE_FINDNEXT ? DIR_DOWN : DIR_UP);
15081508

15091509
FindStatus status = FSNoMessage;
1510-
_findReplaceDlg.processFindNext(str, &op, &status);
1510+
_findReplaceDlg.processFindNext(str.c_str(), &op, &status);
15111511
if (status == FSEndReached)
15121512
{
15131513
wstring msg = _nativeLangSpeaker.getLocalizedStrFromID("find-status-end-reached", FIND_STATUS_END_REACHED_TEXT);
@@ -1541,9 +1541,9 @@ void Notepad_plus::command(int id)
15411541

15421542
auto selectedText = _pEditView->getSelectedTextToWChar(true);
15431543

1544-
if (selectedText)
1544+
if (!selectedText.empty())
15451545
{
1546-
_findReplaceDlg.markAll(selectedText, styleID);
1546+
_findReplaceDlg.markAll(selectedText.c_str(), styleID);
15471547
}
15481548
}
15491549
break;
@@ -3510,8 +3510,8 @@ void Notepad_plus::command(int id)
35103510
{
35113511
int iQuote = -1;
35123512
auto authorW = _pEditView->getSelectedTextToWChar();
3513-
if (authorW)
3514-
iQuote = getQuoteIndexFrom(authorW);
3513+
if (!authorW.empty())
3514+
iQuote = getQuoteIndexFrom(authorW.c_str());
35153515

35163516
if (iQuote == -1)
35173517
{

PowerEditor/src/NppNotification.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,8 @@ BOOL Notepad_plus::notify(SCNotification *notification)
421421
if (nppGui._smartHiliteOnAnotherView)
422422
{
423423
auto selectedText = _pEditView->getSelectedTextToWChar(false);
424-
if (selectedText)
425-
_smartHighlighter.highlightViewWithWord(notifyView, selectedText);
424+
if (!selectedText.empty())
425+
_smartHighlighter.highlightViewWithWord(notifyView, selectedText.c_str());
426426
}
427427
break;
428428
}

PowerEditor/src/ScintillaComponent/FindReplaceDlg.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5382,11 +5382,11 @@ wstring FindReplaceDlg::setSearchText()
53825382
{
53835383
const NppGUI& nppGui = NppParameters::getInstance().getNppGUI();
53845384
Sci_Position selStrCharNum = 0;
5385-
const wchar_t* selStr = (*_ppEditView)->getSelectedTextToWChar(true, &selStrCharNum);
5385+
auto selStr = (*_ppEditView)->getSelectedTextToWChar(true, &selStrCharNum);
53865386

5387-
if (selStr && selStrCharNum <= nppGui._fillFindWhatThreshold)
5387+
if (!selStr.empty() && selStrCharNum <= nppGui._fillFindWhatThreshold)
53885388
{
5389-
setSearchText(selStr);
5389+
setSearchText(selStr.c_str());
53905390
return selStr;
53915391
}
53925392
return L"";
@@ -5401,11 +5401,11 @@ wstring FindReplaceDlg::setSearchTextWithSettings()
54015401
if (nppGui._fillFindFieldWithSelected)
54025402
{
54035403
Sci_Position selStrCharNum = 0;
5404-
const wchar_t* selStr = (*_ppEditView)->getSelectedTextToWChar(nppGui._fillFindFieldSelectCaret, &selStrCharNum);
5404+
auto selStr = (*_ppEditView)->getSelectedTextToWChar(nppGui._fillFindFieldSelectCaret, &selStrCharNum);
54055405

5406-
if (selStr && selStrCharNum <= nppGui._fillFindWhatThreshold)
5406+
if (!selStr.empty() && selStrCharNum <= nppGui._fillFindWhatThreshold)
54075407
{
5408-
setSearchText(selStr);
5408+
setSearchText(selStr.c_str());
54095409
return selStr;
54105410
}
54115411
}
@@ -6322,7 +6322,7 @@ void FindIncrementDlg::markSelectedTextInc(bool enable, FindOption *opt)
63226322
return;
63236323

63246324
auto text2Find = (*(_pFRDlg->_ppEditView))->getSelectedTextToWChar(false); //do not expand selection (false)
6325-
if (!text2Find)
6325+
if (text2Find.empty())
63266326
return;
63276327

63286328
opt->_str2Search = text2Find;

PowerEditor/src/ScintillaComponent/ScintillaEditView.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,8 +2834,7 @@ char * ScintillaEditView::getSelectedTextToMultiChar(char * txt, size_t size, bo
28342834
}
28352835

28362836
// get the selected text & selected text character number (not the multi-chars lenghth for the allocation, if selCharNumber is not nul).
2837-
// This function returns the pointer of wide char string (wchar_t *) that we don't need to and should not deallocate.
2838-
const wchar_t * ScintillaEditView::getSelectedTextToWChar(bool expand, Sci_Position* selCharNumber)
2837+
wstring ScintillaEditView::getSelectedTextToWChar(bool expand, Sci_Position* selCharNumber)
28392838
{
28402839
WcharMbcsConvertor& wmc = WcharMbcsConvertor::getInstance();
28412840
size_t cp = execute(SCI_GETCODEPAGE);
@@ -2855,7 +2854,7 @@ const wchar_t * ScintillaEditView::getSelectedTextToWChar(bool expand, Sci_Posit
28552854
*selCharNumber = selNum;
28562855

28572856
if (selNum == 0)
2858-
return nullptr;
2857+
return L"";
28592858

28602859
// then get the selected string's total bytes (without counting the last NULL char)
28612860
auto neededByte = execute(SCI_GETSELTEXT, 0, 0);

PowerEditor/src/ScintillaComponent/ScintillaEditView.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ friend class Finder;
463463
void getVisibleStartAndEndPosition(intptr_t* startPos, intptr_t* endPos);
464464
char * getWordFromRange(char * txt, size_t size, size_t pos1, size_t pos2);
465465
char * getSelectedTextToMultiChar(char * txt, size_t size, bool expand = true);
466-
const wchar_t* getSelectedTextToWChar(bool expand = true, Sci_Position* selCharNumber = nullptr);
466+
std::wstring getSelectedTextToWChar(bool expand = true, Sci_Position* selCharNumber = nullptr);
467467
char * getWordOnCaretPos(char * txt, size_t size);
468468

469469
intptr_t searchInTarget(const wchar_t * Text2Find, size_t lenOfText2Find, size_t fromPos, size_t toPos) const;

PowerEditor/src/ScintillaComponent/SmartHighlighter.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,8 @@ void SmartHighlighter::highlightView(ScintillaEditView * pHighlightView, Scintil
151151
return;
152152
}
153153

154-
const wchar_t * text2FindW = pHighlightView->getSelectedTextToWChar(false); //do not expand selection (false)
155-
if (!text2FindW)
156-
{
157-
return;
158-
}
154+
auto text2FindW = pHighlightView->getSelectedTextToWChar(false); //do not expand selection (false)
155+
if (text2FindW.empty()) return;
159156

160157
highlightViewWithWord(pHighlightView, text2FindW);
161158

0 commit comments

Comments
 (0)