Skip to content
Browse files

CCMenu - prevent a crash if some child item is not CCMenuItem

CCMenu::itemForTouch expects that all children are CCMenuItems.
But it is still possible to add other child types, for example
if CCAssert is disabled or some other addChild method is called.

In this case dynamic_cast to CCNode and later try to do
((CCMenuItem*)pChild)->isEnabled() triggers an undefined behaviour
- up to a crash or something else.

Fixed by a dynamic_cast straightforward to CCMenuItem.
  • Loading branch information...
1 parent 1ce8545 commit 45b2ee47e06e6180263aa4839700d4b6b88a4b5e @mingulov committed Oct 21, 2012
Showing with 4 additions and 4 deletions.
  1. +4 −4 cocos2dx/menu_nodes/CCMenu.cpp
View
8 cocos2dx/menu_nodes/CCMenu.cpp
@@ -652,16 +652,16 @@ CCMenuItem* CCMenu::itemForTouch(CCTouch *touch)
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
- CCNode* pChild = dynamic_cast<CCNode*>(pObject);
- if (pChild && pChild->isVisible() && ((CCMenuItem*)pChild)->isEnabled())
+ CCMenuItem* pChild = dynamic_cast<CCMenuItem*>(pObject);
+ if (pChild && pChild->isVisible() && pChild->isEnabled())
{
CCPoint local = pChild->convertToNodeSpace(touchLocation);
- CCRect r = ((CCMenuItem*)pChild)->rect();
+ CCRect r = pChild->rect();
r.origin = CCPointZero;
if (r.containsPoint(local))
{
- return (CCMenuItem*)pChild;
+ return pChild;
}
}
}

0 comments on commit 45b2ee4

Please sign in to comment.
Something went wrong with that request. Please try again.