From 21f3a3a103bcf54aaedda734f0186fb150bfa73a Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Fri, 6 Sep 2024 13:43:25 +0900 Subject: [PATCH] Fix warnings --- src/config.c | 13 ++++++++----- src/http_template.c | 16 ++++++++++----- src/main.c | 47 ++++++++++++++++++++++++++++++++------------- 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/config.c b/src/config.c index ebead22..7338146 100644 --- a/src/config.c +++ b/src/config.c @@ -237,10 +237,11 @@ typedef struct C_SIMPLE_HTTP_INTERNAL_RequiredIter { int c_simple_http_required_iter_fn(void *data, void *ud) { C_SIMPLE_HTTP_INTERNAL_RequiredIter *req_iter_struct = ud; + unsigned int data_str_length = (unsigned int)strlen(data) + 1; if (simple_archiver_hash_map_get( req_iter_struct->hash_map, data, - strlen(data) + 1) == NULL) { + data_str_length) == NULL) { if (req_iter_struct->path) { fprintf( stderr, @@ -266,11 +267,12 @@ typedef struct C_SIMPLE_HTTP_INTERNAL_RequiredCheck { int c_simple_http_check_required_iter_fn(void *path_void_str, void *ud) { C_SIMPLE_HTTP_INTERNAL_RequiredCheck *req = ud; + unsigned int path_void_str_len = (unsigned int)strlen(path_void_str) + 1; C_SIMPLE_HTTP_HashMapWrapper *wrapper = simple_archiver_hash_map_get( req->map_of_paths_and_their_vars, path_void_str, - strlen(path_void_str) + 1); + path_void_str_len); if (!wrapper) { fprintf(stderr, "WARNING: Map of paths does not have path \"%s\"!\n", @@ -301,7 +303,8 @@ C_SIMPLE_HTTP_ParsedConfig c_simple_http_parse_config( fprintf(stderr, "ERROR: separating_key argument is NULL!\n"); return config; } - const unsigned int separating_key_size = strlen(separating_key) + 1; + const unsigned int separating_key_size = + (unsigned int)strlen(separating_key) + 1; config.hash_map = simple_archiver_hash_map_init(); @@ -384,7 +387,7 @@ C_SIMPLE_HTTP_ParsedConfig c_simple_http_parse_config( } if ((state & 1) == 0) { if (c != '=') { - key_buf[key_idx++] = c; + key_buf[key_idx++] = (unsigned char)c; if (key_idx >= C_SIMPLE_HTTP_CONFIG_BUF_SIZE) { fprintf(stderr, "ERROR: config file \"key\" is larger than %u bytes!\n", @@ -408,7 +411,7 @@ C_SIMPLE_HTTP_ParsedConfig c_simple_http_parse_config( } } else if ((state & 1) == 1) { if ((c != '\n' && c != '\r') || (state & 0xC) != 0) { - value_buf[value_idx++] = c; + value_buf[value_idx++] = (unsigned char)c; if (value_idx >= C_SIMPLE_HTTP_CONFIG_BUF_SIZE) { fprintf(stderr, "ERROR: config file \"value\" is larger than %u bytes!\n", diff --git a/src/http_template.c b/src/http_template.c index 5430631..ea242cd 100644 --- a/src/http_template.c +++ b/src/http_template.c @@ -103,8 +103,14 @@ char *c_simple_http_path_to_generated( if (output_buf_size) { *output_buf_size = 0; } + size_t path_len_size_t = strlen(path) + 1; + if (path_len_size_t > 0xFFFFFFFF) { + fprintf(stderr, "ERROR: Path string is too large!\n"); + return NULL; + } + unsigned int path_len = (unsigned int)path_len_size_t; C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map = - simple_archiver_hash_map_get(templates->hash_map, path, strlen(path) + 1); + simple_archiver_hash_map_get(templates->hash_map, path, path_len); if (!wrapped_hash_map) { return NULL; } @@ -129,13 +135,13 @@ char *c_simple_http_path_to_generated( if (fseek(f, 0, SEEK_SET) != 0) { return NULL; } - html_buf = malloc(html_file_size + 1); - size_t ret = fread(html_buf, 1, html_file_size, f); + html_buf = malloc((size_t)html_file_size + 1); + size_t ret = fread(html_buf, 1, (size_t)html_file_size, f); if (ret != (size_t)html_file_size) { return NULL; } html_buf[html_file_size] = 0; - html_buf_size = html_file_size; + html_buf_size = (size_t)html_file_size; } else { char *stored_html = simple_archiver_hash_map_get(wrapped_hash_map->hash_map, "HTML", 5); @@ -215,7 +221,7 @@ char *c_simple_http_path_to_generated( simple_archiver_hash_map_get( wrapped_hash_map->hash_map, var, - var_size + 1); + (unsigned int)var_size + 1); if (value_c_str) { if (c_simple_http_internal_ends_with_FILE(var) == 0) { // Load from file specified by "value_c_str". diff --git a/src/main.c b/src/main.c index 17ab294..1fc4c17 100644 --- a/src/main.c +++ b/src/main.c @@ -39,6 +39,13 @@ #include "constants.h" #include "http.h" +#define CHECK_ERROR_WRITE(write_expr) \ + if (write_expr < 0) { \ + close(connection_fd); \ + fprintf(stderr, "ERROR Failed to write to connected peer, closing...\n"); \ + continue; \ + } + int main(int argc, char **argv) { Args args = parse_args(argc, argv); @@ -113,6 +120,13 @@ int main(int argc, char **argv) { puts(""); int connection_fd = ret; read_ret = read(connection_fd, recv_buf, C_SIMPLE_HTTP_RECV_BUF_SIZE); + if (read_ret < 0) { + close(connection_fd); + fprintf( + stderr, + "WARNING Failed to read from new connection, closing...\n"); + continue; + } // DEBUG print received buf. for (unsigned int idx = 0; idx < C_SIMPLE_HTTP_RECV_BUF_SIZE && idx < read_ret; @@ -128,11 +142,11 @@ int main(int argc, char **argv) { size_t response_size = 0; const char *response = c_simple_http_request_response( - (const char*)recv_buf, read_ret, &parsed_config, &response_size); + (const char*)recv_buf, (unsigned int)read_ret, &parsed_config, &response_size); if (response) { - write(connection_fd, "HTTP/1.1 200 OK\n", 16); - write(connection_fd, "Allow: GET\n", 11); - write(connection_fd, "Content-Type: text/html\n", 24); + CHECK_ERROR_WRITE(write(connection_fd, "HTTP/1.1 200 OK\n", 16)); + CHECK_ERROR_WRITE(write(connection_fd, "Allow: GET\n", 11)); + CHECK_ERROR_WRITE(write(connection_fd, "Content-Type: text/html\n", 24)); char content_length_buf[128]; size_t content_length_buf_size = 0; memcpy(content_length_buf, "Content-Length: ", 16); @@ -144,19 +158,26 @@ int main(int argc, char **argv) { "%lu\n%n", response_size, &written); - content_length_buf_size += written; - write(connection_fd, content_length_buf, content_length_buf_size); - write(connection_fd, "\n", 1); - write(connection_fd, response, response_size); + if (written <= 0) { + close(connection_fd); + fprintf( + stderr, + "WARNING Failed to write in response, closing connection...\n"); + continue; + } + content_length_buf_size += (size_t)written; + CHECK_ERROR_WRITE(write(connection_fd, content_length_buf, content_length_buf_size)); + CHECK_ERROR_WRITE(write(connection_fd, "\n", 1)); + CHECK_ERROR_WRITE(write(connection_fd, response, response_size)); free((void*)response); } else { // TODO handle internal errors also. - write(connection_fd, "HTTP/1.1 404 Not Found\n", 23); - write(connection_fd, "Allow: GET\n", 11); - write(connection_fd, "Content-Type: text/html\n", 24); - write(connection_fd, "Content-Length: 14\n", 19); - write(connection_fd, "\n404 Not Found\n", 15); + CHECK_ERROR_WRITE(write(connection_fd, "HTTP/1.1 404 Not Found\n", 23)); + CHECK_ERROR_WRITE(write(connection_fd, "Allow: GET\n", 11)); + CHECK_ERROR_WRITE(write(connection_fd, "Content-Type: text/html\n", 24)); + CHECK_ERROR_WRITE(write(connection_fd, "Content-Length: 14\n", 19)); + CHECK_ERROR_WRITE(write(connection_fd, "\n404 Not Found\n", 15)); } close(connection_fd); } else {