From 1f315d2b9284926175a114c2eb21fcdf8da2caee Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Thu, 19 Nov 2020 23:36:02 +0100 Subject: [PATCH] Remove libbsd and strlcpy --- CMakeLists.txt | 1 - src/cache.c | 5 ++--- src/config.c | 16 +++++++++------- src/database.c | 7 ++++--- src/endpoint.c | 9 +++------ src/endpoints/api_v1_controllers_discover.c | 14 +++++++++----- src/endpoints/api_v1_tags.c | 5 ++--- src/handlers/http.c | 4 ++-- src/models/controller.c | 7 ++++--- src/models/junction_relay_schedule.c | 4 ++-- src/models/junction_tag.c | 7 ++++--- src/models/macro.c | 3 +-- src/models/relay.c | 4 ++-- src/models/schedule.c | 13 +++++++------ src/router.c | 4 ++-- 15 files changed, 53 insertions(+), 50 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9607777..ce89eca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,6 @@ add_executable(core src/main.c) target_link_libraries(core -lsqlite3) target_link_libraries(core -luuid) -target_link_libraries(core -lbsd) set(CMAKE_C_FLAGS "$ENV{CFLAGS}") set(CMAKE_C_FLAGS "-D'__FILENAME__=\"$(subst $(realpath ${CMAKE_SOURCE_DIR}/src/)/,,$(abspath $<))\"'") diff --git a/src/cache.c b/src/cache.c index 37516f0..ce70088 100644 --- a/src/cache.c +++ b/src/cache.c @@ -1,5 +1,3 @@ -#include - #include #include #include @@ -34,7 +32,8 @@ cache_get_value(char *key) free(result); } 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 { diff --git a/src/config.c b/src/config.c index be789c3..7ce9df7 100644 --- a/src/config.c +++ b/src/config.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -245,7 +244,8 @@ config_load_string(char **holder, const char *value) size_t value_len = strlen(value); 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; } @@ -355,16 +355,18 @@ config_load_directory(config_t *config, const char *directory_name) size_t copied = 0; // Add 2 for '/' and '\0'. - size_t path_size = strlen(directory_name) + strlen(entry_name) + 2; - char *path = malloc(sizeof(char) * path_size); + size_t path_len = strlen(directory_name) + strlen(entry_name) + 1; + char *path = malloc(sizeof(char) * (path_len + 1)); 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] != '/') { - 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)) { diff --git a/src/database.c b/src/database.c index 76cb9f2..4ced6cc 100644 --- a/src/database.c +++ b/src/database.c @@ -1,4 +1,3 @@ -#include #include #include @@ -224,7 +223,8 @@ database_helper_get_string(sqlite3_stmt *stmt) free(result); } 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 { @@ -268,7 +268,8 @@ database_helper_get_strings(sqlite3_stmt *stmt) result = (char**)realloc(result, sizeof(char*) * (row + 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 { diff --git a/src/endpoint.c b/src/endpoint.c index 12b7742..d56838f 100644 --- a/src/endpoint.c +++ b/src/endpoint.c @@ -1,5 +1,3 @@ -#include - #include #include #include @@ -100,10 +98,9 @@ endpoint_response_text(endpoint_response_t *response, int status_code, const cha response->content_length = strlen(content); response->alloced_content = true; - int content_size = response->content_length + 1; - - response->content = malloc(sizeof(char) * content_size); - strlcpy(response->content, content, content_size); + response->content = malloc(sizeof(char) * (response->content_length + 1)); + strcpy(response->content, content); + response->content[response->content_length] = '\0'; } } diff --git a/src/endpoints/api_v1_controllers_discover.c b/src/endpoints/api_v1_controllers_discover.c index 0cbda10..00e7042 100644 --- a/src/endpoints/api_v1_controllers_discover.c +++ b/src/endpoints/api_v1_controllers_discover.c @@ -1,5 +1,3 @@ -#include - #include #include #include @@ -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)); known_controllers[i]->active = 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); + strncpy(known_controllers[i]->name, discovered_name, discovered_name_len); 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]->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)); 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); strncpy(discovered_controller->name, discovered_name, discovered_name_len); discovered_controller->name[discovered_name_len] = '\0'; diff --git a/src/endpoints/api_v1_tags.c b/src/endpoints/api_v1_tags.c index cb3891f..5ab74c5 100644 --- a/src/endpoints/api_v1_tags.c +++ b/src/endpoints/api_v1_tags.c @@ -1,5 +1,3 @@ -#include - #include #include #include @@ -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. 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); diff --git a/src/handlers/http.c b/src/handlers/http.c index 5596f44..65fa69f 100644 --- a/src/handlers/http.c +++ b/src/handlers/http.c @@ -1,4 +1,3 @@ -#include #include #include @@ -22,7 +21,8 @@ add_extra_headers(char *extra_headers) if(extra_headers == NULL) { 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; } diff --git a/src/models/controller.c b/src/models/controller.c index f585ff3..3ed6048 100644 --- a/src/models/controller.c +++ b/src/models/controller.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -53,14 +52,16 @@ controller_db_select_mapper(sqlite3_stmt *stmt) new_controller->id = sqlite3_column_int(stmt, i); break; 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; default: // ignore columns not implemented break; } break; 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; case 'p': // port new_controller->port = sqlite3_column_int(stmt, i); diff --git a/src/models/junction_relay_schedule.c b/src/models/junction_relay_schedule.c index 346efad..e2c8411 100644 --- a/src/models/junction_relay_schedule.c +++ b/src/models/junction_relay_schedule.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -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; 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); for(int i = 0; i < 7; ++i) diff --git a/src/models/junction_tag.c b/src/models/junction_tag.c index 04b8cc6..10700ad 100644 --- a/src/models/junction_tag.c +++ b/src/models/junction_tag.c @@ -1,4 +1,3 @@ -#include #include #include @@ -69,8 +68,10 @@ junction_tag_insert_list(int *tag_ids, int relay_id, int schedule_id, int count) static const char query_extender[] = " (?, ?, ?)"; size_t query_len = M_STRLEN(query_base) + (count * (M_STRLEN(query_extender) + 1)) + 1; - char *query = malloc(sizeof(char) * query_len + 1); - strlcpy(query, query_base, query_len + 1); + char *query = malloc(sizeof(char) * (query_len + 1)); + strncpy(query, query_base, query_len); + query[query_len] = '\0'; + query_len -= M_STRLEN(query_base); for(int i = 0; i < count; ++i) { diff --git a/src/models/macro.c b/src/models/macro.c index d658ff1..00a72aa 100644 --- a/src/models/macro.c +++ b/src/models/macro.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -43,7 +42,7 @@ macro_db_select_mapper(sqlite3_stmt *stmt) new_macro->id = sqlite3_column_int(stmt, i); break; 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'; break; case 'u': // uid diff --git a/src/models/relay.c b/src/models/relay.c index 2c1f860..1707d22 100644 --- a/src/models/relay.c +++ b/src/models/relay.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -55,7 +54,8 @@ relay_db_select_mapper(sqlite3_stmt *stmt) switch(name[1]) { 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; case 'u': // number new_relay->number = sqlite3_column_int(stmt, i); diff --git a/src/models/schedule.c b/src/models/schedule.c index f0a04c4..d425b9b 100644 --- a/src/models/schedule.c +++ b/src/models/schedule.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -49,7 +48,7 @@ schedule_db_select_mapper(sqlite3_stmt *stmt) new_schedule->id = sqlite3_column_int(stmt, i); break; 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'; break; case 'p': // periods @@ -481,14 +480,16 @@ schedule_uid_unparse(const uuid_t uid, char *result) schedule_get_uid_off(tmp_uid); if(uuid_compare(uid, tmp_uid) == 0) { - strlcpy(result, "off", 4); + strncpy(result, "off", 4); + result[3] = '\0'; return; } schedule_get_uid_on(tmp_uid); if(uuid_compare(uid, tmp_uid) == 0) { - strlcpy(result, "on", 3); + strncpy(result, "on", 3); + result[2] = '\0'; return; } @@ -499,12 +500,12 @@ void schedule_get_uid_off(uuid_t target) { uuid_clear(target); - strlcpy((char*)target, "off", sizeof(uuid_t)); + strncpy((char*)target, "off", 4); } void schedule_get_uid_on(uuid_t target) { uuid_clear(target); - strlcpy((char*)target, "on", sizeof(uuid_t)); + strncpy((char*)target, "on", 3); } diff --git a/src/router.c b/src/router.c index 1831b67..e683891 100644 --- a/src/router.c +++ b/src/router.c @@ -1,4 +1,3 @@ -#include #include #include @@ -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); 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; } }