From 75c639d9e085a79c09a3c71e0d4cd67e64808c28 Mon Sep 17 00:00:00 2001 From: Ekopalypse <47723516+Ekopalypse@users.noreply.github.com> Date: Wed, 23 Aug 2023 17:50:03 +0200 Subject: [PATCH] Check if GetMenu returns a valid handle and change return type to int for GetMenuItemCount (#306) * Check it GetMenu returns a valid handle and change return type to int for GetMenuItemCount * 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 | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/PythonScript/src/MenuManager.cpp b/PythonScript/src/MenuManager.cpp index 6c504e15..d8987a82 100644 --- a/PythonScript/src/MenuManager.cpp +++ b/PythonScript/src/MenuManager.cpp @@ -837,9 +837,9 @@ 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 ) + for ( int i = 0; i < iMenuItems; ++i ) { MENUITEMINFO mii{}; mii.cbSize = sizeof(MENUITEMINFO); @@ -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,12 +939,12 @@ 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]{}; - for ( idx_t i = 0; i < iMenuItems; ++i ) + for ( int i = 0; i < iMenuItems; ++i ) { MENUITEMINFO mii{}; mii.cbSize = sizeof(MENUITEMINFO); @@ -956,8 +959,8 @@ 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); - for (idx_t subMenuPos = 0; subMenuPos < subMenuItems; ++subMenuPos) + int subMenuItems = GetMenuItemCount(mii.hSubMenu); + for (int subMenuPos = 0; subMenuPos < subMenuItems; ++subMenuPos) { TCHAR *context = NULL; ::GetMenuString(mii.hSubMenu, static_cast(subMenuPos), strBuffer, 500, MF_BYPOSITION); @@ -968,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); } } } @@ -1142,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 +}