Remove libbsd and strlcpy

This commit is contained in:
Tobias Reisinger 2020-11-19 23:36:02 +01:00
parent 3f11431df6
commit 1f315d2b92
15 changed files with 53 additions and 50 deletions

View file

@ -7,7 +7,6 @@ add_executable(core src/main.c)
target_link_libraries(core -lsqlite3) target_link_libraries(core -lsqlite3)
target_link_libraries(core -luuid) target_link_libraries(core -luuid)
target_link_libraries(core -lbsd)
set(CMAKE_C_FLAGS "$ENV{CFLAGS}") set(CMAKE_C_FLAGS "$ENV{CFLAGS}")
set(CMAKE_C_FLAGS "-D'__FILENAME__=\"$(subst $(realpath ${CMAKE_SOURCE_DIR}/src/)/,,$(abspath $<))\"'") set(CMAKE_C_FLAGS "-D'__FILENAME__=\"$(subst $(realpath ${CMAKE_SOURCE_DIR}/src/)/,,$(abspath $<))\"'")

View file

@ -1,5 +1,3 @@
#include <bsd/string.h>
#include <cache.h> #include <cache.h>
#include <logger.h> #include <logger.h>
#include <models/junction_relay_schedule.h> #include <models/junction_relay_schedule.h>
@ -34,7 +32,8 @@ cache_get_value(char *key)
free(result); free(result);
} }
result = (char*)malloc(sizeof(char) * (found_value_len + 1)); result = (char*)malloc(sizeof(char) * (found_value_len + 1));
strlcpy(result, found_value, found_value_len + 1); strncpy(result, found_value, found_value_len);
result[found_value_len] = '\0';
} }
else else
{ {

View file

@ -1,4 +1,3 @@
#include <bsd/string.h>
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
@ -245,7 +244,8 @@ config_load_string(char **holder, const char *value)
size_t value_len = strlen(value); size_t value_len = strlen(value);
char *new_holder = malloc(sizeof(char) * (value_len + 1)); char *new_holder = malloc(sizeof(char) * (value_len + 1));
strlcpy(new_holder, value, value_len + 1); strcpy(new_holder, value);
new_holder[value_len] = '\0';
*holder = new_holder; *holder = new_holder;
} }
@ -355,16 +355,18 @@ config_load_directory(config_t *config, const char *directory_name)
size_t copied = 0; size_t copied = 0;
// Add 2 for '/' and '\0'. // Add 2 for '/' and '\0'.
size_t path_size = strlen(directory_name) + strlen(entry_name) + 2; size_t path_len = strlen(directory_name) + strlen(entry_name) + 1;
char *path = malloc(sizeof(char) * path_size); char *path = malloc(sizeof(char) * (path_len + 1));
path[0] = '\0'; path[0] = '\0';
copied += strlcat(path + copied, directory_name, path_size - copied); strncat(path + copied, directory_name, path_len - copied);
copied = strlen(path);
if(path[copied - 1] != '/') if(path[copied - 1] != '/')
{ {
copied += strlcat(path + copied, "/", path_size - copied); strncat(path + copied, "/", path_len - copied);
copied = strlen(path);
} }
copied += strlcat(path + copied, entry_name, path_size - copied); strncat(path + copied, entry_name, path_len - copied);
if(stat(path, &sb)) if(stat(path, &sb))
{ {

View file

@ -1,4 +1,3 @@
#include <bsd/string.h>
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
@ -224,7 +223,8 @@ database_helper_get_string(sqlite3_stmt *stmt)
free(result); free(result);
} }
result = (char*)malloc(sizeof(char) * (found_string_len + 1)); result = (char*)malloc(sizeof(char) * (found_string_len + 1));
strlcpy(result, found_string, found_string_len + 1); strncpy(result, found_string, found_string_len);
result[found_string_len] = '\0';
} }
else else
{ {
@ -268,7 +268,8 @@ database_helper_get_strings(sqlite3_stmt *stmt)
result = (char**)realloc(result, sizeof(char*) * (row + 1)); result = (char**)realloc(result, sizeof(char*) * (row + 1));
result[row - 1] = malloc(sizeof(char) * (new_string_len + 1)); result[row - 1] = malloc(sizeof(char) * (new_string_len + 1));
strlcpy(result[row - 1], new_string, new_string_len + 1); strncpy(result[row - 1], new_string, new_string_len);
result[new_string_len] = '\0';
} }
else else
{ {

View file

@ -1,5 +1,3 @@
#include <bsd/string.h>
#include <logger.h> #include <logger.h>
#include <cJSON.h> #include <cJSON.h>
#include <mongoose.h> #include <mongoose.h>
@ -100,10 +98,9 @@ endpoint_response_text(endpoint_response_t *response, int status_code, const cha
response->content_length = strlen(content); response->content_length = strlen(content);
response->alloced_content = true; response->alloced_content = true;
int content_size = response->content_length + 1; response->content = malloc(sizeof(char) * (response->content_length + 1));
strcpy(response->content, content);
response->content = malloc(sizeof(char) * content_size); response->content[response->content_length] = '\0';
strlcpy(response->content, content, content_size);
} }
} }

View file

@ -1,5 +1,3 @@
#include <bsd/string.h>
#include <cJSON.h> #include <cJSON.h>
#include <macros.h> #include <macros.h>
#include <constants.h> #include <constants.h>
@ -220,9 +218,12 @@ api_v1_controllers_discover_PUT(struct mg_connection *nc, struct http_message *h
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); strncpy(known_controllers[i]->name, discovered_name, discovered_name_len);
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';
strncpy(known_controllers[i]->ip, inet_ntoa(addr.sin_addr), IP_LENGTH);
known_controllers[i]->ip[IP_LENGTH] = '\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;
@ -245,7 +246,10 @@ api_v1_controllers_discover_PUT(struct mg_connection *nc, struct http_message *h
controller_t *discovered_controller = malloc(sizeof(controller_t)); controller_t *discovered_controller = malloc(sizeof(controller_t));
discovered_controller->id = 0; discovered_controller->id = 0;
strlcpy(discovered_controller->ip, inet_ntoa(addr.sin_addr), IP_LENGTH + 1);
strncpy(discovered_controller->ip, inet_ntoa(addr.sin_addr), IP_LENGTH + 1);
discovered_controller->ip[IP_LENGTH] = '\0';
uuid_copy(discovered_controller->uid, discovered_id); uuid_copy(discovered_controller->uid, discovered_id);
strncpy(discovered_controller->name, discovered_name, discovered_name_len); strncpy(discovered_controller->name, discovered_name, discovered_name_len);
discovered_controller->name[discovered_name_len] = '\0'; discovered_controller->name[discovered_name_len] = '\0';

View file

@ -1,5 +1,3 @@
#include <bsd/string.h>
#include <cJSON.h> #include <cJSON.h>
#include <macros.h> #include <macros.h>
#include <constants.h> #include <constants.h>
@ -87,7 +85,8 @@ api_v1_tags_POST(struct mg_connection *nc, struct http_message *hm, endpoint_arg
// NOLINT(clang-analyzer-unix.Malloc): The endpoint response will be freed later. // NOLINT(clang-analyzer-unix.Malloc): The endpoint response will be freed later.
char *tag = malloc(sizeof(char) * (tag_len + 1)); char *tag = malloc(sizeof(char) * (tag_len + 1));
strlcpy(tag, json_tag->valuestring, tag_len + 1); strcpy(tag, json_tag->valuestring);
tag[tag_len] = '\0';
endpoint_response_text(response, 201, tag, 0); endpoint_response_text(response, 201, tag, 0);

View file

@ -1,4 +1,3 @@
#include <bsd/string.h>
#include <string.h> #include <string.h>
#include <status.h> #include <status.h>
@ -22,7 +21,8 @@ add_extra_headers(char *extra_headers)
if(extra_headers == NULL) if(extra_headers == NULL)
{ {
result = malloc(sizeof(char) * (std_headers_len + 1)); result = malloc(sizeof(char) * (std_headers_len + 1));
strlcpy(result, global_config->http_server_opts.extra_headers, std_headers_len + 1); strcpy(result, global_config->http_server_opts.extra_headers);
result[std_headers_len] = '\0';
return result; return result;
} }

View file

@ -1,4 +1,3 @@
#include <bsd/string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sqlite3.h> #include <sqlite3.h>
@ -53,14 +52,16 @@ controller_db_select_mapper(sqlite3_stmt *stmt)
new_controller->id = sqlite3_column_int(stmt, i); new_controller->id = sqlite3_column_int(stmt, i);
break; break;
case 'p': // ip case 'p': // ip
strlcpy(new_controller->ip, (const char*)sqlite3_column_text(stmt, i), sizeof(new_controller->ip)); strncpy(new_controller->ip, (const char*)sqlite3_column_text(stmt, i), IP_LENGTH);
new_controller->ip[IP_LENGTH] = '\0';
break; break;
default: // ignore columns not implemented default: // ignore columns not implemented
break; break;
} }
break; break;
case 'n': // name case 'n': // name
strlcpy(new_controller->name, (const char*)sqlite3_column_text(stmt, i), sizeof(new_controller->name)); strncpy(new_controller->name, (const char*)sqlite3_column_text(stmt, i), MAX_NAME_LENGTH);
new_controller->name[MAX_NAME_LENGTH] = '\0';
break; break;
case 'p': // port case 'p': // port
new_controller->port = sqlite3_column_int(stmt, i); new_controller->port = sqlite3_column_int(stmt, i);

