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
This commit is contained in:
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);
LPVOID clipboardData2 = NULL;
if (clipboardData != NULL)
{
int len = static_cast<int32_t>(::GlobalSize(clipboardData)); int len = static_cast<int32_t>(::GlobalSize(clipboardData));
LPVOID clipboardDataPtr = ::GlobalLock(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,10 +170,16 @@ 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];
if (m_pNewBuf)
{
m_nAllocatedBufSize = newSize; m_nAllocatedBufSize = newSize;
} }
}
if (m_nAllocatedBufSize)
{
ubyte* pCur = m_pNewBuf; ubyte* pCur = m_pNewBuf;
m_Iter16.set(m_pBuf + nSkip, len - nSkip, m_eEncoding); m_Iter16.set(m_pBuf + nSkip, len - nSkip, m_eEncoding);
@ -187,6 +193,7 @@ size_t Utf8_16_Read::convert(char* buf, size_t len)
} }
m_nNewBufSize = pCur - m_pNewBuf; m_nNewBufSize = pCur - m_pNewBuf;
} }
}
break; break;
default: default:
@ -410,8 +417,11 @@ size_t Utf8_16_Write::convert(char* p, size_t _size)
{ {
delete [] m_pNewBuf; delete [] m_pNewBuf;
m_pNewBuf = NULL; m_pNewBuf = NULL;
m_nBufSize = 0;
} }
try
{
switch (m_eEncoding) switch (m_eEncoding)
{ {
case uni7Bit: case uni7Bit:
@ -419,16 +429,16 @@ size_t Utf8_16_Write::convert(char* p, size_t _size)
case uniCookie: case uniCookie:
{ {
// Normal write // Normal write
m_pNewBuf = new ubyte[_size];
m_nBufSize = _size; m_nBufSize = _size;
m_pNewBuf = (ubyte*)new ubyte[m_nBufSize];
memcpy(m_pNewBuf, p, _size); memcpy(m_pNewBuf, p, _size);
} }
break; break;
case uniUTF8: case uniUTF8:
{ {
m_pNewBuf = new ubyte[_size + 3];
m_nBufSize = _size + 3; m_nBufSize = _size + 3;
m_pNewBuf = (ubyte*)new ubyte[m_nBufSize];
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);
} }
@ -444,13 +454,13 @@ size_t Utf8_16_Write::convert(char* p, size_t _size)
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;
} }
@ -471,6 +481,11 @@ size_t Utf8_16_Write::convert(char* p, size_t _size)
default: default:
break; break;
} }
}
catch (const std::bad_alloc&)
{
m_nBufSize = 0;
}
return m_nBufSize; return m_nBufSize;
} }

View File

@ -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();

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;