Fix more clang-tidy warnings

This commit is contained in:
Tobias Reisinger 2020-11-14 00:34:20 +01:00
parent 5747a65664
commit c7cafa94c4
21 changed files with 178 additions and 208 deletions

View file

@ -1,2 +1,2 @@
--- ---
Checks: 'clang-diagnostics-*,clang-analyzer-*,linuxkernel-*,modernize-*,readability-*' Checks: 'clang-diagnostics-*,clang-analyzer-*,linuxkernel-*,modernize-*,readability-*,-readability-magic-numbers,portability-*'

View file

@ -21,10 +21,10 @@ void
database_transaction_begin(database_transaction_lock *lock); database_transaction_begin(database_transaction_lock *lock);
void void
database_transaction_commit(database_transaction_lock *lock); database_transaction_commit(const database_transaction_lock *lock);
void void
database_transaction_rollback(database_transaction_lock *lock); database_transaction_rollback(const database_transaction_lock *lock);
int int

View file

@ -25,16 +25,16 @@ typedef struct
} controller_t; } controller_t;
void void
controller_free(controller_t* contoller); controller_free(controller_t *controller);
int int
controller_save(controller_t* contoller); controller_save(controller_t *controller);
int int
controller_remove(controller_t* contoller); controller_remove(controller_t *controller);
cJSON* cJSON*
controller_to_json(controller_t* contoller); controller_to_json(controller_t *controller);
controller_t* controller_t*
controller_get_by_id(int id); controller_get_by_id(int id);

View file

@ -30,9 +30,6 @@ macro_free_list(macro_t **macro);
cJSON* cJSON*
macro_to_json(macro_t *macro); macro_to_json(macro_t *macro);
void
macro_free_list(macro_t **macros_list);
macro_t* macro_t*
macro_get_by_id(int id); macro_get_by_id(int id);

View file

@ -35,9 +35,6 @@ schedule_free_list(schedule_t **schedule);
cJSON* cJSON*
schedule_to_json(schedule_t *schedule); schedule_to_json(schedule_t *schedule);
void
schedule_free_list(schedule_t **schedules_list);
uint16_t* uint16_t*
schedule_periods_to_blob(schedule_t *schedule); schedule_periods_to_blob(schedule_t *schedule);

View file

@ -42,11 +42,9 @@ cache_get_value(char *key)
{ {
break; break;
} }
else
{ LOGGER_WARNING("failed selecting %s from cache: %s\n", key, sqlite3_errstr(s));
LOGGER_WARNING("failed selecting %s from cache: %s\n", key, sqlite3_errstr(s)); break;
break;
}
} }
} }

View file

@ -47,5 +47,4 @@ cli_parse(int argc, const char **argv, config_t *config)
printf("%s\n", EMGAUWA_CORE_VERSION); printf("%s\n", EMGAUWA_CORE_VERSION);
exit(0); exit(0);
} }
return;
} }

View file

@ -90,8 +90,6 @@ database_migrate()
default: default:
break; break;
} }
return;
} }
void void
@ -106,7 +104,7 @@ database_transaction_begin(database_transaction_lock *lock)
} }
void void
database_transaction_commit(database_transaction_lock *lock) database_transaction_commit(const database_transaction_lock *lock)
{ {
if(transaction_lock == lock) if(transaction_lock == lock)
{ {
@ -117,7 +115,7 @@ database_transaction_commit(database_transaction_lock *lock)
} }
void void
database_transaction_rollback(database_transaction_lock *lock) database_transaction_rollback(const database_transaction_lock *lock)
{ {
if(transaction_lock == lock) if(transaction_lock == lock)
{ {
@ -147,12 +145,10 @@ database_helper_get_id(sqlite3_stmt *stmt)
{ {
break; break;
} }
else
{ LOGGER_ERR("error selecting id from database: %s\n", sqlite3_errstr(s));
LOGGER_ERR("error selecting id from database: %s\n", sqlite3_errstr(s)); sqlite3_finalize(stmt);
sqlite3_finalize(stmt); return 0;
return 0;
}
} }
} }
@ -191,16 +187,14 @@ database_helper_get_ids(sqlite3_stmt *stmt)
{ {
break; break;
} }
else
if(result)
{ {
if(result) free(result);
{
free(result);
}
LOGGER_ERR("error selecting ids from database: %s\n", sqlite3_errstr(s));
sqlite3_finalize(stmt);
return NULL;
} }
LOGGER_ERR("error selecting ids from database: %s\n", sqlite3_errstr(s));
sqlite3_finalize(stmt);
return NULL;
} }
} }
@ -238,16 +232,14 @@ database_helper_get_string(sqlite3_stmt *stmt)
{ {
break; break;
} }
else
if(result)
{ {
if(result) free(result);
{
free(result);
}
LOGGER_ERR("error selecting string from database: %s\n", sqlite3_errstr(s));
sqlite3_finalize(stmt);
return NULL;
} }
LOGGER_ERR("error selecting string from database: %s\n", sqlite3_errstr(s));
sqlite3_finalize(stmt);
return NULL;
} }
} }
@ -284,11 +276,9 @@ database_helper_get_strings(sqlite3_stmt *stmt)
{ {
break; break;
} }
else
{ LOGGER_ERR("error selecting strings from database: %s\n", sqlite3_errstr(s));
LOGGER_ERR("error selecting strings from database: %s\n", sqlite3_errstr(s)); break;
break;
}
} }
} }
sqlite3_finalize(stmt); sqlite3_finalize(stmt);

