fix: leaks
This commit is contained in:
		
							parent
							
								
									ae119df026
								
							
						
					
					
						commit
						a2dfcebf3f
					
				
					 7 changed files with 92 additions and 37 deletions
				
			
		| 
						 | 
				
			
			@ -9,13 +9,13 @@ using namespace api::v1;
 | 
			
		|||
 | 
			
		||||
void devices::post_discover(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&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::function<void(const
 | 
			
		|||
 | 
			
		||||
    if(helpers::send_udp_broadcast("255.255.255.255", config::discover_port_dev, Json::writeString(json_writer, payload).c_str()) < 0)
 | 
			
		||||
    {
 | 
			
		||||
        auto resp = HttpResponse::newHttpResponse();
 | 
			
		||||
        resp->setStatusCode(k500InternalServerError);
 | 
			
		||||
        callback(resp);
 | 
			
		||||
        return;
 | 
			
		||||
| 
						 | 
				
			
			@ -42,7 +43,7 @@ void devices::post_discover(const HttpRequestPtr &req, std::function<void(const
 | 
			
		|||
    uint8_t discover_answer_buf[1];
 | 
			
		||||
    uint8_t discover_header_buf[1];
 | 
			
		||||
 | 
			
		||||
    device_dbo **all_devices = device_dbo::get_all();
 | 
			
		||||
    device_dbo **known_devices = device_dbo::get_all();
 | 
			
		||||
 | 
			
		||||
    while(true)
 | 
			
		||||
    {
 | 
			
		||||
| 
						 | 
				
			
			@ -98,26 +99,28 @@ void devices::post_discover(const HttpRequestPtr &req, std::function<void(const
 | 
			
		|||
 | 
			
		||||
            json_reader.parse(answer_payload, &answer_payload[payload_length], client_info, false);
 | 
			
		||||
 | 
			
		||||
            free(answer_payload);
 | 
			
		||||
 | 
			
		||||
            const char *discovered_id = client_info["id"].asCString();
 | 
			
		||||
 | 
			
		||||
            bool found_discovered_in_list = false;
 | 
			
		||||
 | 
			
		||||
            for(int i = 0; all_devices[i] != nullptr; i++)
 | 
			
		||||
            for(int i = 0; known_devices[i] != nullptr; i++)
 | 
			
		||||
            {
 | 
			
		||||
                if(!found_discovered_in_list)
 | 
			
		||||
                {
 | 
			
		||||
                    if(strcmp(all_devices[i]->id, 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::function<void(const
 | 
			
		|||
            close(client_fd);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    for(int i = 0; known_devices[i] != nullptr; i++)
 | 
			
		||||
    {
 | 
			
		||||
        known_devices[i]->active = 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);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -61,7 +61,7 @@ schedules::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const
 | 
			
		|||
 | 
			
		||||
        callback(resp);
 | 
			
		||||
 | 
			
		||||
        free(schedule);
 | 
			
		||||
        delete schedule;
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
| 
						 | 
				
			
			@ -88,11 +88,16 @@ schedules::post_new(const HttpRequestPtr &req, std::function<void(const HttpResp
 | 
			
		|||
    new_schedule.id[32] = '\0';
 | 
			
		||||
    new_schedule.periods = periods;
 | 
			
		||||
 | 
			
		||||
    new_schedule.insert();
 | 
			
		||||
 | 
			
		||||
    auto resp = HttpResponse::newHttpResponse();
 | 
			
		||||
    callback(resp);
 | 
			
		||||
 | 
			
		||||
    delete periods;
 | 
			
		||||
    if(!new_schedule.insert())
 | 
			
		||||
    {
 | 
			
		||||
        auto resp = HttpResponse::newHttpResponse();
 | 
			
		||||
        resp->setStatusCode(k500InternalServerError);
 | 
			
		||||
        callback(resp);
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        auto resp = HttpResponse::newHttpJsonResponse(new_schedule.to_json());
 | 
			
		||||
        callback(resp);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										18
									
								
								main.cc
									
										
									
									
									
								
							
							
						
						
									
										18
									
								
								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 */
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -24,6 +24,8 @@ public:
 | 
			
		|||
    bool
 | 
			
		||||
    remove();
 | 
			
		||||
 | 
			
		||||
    ~schedule_dbo();
 | 
			
		||||
 | 
			
		||||
    Json::Value
 | 
			
		||||
    to_json();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue