Added range checks

This commit is contained in:
Kai Lauterbach 2023-05-12 11:43:27 +02:00
parent 013cd8a8bf
commit c974e394d8

View file

@ -405,6 +405,13 @@ void init_webserver()
{ {
const char* key = state.key().c_str(); const char* key = state.key().c_str();
int light = atoi(key) - 1; int light = atoi(key) - 1;
if (light < 0)
{
light = 0;
} else if (light > (LIGHTS_COUNT-1))
{
light = (LIGHTS_COUNT-1);
}
JsonObject values = state.value(); JsonObject values = state.value();
uint8_t tmp = EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS); uint8_t tmp = EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS);
@ -441,6 +448,10 @@ void init_webserver()
if (values.containsKey("transitiontime")) if (values.containsKey("transitiontime"))
{ {
default_transitiontime = values["transitiontime"]; default_transitiontime = values["transitiontime"];
if (default_transitiontime < 0)
{
default_transitiontime = 0;
}
if (tc_enabled == TIMING_CONTROL_DISABLED) if (tc_enabled == TIMING_CONTROL_DISABLED)
{ {
for (uint8_t i = 0 ; i < LIGHTS_COUNT; i++) for (uint8_t i = 0 ; i < LIGHTS_COUNT; i++)
@ -461,6 +472,13 @@ void init_webserver()
server.on("/state", HTTP_GET, []() server.on("/state", HTTP_GET, []()
{ // HTTP GET request used to fetch current light state { // HTTP GET request used to fetch current light state
uint8_t light = server.arg("light").toInt() - 1; uint8_t light = server.arg("light").toInt() - 1;
if (light < 0)
{
light = 0;
} else if (light > (LIGHTS_COUNT-1))
{
light = (LIGHTS_COUNT-1);
}
DynamicJsonDocument root(512); DynamicJsonDocument root(512);
root["on"] = light_state[light]; root["on"] = light_state[light];
root["bri"] = bri[light]; root["bri"] = bri[light];
@ -536,6 +554,10 @@ void init_webserver()
if (server.hasArg("transition")) if (server.hasArg("transition"))
{ {
default_transitiontime = server.arg("transition").toFloat(); default_transitiontime = server.arg("transition").toFloat();
if (default_transitiontime < 0)
{
default_transitiontime = 0;
}
if (tc_enabled == TIMING_CONTROL_DISABLED) if (tc_enabled == TIMING_CONTROL_DISABLED)
{ {
for (uint8_t i = 0 ; i < LIGHTS_COUNT; i++) for (uint8_t i = 0 ; i < LIGHTS_COUNT; i++)
@ -610,9 +632,11 @@ void init_webserver()
} }
// scene switch handling // scene switch handling
if (server.hasArg("scene")) { if (server.hasArg("scene"))
{
scene = server.arg("scene").toInt(); scene = server.arg("scene").toInt();
if (EEPROM.read(EEPROM_SCENE_ADDRESS) != scene) { if (EEPROM.read(EEPROM_SCENE_ADDRESS) != scene)
{
EEPROM.write(EEPROM_SCENE_ADDRESS, scene); EEPROM.write(EEPROM_SCENE_ADDRESS, scene);
EEPROM.commit(); EEPROM.commit();
Serial.print("Scene set to "); Serial.print("Scene set to ");
@ -623,7 +647,8 @@ void init_webserver()
if (server.hasArg("dip")) { if (server.hasArg("dip")) {
uint8_t tmp = EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS); uint8_t tmp = EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS);
uint8_t tmp2 = (server.arg("dip") == "true" ? 1 : 0); uint8_t tmp2 = (server.arg("dip") == "true" ? 1 : 0);
if (tmp != tmp2) { if (tmp != tmp2)
{
EEPROM.write(EEPROM_DYNAMIC_IP_ADDRESS, tmp2); EEPROM.write(EEPROM_DYNAMIC_IP_ADDRESS, tmp2);
EEPROM.commit(); EEPROM.commit();
Serial.print("Set dynamic IP to "); Serial.print("Set dynamic IP to ");