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
::OpenClipboard(_pPublicInterface->getHSelf());
HANDLE clipboardData = ::GetClipboardData(CF_TEXT);
int len = static_cast<int32_t>(::GlobalSize(clipboardData));
LPVOID clipboardDataPtr = ::GlobalLock(clipboardData);
LPVOID clipboardData2 = NULL;
if (clipboardData != NULL)
{
int len = static_cast<int32_t>(::GlobalSize(clipboardData));
LPVOID clipboardDataPtr = ::GlobalLock(clipboardData);
HANDLE allocClipboardData = ::GlobalAlloc(GMEM_MOVEABLE, len);
LPVOID clipboardData2 = ::GlobalLock(allocClipboardData);
HANDLE allocClipboardData = ::GlobalAlloc(GMEM_MOVEABLE, len);
clipboardData2 = ::GlobalLock(allocClipboardData);
::memcpy(clipboardData2, clipboardDataPtr, len);
::GlobalUnlock(clipboardData);
::GlobalUnlock(allocClipboardData);
::CloseClipboard();
::memcpy(clipboardData2, clipboardDataPtr, len);
::GlobalUnlock(clipboardData);
::GlobalUnlock(allocClipboardData);
::CloseClipboard();
}
_pEditView->saveCurrentPos();

View File

@ -813,7 +813,7 @@ BOOL Notepad_plus::notify(SCNotification *notification)
}
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.
// Of course position_of_click must lie between the delimiters.
@ -863,7 +863,7 @@ BOOL Notepad_plus::notify(SCNotification *notification)
}
else
{ // 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);
if (!(indicMsk & (1 << URL_INDIC)))
break;
@ -1103,6 +1103,9 @@ BOOL Notepad_plus::notify(SCNotification *notification)
case SCN_AUTOCSELECTION:
{
if (!notifyView)
return FALSE;
const NppGUI& nppGui = NppParameters::getInstance().getNppGUI();
// 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)
delete [] m_pNewBuf;
m_pNewBuf = NULL;
m_nAllocatedBufSize = 0;
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;
utf8 c;
while (m_Iter16.get(&c))
*pCur++ = c;
ubyte* pCur = m_pNewBuf;
m_Iter16.set(m_pBuf + nSkip, len - nSkip, m_eEncoding);
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;
@ -407,70 +414,78 @@ bool Utf8_16_Write::writeFile(const void* p, size_t _size)
size_t Utf8_16_Write::convert(char* p, size_t _size)
{
if (m_pNewBuf)
{
{
delete [] m_pNewBuf;
m_pNewBuf = NULL;
m_nBufSize = 0;
}
switch (m_eEncoding)
{
try
{
switch (m_eEncoding)
{
case uni7Bit:
case uni8Bit:
case uniCookie:
case uni8Bit:
case uniCookie:
{
// Normal write
m_nBufSize = _size;
m_pNewBuf = (ubyte*)new ubyte[m_nBufSize];
memcpy(m_pNewBuf, p, _size);
}
// Normal write
m_pNewBuf = new ubyte[_size];
m_nBufSize = _size;
memcpy(m_pNewBuf, p, _size);
}
break;
case uniUTF8:
case uniUTF8:
{
m_nBufSize = _size + 3;
m_pNewBuf = (ubyte*)new ubyte[m_nBufSize];
memcpy(m_pNewBuf, k_Boms[m_eEncoding], 3);
memcpy(&m_pNewBuf[3], p, _size);
}
m_pNewBuf = new ubyte[_size + 3];
m_nBufSize = _size + 3;
memcpy(m_pNewBuf, k_Boms[m_eEncoding], 3);
memcpy(&m_pNewBuf[3], p, _size);
}
break;
case uni16BE_NoBOM:
case uni16LE_NoBOM:
case uni16BE:
case uni16LE:
case uni16BE_NoBOM:
case uni16LE_NoBOM:
case uni16BE:
case uni16LE:
{
utf16* pCur = NULL;
if (m_eEncoding == uni16BE || m_eEncoding == uni16LE)
if (m_eEncoding == uni16BE || m_eEncoding == uni16LE)
{
// Write the BOM
m_pNewBuf = (ubyte*)new ubyte[sizeof(utf16) * (_size + 1)];
memcpy(m_pNewBuf, k_Boms[m_eEncoding], 2);
pCur = (utf16*)&m_pNewBuf[2];
}
// Write the BOM
m_pNewBuf = new ubyte[sizeof(utf16) * (_size + 1)];
memcpy(m_pNewBuf, k_Boms[m_eEncoding], 2);
pCur = (utf16*)&m_pNewBuf[2];
}
else
{
m_pNewBuf = (ubyte*)new ubyte[sizeof(utf16) * _size];
pCur = (utf16*)m_pNewBuf;
m_pNewBuf = new ubyte[sizeof(utf16) * _size];
pCur = (utf16*)m_pNewBuf;
}
Utf8_Iter iter8;
iter8.set(reinterpret_cast<const ubyte*>(p), _size, m_eEncoding);
Utf8_Iter iter8;
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++);
}
}
m_nBufSize = (const char*)pCur - (const char*)m_pNewBuf;
}
iter8.get(pCur++);
}
}
m_nBufSize = (const char*)pCur - (const char*)m_pNewBuf;
}
break;
default:
break;
}
default:
break;
}
}
catch (const std::bad_alloc&)
{
m_nBufSize = 0;
}
return m_nBufSize;
}

View File

@ -801,7 +801,8 @@ void WindowsDlg::fitColumnsToSize()
void WindowsDlg::resetSelection()
{
assert(_pTab != nullptr);
if (!_pTab)
return;
auto curSel = _pTab->getCurrentTabIndex();
int pos = 0;
@ -946,7 +947,7 @@ void WindowsDlg::doCount()
void WindowsDlg::doSort()
{
if (_pTab == NULL)
if (!_pTab)
return;
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) {
char lineBuffer[SC_SEARCHRESULT_LINEBUFFERMAXLENGTH];
char lineBuffer[SC_SEARCHRESULT_LINEBUFFERMAXLENGTH] {};
styler.StartAt(startPos);
styler.StartSegment(startPos);
unsigned int linePos = 0;