From a2dfcebf3ffe5260c44f4c51d1941fab8be3d3b6 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Fri, 19 Jul 2019 14:42:36 +0200 Subject: [PATCH] fix: leaks --- controllers/api_v1_devices_discover.cc | 41 ++++++++++++++++++-------- controllers/api_v1_schedules.cc | 19 +++++++----- main.cc | 18 ++++++++++- models/device_dbo.cc | 3 ++ models/period.cc | 14 ++++----- models/schedule_dbo.cc | 32 ++++++++++++++------ models/schedule_dbo.h | 2 ++ 7 files changed, 92 insertions(+), 37 deletions(-) diff --git a/controllers/api_v1_devices_discover.cc b/controllers/api_v1_devices_discover.cc index 61dee94..86524f9 100644 --- a/controllers/api_v1_devices_discover.cc +++ b/controllers/api_v1_devices_discover.cc @@ -9,13 +9,13 @@ using namespace api::v1; void devices::post_discover(const HttpRequestPtr &req, std::function &&callback) { - auto resp = HttpResponse::newHttpResponse(); int discover_server_socket = helpers::bind_tcp_server("0.0.0.0", "0", config::discover_max_client_backlog); int discover_server_port = helpers::get_server_port(discover_server_socket); if(discover_server_port == -1) { + auto resp = HttpResponse::newHttpResponse(); resp->setStatusCode(k500InternalServerError); callback(resp); return; @@ -28,6 +28,7 @@ void devices::post_discover(const HttpRequestPtr &req, std::functionsetStatusCode(k500InternalServerError); callback(resp); return; @@ -42,7 +43,7 @@ void devices::post_discover(const HttpRequestPtr &req, std::functionid, discovered_id) == 0) + if(strcmp(known_devices[i]->id, discovered_id) == 0) { - all_devices[i]->active = true; - all_devices[i]->update(); - free(all_devices[i]); + known_devices[i]->active = true; + known_devices[i]->update(); + free(known_devices[i]); found_discovered_in_list = true; - all_devices[i] = all_devices[i + 1]; + known_devices[i] = known_devices[i + 1]; } } else { - all_devices[i] = all_devices[i + 1]; + known_devices[i] = known_devices[i + 1]; } } @@ -139,15 +142,27 @@ void devices::post_discover(const HttpRequestPtr &req, std::functionactive = false; + known_devices[i]->update(); + LOG_DEBUG << "Lost: " << known_devices[i]->name; + } + device_dbo::free_list(known_devices); + + device_dbo **all_devices = device_dbo::get_all(); + Json::Value all_devices_json(Json::arrayValue); + for(int i = 0; all_devices[i] != nullptr; i++) { - all_devices[i]->active = false; - all_devices[i]->update(); - LOG_DEBUG << "Lost: " << all_devices[i]->name; + all_devices_json.append(all_devices[i]->to_json()); } - close(discover_server_socket); + auto resp = HttpResponse::newHttpJsonResponse(all_devices_json); callback(resp); + + device_dbo::free_list(all_devices); + } diff --git a/controllers/api_v1_schedules.cc b/controllers/api_v1_schedules.cc index 1e1dc9d..7463eec 100644 --- a/controllers/api_v1_schedules.cc +++ b/controllers/api_v1_schedules.cc @@ -61,7 +61,7 @@ schedules::delete_one_by_id(const HttpRequestPtr &req, std::functionsetStatusCode(k500InternalServerError); + callback(resp); + } + else + { + auto resp = HttpResponse::newHttpJsonResponse(new_schedule.to_json()); + callback(resp); + } } diff --git a/main.cc b/main.cc index 60b44b8..9a89e9d 100644 --- a/main.cc +++ b/main.cc @@ -6,13 +6,29 @@ #include "globals.h" +static void +terminate(int signum) +{ + LOG_INFO << "Terminating Server (" << signum << ")"; + + sqlite3_close(globals::db); + + quick_exit(signum); +} + /*static void test() { LOG_DEBUG << "LOOP"; }*/ -int main() +int +main() { + signal(SIGINT, terminate); + signal(SIGABRT, terminate); + signal(SIGTERM, terminate); + signal(SIGKILL, terminate); + int rc; /* Open database */ diff --git a/models/device_dbo.cc b/models/device_dbo.cc index 66705d2..30c20dd 100644 --- a/models/device_dbo.cc +++ b/models/device_dbo.cc @@ -98,11 +98,13 @@ device_db_select(sqlite3_stmt *stmt) else { LOG_ERROR << "Error Selecting devices from database"; + sqlite3_finalize(stmt); return nullptr; } } } + sqlite3_finalize(stmt); all_devices[row] = nullptr; return all_devices; @@ -137,6 +139,7 @@ device_dbo::remove() sqlite3_prepare_v2(globals::db, "DELETE FROM devices WHERE id=?1;", -1, &stmt, nullptr); sqlite3_bind_text(stmt, 1, this->id, -1, SQLITE_STATIC); rc = sqlite3_step(stmt); + sqlite3_finalize(stmt); return rc == SQLITE_DONE; diff --git a/models/period.cc b/models/period.cc index 322beba..4f4269c 100644 --- a/models/period.cc +++ b/models/period.cc @@ -15,13 +15,13 @@ period::to_json() { Json::Value result; - char start[6], end[6]; + char start_str[6], end_str[6]; - sprintf(start, "%02d:%02d", (int)(this->start / 60), this->start % 60); - sprintf(end, "%02d:%02d", (int)(this->end / 60), this->end % 60); + sprintf(start_str, "%02d:%02d", (int)(this->start / 60), this->start % 60); + sprintf(end_str, "%02d:%02d", (int)(this->end / 60), this->end % 60); - result["start"] = std::string(start); - result["end"] = std::string(end); + result["start"] = std::string(start_str); + result["end"] = std::string(end_str); return result; } @@ -31,11 +31,11 @@ period::is_in_period(uint16_t timestamp) { if(this->start < this->end) { - return this->start < timestamp and timestamp < this->end; + return this->start <= timestamp and timestamp <= this->end; } if(this->start > this->end) { - return this->end < timestamp and timestamp < this->start; + return this->end <= timestamp and timestamp <= this->start; } return this->start == timestamp; } diff --git a/models/schedule_dbo.cc b/models/schedule_dbo.cc index 6fa3d48..aeea7d0 100644 --- a/models/schedule_dbo.cc +++ b/models/schedule_dbo.cc @@ -20,17 +20,16 @@ static bool schedule_db_update_insert(schedule_dbo *schedule, sqlite3_stmt *stmt sqlite3_bind_blob(stmt, 3, periods_blob, sizeof(uint16_t) * ((periods_blob[0] * 2) + 1), SQLITE_STATIC); rc = sqlite3_step(stmt); - if (rc != SQLITE_DONE) - { - printf("ERROR inserting data: %s\n", sqlite3_errmsg(globals::db)); - free(periods_blob); - return false; - } sqlite3_finalize(stmt); - free(periods_blob); + if (rc != SQLITE_DONE) + { + LOG_ERROR << "ERROR inserting data: " << sqlite3_errmsg(globals::db); + return false; + } + return true; } @@ -89,11 +88,14 @@ schedule_db_select(sqlite3_stmt *stmt) else { LOG_ERROR << "Error Selecting schedules from database"; + sqlite3_finalize(stmt); return nullptr; } } } + sqlite3_finalize(stmt); + all_schedules[row] = nullptr; return all_schedules; @@ -129,10 +131,17 @@ schedule_dbo::remove() sqlite3_bind_text(stmt, 1, this->id, -1, SQLITE_STATIC); rc = sqlite3_step(stmt); + sqlite3_finalize(stmt); + return rc == SQLITE_DONE; } +schedule_dbo::~schedule_dbo() +{ + delete this->periods; +} + Json::Value schedule_dbo::to_json() { @@ -165,7 +174,13 @@ schedule_dbo::get_one_by(const char *key, const char *value) sqlite3_prepare_v2(globals::db, sql, -1, &stmt, nullptr); sqlite3_bind_text(stmt, 1, value, -1, SQLITE_STATIC); - return schedule_db_select(stmt)[0]; + schedule_dbo **schedule_list = schedule_db_select(stmt); + schedule_dbo *target_schedule = schedule_list[0]; + + free(schedule_list); + free(sql); + + return target_schedule; } void @@ -173,7 +188,6 @@ schedule_dbo::free_list(schedule_dbo **schedules_list) { for(int i = 0; schedules_list[i] != nullptr; i++) { - delete schedules_list[i]->periods; delete schedules_list[i]; } free(schedules_list); diff --git a/models/schedule_dbo.h b/models/schedule_dbo.h index 05826db..4adb78f 100644 --- a/models/schedule_dbo.h +++ b/models/schedule_dbo.h @@ -24,6 +24,8 @@ public: bool remove(); + ~schedule_dbo(); + Json::Value to_json();