View file

@ -168,5 +168,4 @@ api_v1_controllers_STR_DELETE(struct mg_connection *nc, struct http_message *hm,
M_RESPONSE_MSG(LOGGER_DEBUG, response, 200, "deleted controller"); M_RESPONSE_MSG(LOGGER_DEBUG, response, 200, "deleted controller");
} }
controller_free(controller); controller_free(controller);
return;
} }

View file

@ -21,7 +21,8 @@ typedef enum
static int static int
bind_tcp_server(const char *addr, const char *port, int max_client_backlog) bind_tcp_server(const char *addr, const char *port, int max_client_backlog)
{ {
struct addrinfo hints, *res; struct addrinfo hints;
struct addrinfo *res;
int fd; int fd;
int status; int status;
@ -132,7 +133,8 @@ api_v1_controllers_discover_PUT(struct mg_connection *nc, struct http_message *h
struct sockaddr_storage their_addr; struct sockaddr_storage their_addr;
socklen_t addr_size; socklen_t addr_size;
int client_fd, s_ret; int s_ret;
int client_fd;
fd_set accept_fds; fd_set accept_fds;
struct timeval timeout; struct timeval timeout;
@ -155,143 +157,141 @@ api_v1_controllers_discover_PUT(struct mg_connection *nc, struct http_message *h
{ {
break; break;
} }
else
if((client_fd = accept(discover_server_socket, (struct sockaddr *) &their_addr, &addr_size)) < 0)
{ {
if((client_fd = accept(discover_server_socket, (struct sockaddr *) &their_addr, &addr_size)) < 0) LOGGER_ERR("error accepting client %s\n", strerror(errno));
continue;
}
size_t payload_length;
if(recv(client_fd, &payload_length, sizeof(payload_length), 0) <= 0)
{
LOGGER_ERR("error receiving header from client\n");
continue;
}
char *answer_payload = (char*)malloc((payload_length));
if(recv(client_fd, answer_payload, payload_length, 0) <= 0)
{
LOGGER_ERR("error receiving payload from client\n");
continue;
}
struct sockaddr_in addr;
socklen_t client_addr_size = sizeof(struct sockaddr_in);
if(getpeername(client_fd, (struct sockaddr *)&addr, &client_addr_size) != 0)
{
LOGGER_ERR("error receiving payload from client\n");
continue;
}
LOGGER_DEBUG("received info for discovered controller\n");
uuid_t discovered_id;
mpack_tree_t tree;
mpack_tree_init_data(&tree, answer_payload, payload_length);
mpack_tree_parse(&tree);
mpack_node_t root = mpack_tree_root(&tree);
memcpy(discovered_id, mpack_node_data(mpack_node_map_uint(root, DISCOVERY_MAPPING_ID)), sizeof(uuid_t));
uint16_t discovered_command_port = mpack_node_u16(mpack_node_map_uint(root, DISCOVERY_MAPPING_COMMAND_PORT));
uint8_t discovered_relay_count = mpack_node_u8(mpack_node_map_uint(root, DISCOVERY_MAPPING_RELAY_COUNT));
const char *discovered_name = mpack_node_str(mpack_node_map_uint(root, DISCOVERY_MAPPING_NAME));
size_t discovered_name_len = mpack_node_strlen(mpack_node_map_uint(root, DISCOVERY_MAPPING_NAME));
if(discovered_name_len > MAX_NAME_LENGTH)
{
discovered_name_len = MAX_NAME_LENGTH;
}
bool found_discovered_in_list = 0;
for(int i = 0; known_controllers[i] != NULL; i++)
{
if(!found_discovered_in_list)
{ {
LOGGER_ERR("error accepting client %s\n", strerror(errno)); if(uuid_compare(known_controllers[i]->uid, discovered_id) == 0)
continue;
}
size_t payload_length;
if(recv(client_fd, &payload_length, sizeof(payload_length), 0) <= 0)
{
LOGGER_ERR("error receiving header from client\n");
continue;
}
char *answer_payload = (char*)malloc((payload_length));
if(recv(client_fd, answer_payload, payload_length, 0) <= 0)
{
LOGGER_ERR("error receiving payload from client\n");
continue;
}
struct sockaddr_in addr;
socklen_t client_addr_size = sizeof(struct sockaddr_in);
if(getpeername(client_fd, (struct sockaddr *)&addr, &client_addr_size) != 0)
{
LOGGER_ERR("error receiving payload from client\n");
continue;
}
LOGGER_DEBUG("received info for discovered controller\n");
uuid_t discovered_id;
mpack_tree_t tree;
mpack_tree_init_data(&tree, answer_payload, payload_length);
mpack_tree_parse(&tree);
mpack_node_t root = mpack_tree_root(&tree);
memcpy(discovered_id, mpack_node_data(mpack_node_map_uint(root, DISCOVERY_MAPPING_ID)), sizeof(uuid_t));
uint16_t discovered_command_port = mpack_node_u16(mpack_node_map_uint(root, DISCOVERY_MAPPING_COMMAND_PORT));
uint8_t discovered_relay_count = mpack_node_u8(mpack_node_map_uint(root, DISCOVERY_MAPPING_RELAY_COUNT));
const char *discovered_name = mpack_node_str(mpack_node_map_uint(root, DISCOVERY_MAPPING_NAME));
size_t discovered_name_len = mpack_node_strlen(mpack_node_map_uint(root, DISCOVERY_MAPPING_NAME));
if(discovered_name_len > MAX_NAME_LENGTH)
{
discovered_name_len = MAX_NAME_LENGTH;
}
bool found_discovered_in_list = 0;
for(int i = 0; known_controllers[i] != NULL; i++)
{
if(!found_discovered_in_list)
{ {
if(uuid_compare(known_controllers[i]->uid, discovered_id) == 0) LOGGER_DEBUG("rediscovered a known controller at %s\n", inet_ntoa(addr.sin_addr));
{
LOGGER_DEBUG("rediscovered a known controller at %s\n", inet_ntoa(addr.sin_addr));
known_controllers[i]->active = 1; known_controllers[i]->active = 1;
strlcpy(known_controllers[i]->name, discovered_name, discovered_name_len + 1); strlcpy(known_controllers[i]->name, discovered_name, discovered_name_len + 1);
strlcpy(known_controllers[i]->ip, inet_ntoa(addr.sin_addr), IP_LENGTH + 1); strlcpy(known_controllers[i]->ip, inet_ntoa(addr.sin_addr), IP_LENGTH + 1);
known_controllers[i]->name[discovered_name_len] = '\0'; known_controllers[i]->name[discovered_name_len] = '\0';
known_controllers[i]->port = discovered_command_port; known_controllers[i]->port = discovered_command_port;
known_controllers[i]->relay_count = discovered_relay_count; known_controllers[i]->relay_count = discovered_relay_count;
controller_save(known_controllers[i]); controller_save(known_controllers[i]);
controller_free(known_controllers[i]); controller_free(known_controllers[i]);
found_discovered_in_list = 1; found_discovered_in_list = 1;
known_controllers[i] = known_controllers[i + 1];
}
}
else
{
known_controllers[i] = known_controllers[i + 1]; known_controllers[i] = known_controllers[i + 1];
} }
} }
else
if(!found_discovered_in_list)
{ {
LOGGER_DEBUG("discovered a new controller at %s\n", inet_ntoa(addr.sin_addr)); known_controllers[i] = known_controllers[i + 1];
controller_t *discovered_controller = malloc(sizeof(controller_t));
discovered_controller->id = 0;
strlcpy(discovered_controller->ip, inet_ntoa(addr.sin_addr), IP_LENGTH + 1);
uuid_copy(discovered_controller->uid, discovered_id);
strncpy(discovered_controller->name, discovered_name, discovered_name_len);
discovered_controller->name[discovered_name_len] = '\0';
discovered_controller->relay_count = discovered_relay_count;
discovered_controller->port = discovered_command_port;
discovered_controller->active = 1;
controller_save(discovered_controller);
// TODO get relays during discovery and don't create empty ones
relay_t **discovered_relays = malloc(sizeof(relay_t*) * (discovered_controller->relay_count + 1));
for(int i = 0; i < discovered_controller->relay_count; ++i)
{
relay_t *new_relay = malloc(sizeof(relay_t));
new_relay->id = 0;
sprintf(new_relay->name, "Relay %d", i + 1);
new_relay->number = i;
new_relay->controller_id = discovered_controller->id;
uuid_t tmp_uuid;
memset(tmp_uuid, 0, sizeof(uuid_t));
memcpy(tmp_uuid, "off", 3);
for(int i = 0; i < 7; ++i)
{
new_relay->schedules[i] = schedule_get_by_uid(tmp_uuid);
}
time_t timestamp = time(NULL);
struct tm *time_struct = localtime(&timestamp);
new_relay->active_schedule = new_relay->schedules[helper_get_weekday(time_struct)];
relay_save(new_relay);
discovered_relays[i] = new_relay;
}
discovered_relays[discovered_controller->relay_count] = NULL;
discovered_controller->relays = discovered_relays;
controller_free(discovered_controller);
} }
mpack_tree_destroy(&tree);
free(answer_payload);
discover_answer_buf[0] = 0; // TODO add discovery return codes
send(client_fd, discover_answer_buf, sizeof(uint8_t), 0);
close(client_fd);
} }
if(!found_discovered_in_list)
{
LOGGER_DEBUG("discovered a new controller at %s\n", inet_ntoa(addr.sin_addr));
controller_t *discovered_controller = malloc(sizeof(controller_t));
discovered_controller->id = 0;
strlcpy(discovered_controller->ip, inet_ntoa(addr.sin_addr), IP_LENGTH + 1);
uuid_copy(discovered_controller->uid, discovered_id);
strncpy(discovered_controller->name, discovered_name, discovered_name_len);
discovered_controller->name[discovered_name_len] = '\0';
discovered_controller->relay_count = discovered_relay_count;
discovered_controller->port = discovered_command_port;
discovered_controller->active = 1;
controller_save(discovered_controller);
// TODO get relays during discovery and don't create empty ones
relay_t **discovered_relays = malloc(sizeof(relay_t*) * (discovered_controller->relay_count + 1));
for(int i = 0; i < discovered_controller->relay_count; ++i)
{
relay_t *new_relay = malloc(sizeof(relay_t));
new_relay->id = 0;
sprintf(new_relay->name, "Relay %d", i + 1);
new_relay->number = i;
new_relay->controller_id = discovered_controller->id;
uuid_t tmp_uuid;
memset(tmp_uuid, 0, sizeof(uuid_t));
memcpy(tmp_uuid, "off", 3);
for(int i = 0; i < 7; ++i)
{
new_relay->schedules[i] = schedule_get_by_uid(tmp_uuid);
}
time_t timestamp = time(NULL);
struct tm *time_struct = localtime(&timestamp);
new_relay->active_schedule = new_relay->schedules[helper_get_weekday(time_struct)];
relay_save(new_relay);
discovered_relays[i] = new_relay;
}
discovered_relays[discovered_controller->relay_count] = NULL;
discovered_controller->relays = discovered_relays;
controller_free(discovered_controller);
}
mpack_tree_destroy(&tree);
free(answer_payload);
discover_answer_buf[0] = 0; // TODO add discovery return codes
send(client_fd, discover_answer_buf, sizeof(uint8_t), 0);
close(client_fd);
} }
for(int i = 0; known_controllers[i] != NULL; i++) for(int i = 0; known_controllers[i] != NULL; i++)
{ {

View file

@ -277,5 +277,4 @@ api_v1_macros_STR_DELETE(struct mg_connection *nc, struct http_message *hm, endp
M_RESPONSE_MSG(LOGGER_DEBUG, response, 200, "deleted macro"); M_RESPONSE_MSG(LOGGER_DEBUG, response, 200, "deleted macro");
} }
macro_free(macro); macro_free(macro);
return;
} }

View file

@ -204,5 +204,4 @@ api_v1_schedules_STR_DELETE(struct mg_connection *nc, struct http_message *hm, e
M_RESPONSE_MSG(LOGGER_DEBUG, response, 200, "the target schedule got deleted"); M_RESPONSE_MSG(LOGGER_DEBUG, response, 200, "the target schedule got deleted");
} }
schedule_free(schedule); schedule_free(schedule);
return;
} }

