diff --git a/src/emsdevicevalue.cpp b/src/emsdevicevalue.cpp index 72087864c..f436e96b3 100644 --- a/src/emsdevicevalue.cpp +++ b/src/emsdevicevalue.cpp @@ -327,29 +327,37 @@ bool DeviceValue::get_min_max(int16_t & dv_set_min, int16_t & dv_set_max) { } // extract custom min from custom_fullname -bool DeviceValue::get_custom_min(int16_t & v) { +bool DeviceValue::get_custom_min(int16_t & val) { auto min_pos = custom_fullname.find('>'); bool has_min = (min_pos != std::string::npos); uint8_t fahrenheit = !EMSESP::system_.fahrenheit() ? 0 : (uom == DeviceValueUOM::DEGREES) ? 2 : (uom == DeviceValueUOM::DEGREES_R) ? 1 : 0; if (has_min) { - v = Helpers::atoint(custom_fullname.substr(min_pos + 1).c_str()); + int v = Helpers::atoint(custom_fullname.substr(min_pos + 1).c_str()); if (fahrenheit) { v = (v - (32 * (fahrenheit - 1))) / 1.8; // reset to °C } + if (v > max) { + return false; + } + val = v; } return has_min; } // extract custom max from custom_fullname -bool DeviceValue::get_custom_max(uint16_t & v) { +bool DeviceValue::get_custom_max(uint16_t & val) { auto max_pos = custom_fullname.find('<'); bool has_max = (max_pos != std::string::npos); uint8_t fahrenheit = !EMSESP::system_.fahrenheit() ? 0 : (uom == DeviceValueUOM::DEGREES) ? 2 : (uom == DeviceValueUOM::DEGREES_R) ? 1 : 0; if (has_max) { - v = Helpers::atoint(custom_fullname.substr(max_pos + 1).c_str()); + int v = Helpers::atoint(custom_fullname.substr(max_pos + 1).c_str()); if (fahrenheit) { v = (v - (32 * (fahrenheit - 1))) / 1.8; // reset to °C } + if (v < 0 || v < min) { + return false; + } + val = v; } return has_max; } diff --git a/src/emsdevicevalue.h b/src/emsdevicevalue.h index 269a476d4..d3ac85ce5 100644 --- a/src/emsdevicevalue.h +++ b/src/emsdevicevalue.h @@ -180,8 +180,8 @@ class DeviceValue { bool get_min_max(int16_t & dv_set_min, int16_t & dv_set_max); void set_custom_minmax(); - bool get_custom_min(int16_t & v); - bool get_custom_max(uint16_t & v); + bool get_custom_min(int16_t & val); + bool get_custom_max(uint16_t & val); std::string get_custom_fullname() const; std::string get_fullname() const;