From 5f8ac45789f00467e0f7fc588ac18a3561815cd3 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 21 May 2015 01:09:55 +0200 Subject: [PATCH] Avoid using out of range value in wxSpinCtrl with inversed range. In wxMSW it is possible that minimal allowed value is greater than maximal allowed one and the native control works correctly in this case, however wxSpinCtrl set m_oldValue to an invalid value which could result in an infinite recursion if SetRange() was called from the wxEVT_SPINCTRL event handler. For example, if the control value was initially 0 and the event handler called SetRange(1, 0), it would result setting the value to 1 because it was less than the minimum, resulting in another call to the event handler which would now set the value to 0 because it was more than the maximum resulting in another call to the event handler and so forth. Fix this by ensuring that the value lies between minimal and maximal values, whatever is their relative order. --- src/msw/spinctrl.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/msw/spinctrl.cpp b/src/msw/spinctrl.cpp index f2eece9f36..7c9f617905 100644 --- a/src/msw/spinctrl.cpp +++ b/src/msw/spinctrl.cpp @@ -544,10 +544,20 @@ void wxSpinCtrl::SetRange(int minVal, int maxVal) // Manually adjust the old value to avoid an event being sent from // NormalizeValue() called from inside the base class SetRange() as we're // not supposed to generate any events from here. - if ( m_oldValue < minVal ) - m_oldValue = minVal; - else if ( m_oldValue > maxVal ) - m_oldValue = maxVal; + if ( minVal <= maxVal ) + { + if ( m_oldValue < minVal ) + m_oldValue = minVal; + else if ( m_oldValue > maxVal ) + m_oldValue = maxVal; + } + else // reversed range + { + if ( m_oldValue > minVal ) + m_oldValue = minVal; + else if ( m_oldValue < maxVal ) + m_oldValue = maxVal; + } wxSpinButton::SetRange(minVal, maxVal);