Use wxCHECKs to protect non-position TOOL_EVENTS

Getting the position of a non-position TOOL_EVENT will
now result in a wxCHECK, and a null-position (0,0) will
be returned. The new interface HasPosition() can be used
to determine if a call to a position function is safe to
make from the caller.

Fixes the underlying danger behind lp:1796045, which has
already been neutralised by a4966adb6, which avoids calling
the Position() functions when the TOOL_EVENT is not known
to be a position.

Fixes: lp:1796045
* https://bugs.launchpad.net/kicad/+bug/1796045
This commit is contained in:
John Beard 2018-10-08 23:49:42 +01:00 committed by Seth Hillbrand
parent dbc9130da9
commit b64c6dbf35
1 changed files with 40 additions and 10 deletions

View File

@ -30,10 +30,16 @@
#include <iterator> #include <iterator>
#include <math/vector2d.h> #include <math/vector2d.h>
#include <cassert>
#include <core/optional.h> #include <core/optional.h>
#ifdef WX_COMPATIBILITY
#include <wx/debug.h>
#else
#include <cassert>
#endif
class TOOL_ACTION; class TOOL_ACTION;
class TOOL_MANAGER; class TOOL_MANAGER;
@ -236,26 +242,29 @@ public:
return m_actions; return m_actions;
} }
///> Returns if it this event has a valid position (true for mouse events)
bool HasPosition() const
{
return m_category == TC_MOUSE;
}
///> Returns information about difference between current mouse cursor position and the place ///> Returns information about difference between current mouse cursor position and the place
///> where dragging has started. ///> where dragging has started.
const VECTOR2D& Delta() const const VECTOR2D Delta() const
{ {
assert( m_category == TC_MOUSE ); // this should be used only with mouse events return returnCheckedPosition( m_mouseDelta );
return m_mouseDelta;
} }
///> Returns mouse cursor position in world coordinates. ///> Returns mouse cursor position in world coordinates.
const VECTOR2D& Position() const const VECTOR2D Position() const
{ {
assert( m_category == TC_MOUSE ); // this should be used only with mouse events return returnCheckedPosition( m_mousePos );
return m_mousePos;
} }
///> Returns the point where dragging has started. ///> Returns the point where dragging has started.
const VECTOR2D& DragOrigin() const const VECTOR2D DragOrigin() const
{ {
assert( m_category == TC_MOUSE ); // this should be used only with mouse events return returnCheckedPosition( m_mouseDragOrigin );
return m_mouseDragOrigin;
} }
///> Returns information about mouse buttons state. ///> Returns information about mouse buttons state.
@ -432,6 +441,27 @@ private:
m_modifiers = aMods; m_modifiers = aMods;
} }
/**
* Ensure that the event is a type that has a position before returning a
* position, otherwise return a mull-constructed position.
* Used to defend the position accessors from runtime access when the event
* does not have a valid position.
*
* @param aPos the position to return if the event is valid
* @return the checked position
*/
VECTOR2D returnCheckedPosition( const VECTOR2D& aPos ) const
{
#ifdef WX_COMPATIBILITY
wxCHECK_MSG( HasPosition(), VECTOR2D(),
"Attempted to get position from non-position event" );
#else
assert( HasPosition() );
#endif
return aPos;
}
TOOL_EVENT_CATEGORY m_category; TOOL_EVENT_CATEGORY m_category;
TOOL_ACTIONS m_actions; TOOL_ACTIONS m_actions;
TOOL_ACTION_SCOPE m_scope; TOOL_ACTION_SCOPE m_scope;