fix: leaks and crash for relay PUT

This commit is contained in:
Tobias Reisinger 2019-09-08 19:34:37 +02:00
parent 71f7447f09
commit 77975299a5
3 changed files with 24 additions and 11 deletions

View file

@ -84,6 +84,8 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
strncpy(relay->active_schedule_id, body["active_schedule"].asCString(), 32); strncpy(relay->active_schedule_id, body["active_schedule"].asCString(), 32);
strncpy(relay->controller_id, controller_id.c_str(), 32); strncpy(relay->controller_id, controller_id.c_str(), 32);
relay->reload_active_schedule();
db_action_result = relay->insert(); db_action_result = relay->insert();
} }

View file

@ -82,18 +82,9 @@ relay_db_select(sqlite3_stmt *stmt)
if (s == SQLITE_ROW) if (s == SQLITE_ROW)
{ {
relay_dbo *new_relay = relay_db_select_mapper(stmt); relay_dbo *new_relay = relay_db_select_mapper(stmt);
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", new_relay->active_schedule_id, (intptr_t)&sqlite3_bind_text);
if(!schedules[0]) new_relay->reload_active_schedule();
{
free(schedules);
schedules = schedule_dbo::get_by_simple("id", "off", (intptr_t)&sqlite3_bind_text);
strcpy(new_relay->active_schedule_id, "off");
}
new_relay->active_schedule = schedules[0];
free(schedules);
row++; row++;
all_relays = (relay_dbo**)realloc(all_relays, sizeof(relay_dbo*) * (row + 1)); all_relays = (relay_dbo**)realloc(all_relays, sizeof(relay_dbo*) * (row + 1));
@ -119,6 +110,23 @@ relay_db_select(sqlite3_stmt *stmt)
return all_relays; return all_relays;
} }
void
relay_dbo::reload_active_schedule()
{
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", this->active_schedule_id, (intptr_t)&sqlite3_bind_text);
if(!schedules[0])
{
free(schedules);
schedules = schedule_dbo::get_by_simple("id", "off", (intptr_t)&sqlite3_bind_text);
strcpy(this->active_schedule_id, "off");
}
this->active_schedule = schedules[0];
free(schedules);
}
bool bool
relay_dbo::update() relay_dbo::update()
{ {
@ -244,7 +252,7 @@ relay_dbo::free_list(relay_dbo **relays_list)
{ {
for(int i = 0; relays_list[i] != nullptr; i++) for(int i = 0; relays_list[i] != nullptr; i++)
{ {
free(relays_list[i]); delete relays_list[i];
} }
free(relays_list); free(relays_list);
} }

View file

@ -18,6 +18,9 @@ public:
char active_schedule_id[33]; char active_schedule_id[33];
schedule_dbo *active_schedule; schedule_dbo *active_schedule;
void
reload_active_schedule();
bool bool
update(); update();