1
0
forked from me/IronOS

Fix bug in setSettingsValue for values constrained between 0 and 1

Fixed a bug in the `setSettingValue` function that caused valid values
within the allowed range to be incorrectly constrained to the minimum
value, when the default value of the option was zero and the allowed
range was from zero to one. The bug was fixed by updating the order of
the `if` statements in the function to ensure that the range check is
done before the value is set. This ensures that valid values within the
range are correctly retained, while out-of-range values are still
constrained to the allowed range.
This commit is contained in:
Neil Hanlon
2023-02-22 01:26:06 -05:00
parent b1658ec6ee
commit a76049acde

View File

@@ -140,15 +140,16 @@ void resetSettings() {
void setSettingValue(const enum SettingsOptions option, const uint16_t newValue) { void setSettingValue(const enum SettingsOptions option, const uint16_t newValue) {
const auto constants = settingsConstants[(int)option]; const auto constants = settingsConstants[(int)option];
systemSettings.settingsValues[(int)option] = newValue; uint16_t constrainedValue = newValue;
// If less than min, constrain if (constrainedValue < constants.min) {
if (systemSettings.settingsValues[(int)option] < constants.min) { // If less than min, constrain
systemSettings.settingsValues[(int)option] = constants.min; constrainedValue = constants.min;
} }
// If hit max, constrain else if (constrainedValue > constants.max) {
if (systemSettings.settingsValues[(int)option] > constants.max) { // If hit max, constrain
systemSettings.settingsValues[(int)option] = constants.max; constrainedValue = constants.max;
} }
systemSettings.settingsValues[(int)option] = constrainedValue;
} }
// Lookup wrapper for ease of use (with typing) // Lookup wrapper for ease of use (with typing)
uint16_t getSettingValue(const enum SettingsOptions option) { return systemSettings.settingsValues[(int)option]; } uint16_t getSettingValue(const enum SettingsOptions option) { return systemSettings.settingsValues[(int)option]; }