Code enhancement: Nullpointer checks

- zero init LexSearchResult.cxx lineBuffer
- WindowsDlg::resetSelection() add nullpointer check as assert just triggers on debug builds
- added missing nullpointer checks in Utf8_16.cpp on allocated memory
- added missing nullpointer check on notifyView for case SCN_AUTOCSELECTION in NppNotification.cpp
- added missing nullpointer check on clipboardData in NppCommands.cpp

Close #15195
pull/15224/head
Christian Grasser 2024-05-26 12:45:12 +02:00 committed by Don Ho
parent c89033bd8a
commit b4b76ff770
5 changed files with 106 additions and 83 deletions

View File

@ -3122,16 +3122,20 @@ void Notepad_plus::command(int id)
// Save the current clipboard content // Save the current clipboard content
::OpenClipboard(_pPublicInterface->getHSelf()); ::OpenClipboard(_pPublicInterface->getHSelf());
HANDLE clipboardData = ::GetClipboardData(CF_TEXT); HANDLE clipboardData = ::GetClipboardData(CF_TEXT);
int len = static_cast<int32_t>(::GlobalSize(clipboardData)); LPVOID clipboardData2 = NULL;
LPVOID clipboardDataPtr = ::GlobalLock(clipboardData); if (clipboardData != NULL)
{
int len = static_cast<int32_t>(::GlobalSize(clipboardData));
LPVOID clipboardDataPtr = ::GlobalLock(clipboardData);
HANDLE allocClipboardData = ::GlobalAlloc(GMEM_MOVEABLE, len); HANDLE allocClipboardData = ::GlobalAlloc(GMEM_MOVEABLE, len);
LPVOID clipboardData2 = ::GlobalLock(allocClipboardData); clipboardData2 = ::GlobalLock(allocClipboardData);
::memcpy(clipboardData2, clipboardDataPtr, len); ::memcpy(clipboardData2, clipboardDataPtr, len);
::GlobalUnlock(clipboardData); ::GlobalUnlock(clipboardData);
::GlobalUnlock(allocClipboardData); ::GlobalUnlock(allocClipboardData);
::CloseClipboard(); ::CloseClipboard();
}
_pEditView->saveCurrentPos(); _pEditView->saveCurrentPos();

View File

