QDial: don't crash when min==max and setting a value != min & max

QDial::bound() is crashing when min == max due to a division by zero.
Therefore check for this condition beforehand and return min.

Pick-to: 6.5 6.2 5.15
Fixes: QTBUG-104641
Change-Id: I612625af1ad18333d59a7771abfdec602301b58e
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
(cherry picked from commit e4315204b1412d74842b3167c3eb9a49dc233355)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Christian Ehrlicher 2023-11-12 13:49:16 +01:00 committed by Qt Cherry-pick Bot
parent 83f7843403
commit ef2a6ba74f
2 changed files with 12 additions and 0 deletions

View File

@ -58,6 +58,8 @@ int QDialPrivate::bound(int val) const
if (wrapping) { if (wrapping) {
if ((val >= minimum) && (val <= maximum)) if ((val >= minimum) && (val <= maximum))
return val; return val;
if (minimum == maximum)
return minimum;
val = minimum + ((val - minimum) % (maximum - minimum)); val = minimum + ((val - minimum) % (maximum - minimum));
if (val < minimum) if (val < minimum)
val += maximum - minimum; val += maximum - minimum;

View File

@ -17,6 +17,7 @@ private slots:
void valueChanged(); void valueChanged();
void sliderMoved(); void sliderMoved();
void wrappingCheck(); void wrappingCheck();
void minEqualMaxValueOutsideRange();
void notchSize_data(); void notchSize_data();
void notchSize(); void notchSize();
@ -172,6 +173,15 @@ void tst_QDial::wrappingCheck()
} }
} }
// QTBUG-104641
void tst_QDial::minEqualMaxValueOutsideRange()
{
QDial dial;
dial.setRange(30, 30);
dial.setWrapping(true);
dial.setValue(45);
}
/* /*
Verify that the notchSizes calculated don't change compared Verify that the notchSizes calculated don't change compared
to Qt 5.15 results for dial sizes at the edge values of the to Qt 5.15 results for dial sizes at the edge values of the