View file

@ -95,5 +95,4 @@ api_v1_tags_POST(struct mg_connection *nc, struct http_message *hm, endpoint_arg
} }
cJSON_Delete(json); cJSON_Delete(json);
return;
} }

View file

@ -118,11 +118,9 @@ handle_http_request(struct mg_connection *nc, struct http_message *hm)
LOGGER_DEBUG("serving %.*s\n", hm->uri.len, hm->uri.p); LOGGER_DEBUG("serving %.*s\n", hm->uri.len, hm->uri.p);
return; return;
} }
else
{ LOGGER_DEBUG("serving 'not found'\n");
LOGGER_DEBUG("serving 'not found'\n"); endpoint = router_get_not_found_endpoint();
endpoint = router_get_not_found_endpoint();
}
} }
if(endpoint->method == HTTP_METHOD_OPTIONS) if(endpoint->method == HTTP_METHOD_OPTIONS)

View file

@ -9,7 +9,7 @@
#include <models/relay.h> #include <models/relay.h>
static void static void
handle_mqtt_publish_controller(char **topic_save, int controller_id, char *payload) handle_mqtt_publish_controller(char **topic_save, int controller_id, const char *payload)
{ {
(void)controller_id; (void)controller_id;
(void)payload; (void)payload;

View file

@ -13,8 +13,11 @@ helper_connect_tcp_server(char* host, uint16_t port)
char port_str[6]; char port_str[6];
sprintf(port_str, "%d", port); sprintf(port_str, "%d", port);
int s, status; int s;
struct addrinfo hints, *res; int status;
struct addrinfo *res;
struct addrinfo hints;
memset(&hints, 0, sizeof hints); memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET; //set IP Protocol flag (IPv4 or IPv6 - we don't care) hints.ai_family = AF_INET; //set IP Protocol flag (IPv4 or IPv6 - we don't care)
hints.ai_socktype = SOCK_STREAM; //set socket flag hints.ai_socktype = SOCK_STREAM; //set socket flag

View file

@ -105,11 +105,9 @@ controller_db_select(sqlite3_stmt *stmt)
{ {
break; break;
} }
else
{ LOGGER_ERR("error selecting controllers from database: %s\n", sqlite3_errstr(s));
LOGGER_ERR("error selecting controllers from database: %s\n", sqlite3_errstr(s)); break;
break;
}
} }
} }
sqlite3_finalize(stmt); sqlite3_finalize(stmt);