@ -813,7 +813,7 @@ BOOL Notepad_plus::notify(SCNotification *notification)
} }
else else
{ {
// Find matching pairs of delimiters (e.g. parantheses). // Find matching pairs of delimiters (e.g. parentheses).
// The pair where the distance from the left delimiter to position_of_click is at a minimum is the one we're looking for. // The pair where the distance from the left delimiter to position_of_click is at a minimum is the one we're looking for.
// Of course position_of_click must lie between the delimiters. // Of course position_of_click must lie between the delimiters.
@ -863,7 +863,7 @@ BOOL Notepad_plus::notify(SCNotification *notification)
} }
else else
{ // Double click with no modifiers { // Double click with no modifiers
// Check wether cursor is within URL // Check whether cursor is within URL
auto indicMsk = notifyView->execute(SCI_INDICATORALLONFOR, notification->position); auto indicMsk = notifyView->execute(SCI_INDICATORALLONFOR, notification->position);
if (!(indicMsk & (1 << URL_INDIC))) if (!(indicMsk & (1 << URL_INDIC)))
break; break;
@ -1103,6 +1103,9 @@ BOOL Notepad_plus::notify(SCNotification *notification)
case SCN_AUTOCSELECTION: case SCN_AUTOCSELECTION:
{ {
if (!notifyView)
return FALSE;
const NppGUI& nppGui = NppParameters::getInstance().getNppGUI(); const NppGUI& nppGui = NppParameters::getInstance().getNppGUI();
// if autocompletion is disabled and it is triggered manually, then both ENTER & TAB will insert the selection // if autocompletion is disabled and it is triggered manually, then both ENTER & TAB will insert the selection

View File

@ -170,22 +170,29 @@ size_t Utf8_16_Read::convert(char* buf, size_t len)
if (m_pNewBuf) if (m_pNewBuf)
delete [] m_pNewBuf; delete [] m_pNewBuf;
m_pNewBuf = NULL; m_pNewBuf = NULL;
m_nAllocatedBufSize = 0;
m_pNewBuf = new ubyte[newSize]; m_pNewBuf = new ubyte[newSize];
m_nAllocatedBufSize = newSize; if (m_pNewBuf)
{
m_nAllocatedBufSize = newSize;
}
} }
ubyte* pCur = m_pNewBuf;
m_Iter16.set(m_pBuf + nSkip, len - nSkip, m_eEncoding);
while (m_Iter16) if (m_nAllocatedBufSize)
{ {
++m_Iter16; ubyte* pCur = m_pNewBuf;
utf8 c;
while (m_Iter16.get(&c)) m_Iter16.set(m_pBuf + nSkip, len - nSkip, m_eEncoding);
*pCur++ = c;
while (m_Iter16)
{
++m_Iter16;
utf8 c;
while (m_Iter16.get(&c))
*pCur++ = c;
}
m_nNewBufSize = pCur - m_pNewBuf;
} }
m_nNewBufSize = pCur - m_pNewBuf;
} }
break; break;
@ -407,71 +414,79 @@ bool Utf8_16_Write::writeFile(const void* p, size_t _size)
size_t Utf8_16_Write::convert(char* p, size_t _size) size_t Utf8_16_Write::convert(char* p, size_t _size)
{ {
if (m_pNewBuf) if (m_pNewBuf)
{ {
delete [] m_pNewBuf; delete [] m_pNewBuf;
m_pNewBuf = NULL; m_pNewBuf = NULL;
m_nBufSize = 0;
} }
switch (m_eEncoding) try
{ {
switch (m_eEncoding)
{
case uni7Bit: case uni7Bit:
case uni8Bit: case uni8Bit:
case uniCookie: case uniCookie:
{ {
// Normal write // Normal write
m_nBufSize = _size; m_pNewBuf = new ubyte[_size];
m_pNewBuf = (ubyte*)new ubyte[m_nBufSize]; m_nBufSize = _size;
memcpy(m_pNewBuf, p, _size); memcpy(m_pNewBuf, p, _size);
} }
break; break;
case uniUTF8: case uniUTF8:
{ {
m_nBufSize = _size + 3; m_pNewBuf = new ubyte[_size + 3];
m_pNewBuf = (ubyte*)new ubyte[m_nBufSize]; m_nBufSize = _size + 3;
memcpy(m_pNewBuf, k_Boms[m_eEncoding], 3); memcpy(m_pNewBuf, k_Boms[m_eEncoding], 3);
memcpy(&m_pNewBuf[3], p, _size); memcpy(&m_pNewBuf[3], p, _size);
} }
break; break;
case uni16BE_NoBOM: case uni16BE_NoBOM:
case uni16LE_NoBOM: case uni16LE_NoBOM:
case uni16BE: case uni16BE:
case uni16LE: case uni16LE:
{ {
utf16* pCur = NULL; utf16* pCur = NULL;
if (m_eEncoding == uni16BE || m_eEncoding == uni16LE) if (m_eEncoding == uni16BE || m_eEncoding == uni16LE)
{ {
// Write the BOM // Write the BOM
m_pNewBuf = (ubyte*)new ubyte[sizeof(utf16) * (_size + 1)]; m_pNewBuf = new ubyte[sizeof(utf16) * (_size + 1)];
memcpy(m_pNewBuf, k_Boms[m_eEncoding], 2); memcpy(m_pNewBuf, k_Boms[m_eEncoding], 2);
pCur = (utf16*)&m_pNewBuf[2]; pCur = (utf16*)&m_pNewBuf[2];
} }
else else
{ {
m_pNewBuf = (ubyte*)new ubyte[sizeof(utf16) * _size]; m_pNewBuf = new ubyte[sizeof(utf16) * _size];
pCur = (utf16*)m_pNewBuf; pCur = (utf16*)m_pNewBuf;
} }
Utf8_Iter iter8; Utf8_Iter iter8;
iter8.set(reinterpret_cast<const ubyte*>(p), _size, m_eEncoding); iter8.set(reinterpret_cast<const ubyte*>(p), _size, m_eEncoding);
for (; iter8; ++iter8) for (; iter8; ++iter8)
{ {
if (iter8.canGet()) if (iter8.canGet())
{ {
iter8.get(pCur++); iter8.get(pCur++);
} }
} }
m_nBufSize = (const char*)pCur - (const char*)m_pNewBuf; m_nBufSize = (const char*)pCur - (const char*)m_pNewBuf;
} }
break; break;
default: default:
break; break;
} }
}
catch (const std::bad_alloc&)
{
m_nBufSize = 0;
}
return m_nBufSize; return m_nBufSize;
} }

