From b9d52a26086e285ea29e260e5c2e827315b21e55 Mon Sep 17 00:00:00 2001 From: Ekopalypse Date: Mon, 21 Aug 2023 11:37:14 +0200 Subject: [PATCH 1/2] Check it GetMenu returns a valid handle and change return type to int for GetMenuItemCount --- PythonScript/src/MenuManager.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/PythonScript/src/MenuManager.cpp b/PythonScript/src/MenuManager.cpp index 6c504e15..e5b384a8 100644 --- a/PythonScript/src/MenuManager.cpp +++ b/PythonScript/src/MenuManager.cpp @@ -837,7 +837,7 @@ idx_t MenuManager::findPluginCommand(const TCHAR *pluginName, const TCHAR *menuO { HMENU hPluginMenu = (HMENU)::SendMessage(m_hNotepad, NPPM_GETMENUHANDLE, 0, 0); - size_t iMenuItems = (size_t)GetMenuItemCount(hPluginMenu); + int iMenuItems = GetMenuItemCount(hPluginMenu); TCHAR strBuffer[500]{}; for ( idx_t i = 0; i < iMenuItems; ++i ) { @@ -913,6 +913,9 @@ idx_t MenuManager::findMenuCommand(const TCHAR *menuName, const TCHAR *menuOptio } HMENU hMenuBar = ::GetMenu(m_hNotepad); + if (hMenuBar == NULL) { + return 0; + } idx_t retVal = findMenuCommand(hMenuBar, _T(""), menuName, menuOption); if (retVal != 0) @@ -936,7 +939,7 @@ tstring MenuManager::formatMenuName(const TCHAR *name) idx_t MenuManager::findMenuCommand(HMENU hParentMenu, const TCHAR *parentMenuName, const TCHAR *menuName, const TCHAR *menuOption) { - size_t iMenuItems = (size_t)GetMenuItemCount(hParentMenu); + int iMenuItems = GetMenuItemCount(hParentMenu); idx_t retVal = 0; TCHAR strBuffer[500]{}; @@ -956,7 +959,7 @@ idx_t MenuManager::findMenuCommand(HMENU hParentMenu, const TCHAR *parentMenuNam tstring thisMenuName = formatMenuName(strBuffer); if (NULL == menuName || 0 == _tcsicmp(menuName, thisMenuName.c_str())) { - size_t subMenuItems = (size_t)GetMenuItemCount(mii.hSubMenu); + int subMenuItems = GetMenuItemCount(mii.hSubMenu); for (idx_t subMenuPos = 0; subMenuPos < subMenuItems; ++subMenuPos) { TCHAR *context = NULL; From f113b91b8524896fe3aef4d5092d08448b9832c5 Mon Sep 17 00:00:00 2001 From: Ekopalypse <47723516+Ekopalypse@users.noreply.github.com> Date: Wed, 23 Aug 2023 08:50:13 +0200 Subject: [PATCH 2/2] Refactor loop variable type from idx_t to int in MenuManager.cpp The change was made to match the type of the for loop variable to the value returned by GetMenuItemCount. --- PythonScript/src/MenuManager.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/PythonScript/src/MenuManager.cpp b/PythonScript/src/MenuManager.cpp index e5b384a8..d8987a82 100644 --- a/PythonScript/src/MenuManager.cpp +++ b/PythonScript/src/MenuManager.cpp @@ -839,7 +839,7 @@ idx_t MenuManager::findPluginCommand(const TCHAR *pluginName, const TCHAR *menuO int iMenuItems = GetMenuItemCount(hPluginMenu); TCHAR strBuffer[500]{}; - for ( idx_t i = 0; i < iMenuItems; ++i ) + for ( int i = 0; i < iMenuItems; ++i ) { MENUITEMINFO mii{}; mii.cbSize = sizeof(MENUITEMINFO); @@ -944,7 +944,7 @@ idx_t MenuManager::findMenuCommand(HMENU hParentMenu, const TCHAR *parentMenuNam TCHAR strBuffer[500]{}; - for ( idx_t i = 0; i < iMenuItems; ++i ) + for ( int i = 0; i < iMenuItems; ++i ) { MENUITEMINFO mii{}; mii.cbSize = sizeof(MENUITEMINFO); @@ -960,7 +960,7 @@ idx_t MenuManager::findMenuCommand(HMENU hParentMenu, const TCHAR *parentMenuNam if (NULL == menuName || 0 == _tcsicmp(menuName, thisMenuName.c_str())) { int subMenuItems = GetMenuItemCount(mii.hSubMenu); - for (idx_t subMenuPos = 0; subMenuPos < subMenuItems; ++subMenuPos) + for (int subMenuPos = 0; subMenuPos < subMenuItems; ++subMenuPos) { TCHAR *context = NULL; ::GetMenuString(mii.hSubMenu, static_cast(subMenuPos), strBuffer, 500, MF_BYPOSITION); @@ -971,7 +971,7 @@ idx_t MenuManager::findMenuCommand(HMENU hParentMenu, const TCHAR *parentMenuNam if (0 == _tcsicmp(menuOption, nameStr.c_str())) { - return ::GetMenuItemID(mii.hSubMenu, (int)subMenuPos); + return ::GetMenuItemID(mii.hSubMenu, subMenuPos); } } } @@ -1145,4 +1145,4 @@ void MenuManager::checkShowConsole(bool checked) { ::SendMessage(m_hNotepad, NPPM_SETMENUITEMCHECK, (WPARAM)m_funcItems[1]._cmdID, (LPARAM)checked); s_menuItemConsoleChecked = checked; -} \ No newline at end of file +}