View file

@ -82,11 +82,9 @@ macro_db_select(sqlite3_stmt *stmt)
{ {
break; break;
} }
else
{ LOGGER_ERR("error selecting macros from database: %s\n", sqlite3_errstr(s));
LOGGER_ERR("error selecting macros from database: %s\n", sqlite3_errstr(s)); break;
break;
}
} }
} }
sqlite3_finalize(stmt); sqlite3_finalize(stmt);

View file

@ -61,11 +61,9 @@ macro_action_db_select(sqlite3_stmt *stmt)
{ {
break; break;
} }
else
{ LOGGER_ERR("error selecting macro_actions from database: %s\n", sqlite3_errstr(s));
LOGGER_ERR("error selecting macro_actions from database: %s\n", sqlite3_errstr(s)); break;
break;
}
} }
} }
sqlite3_finalize(stmt); sqlite3_finalize(stmt);

View file

@ -27,9 +27,8 @@ period_helper_parse_hhmm(const char *hhmm_str, uint16_t *hhmm)
} }
} }
uint16_t tmp_h, tmp_m; uint16_t tmp_h = (uint16_t)strtol(&hhmm_str[0], NULL, 10);
tmp_h = (uint16_t)strtol(&hhmm_str[0], NULL, 10); uint16_t tmp_m = (uint16_t)strtol(&hhmm_str[3], NULL, 10);
tmp_m = (uint16_t)strtol(&hhmm_str[3], NULL, 10);
if(tmp_h > 24 || tmp_m >= 60) if(tmp_h > 24 || tmp_m >= 60)
{ {

View file

@ -314,7 +314,6 @@ router_find_endpoint(const char *uri_str, size_t uri_len, struct mg_str *method_
continue; continue;
} }
endpoints[i].possible_route = 0; endpoints[i].possible_route = 0;
continue;
} }
uri_token = strtok_r(NULL, delimiter, &uri_token_save); uri_token = strtok_r(NULL, delimiter, &uri_token_save);
++route_part; ++route_part;