View File

@ -155,13 +155,13 @@ struct BufferEquivalent
BufferID bid2 = _pTab->getBufferByIndex(i2); BufferID bid2 = _pTab->getBufferByIndex(i2);
Buffer * b1 = MainFileManager.getBufferByID(bid1); Buffer * b1 = MainFileManager.getBufferByID(bid1);
Buffer * b2 = MainFileManager.getBufferByID(bid2); Buffer * b2 = MainFileManager.getBufferByID(bid2);
if (_iColumn == 0) if (_iColumn == 0)
{ {
const TCHAR *s1 = b1->getFileName(); const TCHAR *s1 = b1->getFileName();
const TCHAR *s2 = b2->getFileName(); const TCHAR *s2 = b2->getFileName();
int result = _strequiv(s1, s2); int result = _strequiv(s1, s2);
if (result != 0) // default to filepath sorting when equivalent if (result != 0) // default to filepath sorting when equivalent
return result < 0; return result < 0;
} }
@ -179,7 +179,7 @@ struct BufferEquivalent
} }
else else
s1 = TEXT(""); s1 = TEXT("");
Lang *lang2 = nppParameters.getLangFromID(b2->getLangType()); Lang *lang2 = nppParameters.getLangFromID(b2->getLangType());
if (lang2) if (lang2)
{ {
@ -187,7 +187,7 @@ struct BufferEquivalent
} }
else else
s2 = TEXT(""); s2 = TEXT("");
int result = _strequiv(s1, s2); int result = _strequiv(s1, s2);
if (result != 0) // default to filepath sorting when equivalent if (result != 0) // default to filepath sorting when equivalent
@ -198,11 +198,11 @@ struct BufferEquivalent
{ {
auto t1 = b1->docLength(); auto t1 = b1->docLength();
auto t2 = b2->docLength(); auto t2 = b2->docLength();
if (t1 != t2) // default to filepath sorting when equivalent if (t1 != t2) // default to filepath sorting when equivalent
return (t1 < t2); return (t1 < t2);
} }
// _iColumn == 1 // _iColumn == 1
const TCHAR *s1 = b1->getFullPathName(); const TCHAR *s1 = b1->getFullPathName();
const TCHAR *s2 = b2->getFullPathName(); const TCHAR *s2 = b2->getFullPathName();
@ -326,13 +326,13 @@ intptr_t CALLBACK WindowsDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM lP
_currentColumn = 0; _currentColumn = 0;
_reverseSort = false; _reverseSort = false;
_lastSort = _currentColumn; _lastSort = _currentColumn;
updateColumnNames(); updateColumnNames();
doColumnSort(); doColumnSort();
} }
doSortToTabs(); doSortToTabs();
// must re-sort because tab indexes changed // must re-sort because tab indexes changed
doColumnSort(); doColumnSort();
break; break;
@ -431,7 +431,7 @@ intptr_t CALLBACK WindowsDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM lP
if (pNMLV->iItem == -1) if (pNMLV->iItem == -1)
{ {
_currentColumn = pNMLV->iSubItem; _currentColumn = pNMLV->iSubItem;
if (_lastSort == _currentColumn) if (_lastSort == _currentColumn)
{ {
_reverseSort = true; _reverseSort = true;
@ -442,7 +442,7 @@ intptr_t CALLBACK WindowsDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM lP
_reverseSort = false; _reverseSort = false;
_lastSort = _currentColumn; _lastSort = _currentColumn;
} }
updateColumnNames(); updateColumnNames();
doColumnSort(); doColumnSort();
} }
@ -512,7 +512,7 @@ void WindowsDlg::doColumnSort()
{ {
if (_currentColumn == -1) if (_currentColumn == -1)
return; return;
size_t i = 0; size_t i = 0;
size_t n = _idxMap.size(); size_t n = _idxMap.size();
vector<int> sortMap; vector<int> sortMap;
@ -597,7 +597,7 @@ BOOL WindowsDlg::onInitDialog()
LVCOLUMN lvColumn{}; LVCOLUMN lvColumn{};
lvColumn.mask = LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM | LVCF_FMT; lvColumn.mask = LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM | LVCF_FMT;
lvColumn.fmt = LVCFMT_LEFT; lvColumn.fmt = LVCFMT_LEFT;
generic_string columnText; generic_string columnText;
NativeLangSpeaker *pNativeSpeaker = (NppParameters::getInstance()).getNativeLangSpeaker(); NativeLangSpeaker *pNativeSpeaker = (NppParameters::getInstance()).getNativeLangSpeaker();
@ -646,7 +646,7 @@ void WindowsDlg::updateColumnNames()
generic_string columnText; generic_string columnText;
NativeLangSpeaker *pNativeSpeaker = (NppParameters::getInstance()).getNativeLangSpeaker(); NativeLangSpeaker *pNativeSpeaker = (NppParameters::getInstance()).getNativeLangSpeaker();
columnText = pNativeSpeaker->getAttrNameStr(TEXT("Name"), WD_ROOTNODE, WD_CLMNNAME); columnText = pNativeSpeaker->getAttrNameStr(TEXT("Name"), WD_ROOTNODE, WD_CLMNNAME);
if (_currentColumn != 0) if (_currentColumn != 0)
{ {
@ -801,7 +801,8 @@ void WindowsDlg::fitColumnsToSize()
void WindowsDlg::resetSelection() void WindowsDlg::resetSelection()
{ {
assert(_pTab != nullptr); if (!_pTab)
return;
auto curSel = _pTab->getCurrentTabIndex(); auto curSel = _pTab->getCurrentTabIndex();
int pos = 0; int pos = 0;
@ -946,7 +947,7 @@ void WindowsDlg::doCount()
void WindowsDlg::doSort() void WindowsDlg::doSort()
{ {
if (_pTab == NULL) if (!_pTab)
return; return;
size_t count = _pTab->nbItem(); size_t count = _pTab->nbItem();
@ -969,7 +970,7 @@ void WindowsDlg::doSort()
_idxMap.clear(); _idxMap.clear();
refreshMap(); refreshMap();
} }
//After sorting, need to open the active tab before sorting //After sorting, need to open the active tab before sorting
//This will be helpful when large number of documents are opened //This will be helpful when large number of documents are opened
__int64 newPosition = -1; __int64 newPosition = -1;
@ -981,7 +982,7 @@ void WindowsDlg::doSort()
nmdlg.type = WDT_ACTIVATE; nmdlg.type = WDT_ACTIVATE;
nmdlg.curSel = static_cast<UINT>(newPosition); nmdlg.curSel = static_cast<UINT>(newPosition);
nmdlg.hwndFrom = _hSelf; nmdlg.hwndFrom = _hSelf;
nmdlg.code = WDN_NOTIFY; nmdlg.code = WDN_NOTIFY;
SendMessage(_hParent, WDN_NOTIFY, 0, LPARAM(&nmdlg)); SendMessage(_hParent, WDN_NOTIFY, 0, LPARAM(&nmdlg));
} }

View File

@ -108,7 +108,7 @@ static void ColouriseSearchResultLine(SearchResultMarkings* pMarkings, char *lin
static void ColouriseSearchResultDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler) { static void ColouriseSearchResultDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler) {
char lineBuffer[SC_SEARCHRESULT_LINEBUFFERMAXLENGTH]; char lineBuffer[SC_SEARCHRESULT_LINEBUFFERMAXLENGTH] {};
styler.StartAt(startPos); styler.StartAt(startPos);
styler.StartSegment(startPos); styler.StartSegment(startPos);
unsigned int linePos = 0; unsigned int linePos = 0;