View file

@ -1,4 +1,3 @@
#include <bsd/string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
@ -43,7 +42,8 @@ junction_relay_schedule_insert_weekdays(int relay_id, int *schedule_ids)
size_t query_len = M_STRLEN(query_base) + (7 * (M_STRLEN(query_extender) + 1)) + 1; size_t query_len = M_STRLEN(query_base) + (7 * (M_STRLEN(query_extender) + 1)) + 1;
char *query = malloc(sizeof(char) * query_len + 1); char *query = malloc(sizeof(char) * query_len + 1);
strlcpy(query, query_base, query_len + 1); strncpy(query, query_base, query_len);
query[query_len] = '\0';
query_len -= M_STRLEN(query_base); query_len -= M_STRLEN(query_base);
for(int i = 0; i < 7; ++i) for(int i = 0; i < 7; ++i)

View file

@ -1,4 +1,3 @@
#include <bsd/string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stddef.h> #include <stddef.h>
@ -69,8 +68,10 @@ junction_tag_insert_list(int *tag_ids, int relay_id, int schedule_id, int count)
static const char query_extender[] = " (?, ?, ?)"; static const char query_extender[] = " (?, ?, ?)";
size_t query_len = M_STRLEN(query_base) + (count * (M_STRLEN(query_extender) + 1)) + 1; size_t query_len = M_STRLEN(query_base) + (count * (M_STRLEN(query_extender) + 1)) + 1;
char *query = malloc(sizeof(char) * query_len + 1); char *query = malloc(sizeof(char) * (query_len + 1));
strlcpy(query, query_base, query_len + 1); strncpy(query, query_base, query_len);
query[query_len] = '\0';
query_len -= M_STRLEN(query_base); query_len -= M_STRLEN(query_base);
for(int i = 0; i < count; ++i) for(int i = 0; i < count; ++i)
{ {

View file

@ -1,4 +1,3 @@
#include <bsd/string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sqlite3.h> #include <sqlite3.h>
@ -43,7 +42,7 @@ macro_db_select_mapper(sqlite3_stmt *stmt)
new_macro->id = sqlite3_column_int(stmt, i); new_macro->id = sqlite3_column_int(stmt, i);
break; break;
case 'n': // name case 'n': // name
strlcpy(new_macro->name, (const char*)sqlite3_column_text(stmt, i), sizeof(new_macro->name)); strncpy(new_macro->name, (const char*)sqlite3_column_text(stmt, i), MAX_NAME_LENGTH);
new_macro->name[MAX_NAME_LENGTH] = '\0'; new_macro->name[MAX_NAME_LENGTH] = '\0';
break; break;
case 'u': // uid case 'u': // uid

View file

@ -1,4 +1,3 @@
#include <bsd/string.h>
#include <sqlite3.h> #include <sqlite3.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -55,7 +54,8 @@ relay_db_select_mapper(sqlite3_stmt *stmt)
switch(name[1]) switch(name[1])
{ {
case 'a': // name case 'a': // name
strlcpy(new_relay->name, (const char*)sqlite3_column_text(stmt, i), sizeof(new_relay->name)); strncpy(new_relay->name, (const char*)sqlite3_column_text(stmt, i), MAX_NAME_LENGTH);
new_relay->name[MAX_NAME_LENGTH] = '\0';
break; break;
case 'u': // number case 'u': // number
new_relay->number = sqlite3_column_int(stmt, i); new_relay->number = sqlite3_column_int(stmt, i);

View file

@ -1,4 +1,3 @@
#include <bsd/string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sqlite3.h> #include <sqlite3.h>
@ -49,7 +48,7 @@ schedule_db_select_mapper(sqlite3_stmt *stmt)
new_schedule->id = sqlite3_column_int(stmt, i); new_schedule->id = sqlite3_column_int(stmt, i);
break; break;
case 'n': // name case 'n': // name
strlcpy(new_schedule->name, (const char*)sqlite3_column_text(stmt, i), sizeof(new_schedule->name)); strncpy(new_schedule->name, (const char*)sqlite3_column_text(stmt, i), MAX_NAME_LENGTH);
new_schedule->name[MAX_NAME_LENGTH] = '\0'; new_schedule->name[MAX_NAME_LENGTH] = '\0';
break; break;
case 'p': // periods case 'p': // periods
@ -481,14 +480,16 @@ schedule_uid_unparse(const uuid_t uid, char *result)
schedule_get_uid_off(tmp_uid); schedule_get_uid_off(tmp_uid);
if(uuid_compare(uid, tmp_uid) == 0) if(uuid_compare(uid, tmp_uid) == 0)
{ {
strlcpy(result, "off", 4); strncpy(result, "off", 4);
result[3] = '\0';
return; return;
} }
schedule_get_uid_on(tmp_uid); schedule_get_uid_on(tmp_uid);
if(uuid_compare(uid, tmp_uid) == 0) if(uuid_compare(uid, tmp_uid) == 0)
{ {
strlcpy(result, "on", 3); strncpy(result, "on", 3);
result[2] = '\0';
return; return;
} }
@ -499,12 +500,12 @@ void
schedule_get_uid_off(uuid_t target) schedule_get_uid_off(uuid_t target)
{ {
uuid_clear(target); uuid_clear(target);
strlcpy((char*)target, "off", sizeof(uuid_t)); strncpy((char*)target, "off", 4);
} }
void void
schedule_get_uid_on(uuid_t target) schedule_get_uid_on(uuid_t target)
{ {
uuid_clear(target); uuid_clear(target);
strlcpy((char*)target, "on", sizeof(uuid_t)); strncpy((char*)target, "on", 3);
} }

View file

@ -1,4 +1,3 @@
#include <bsd/string.h>
#include <string.h> #include <string.h>
#include <logger.h> #include <logger.h>
@ -343,7 +342,8 @@ router_find_endpoint(const char *uri_str, size_t uri_len, struct mg_str *method_
{ {
size_t arg_value_str_len = strlen(best_endpoint->args[i].value.v_str); size_t arg_value_str_len = strlen(best_endpoint->args[i].value.v_str);
char *arg_value_str = malloc(sizeof(char) * (arg_value_str_len + 1)); char *arg_value_str = malloc(sizeof(char) * (arg_value_str_len + 1));
strlcpy(arg_value_str, best_endpoint->args[i].value.v_str, arg_value_str_len + 1); strcpy(arg_value_str, best_endpoint->args[i].value.v_str);
arg_value_str[arg_value_str_len] = '\0';
best_endpoint->args[i].value.v_str = arg_value_str; best_endpoint->args[i].value.v_str = arg_value_str;
} }
} }