diff options
author | stderr64 <linuxwizard@voidnet.dy.fi> | 2024-01-28 23:14:53 +0200 |
---|---|---|
committer | stderr64 <linuxwizard@voidnet.dy.fi> | 2024-01-28 23:14:53 +0200 |
commit | 4d8e92033a32e17b0f5875aa67f0ccc67a212cc7 (patch) | |
tree | daf721d1b40cfe2c181f37009e6c45947a2185b4 | |
parent | bee0ea1cf58e1dd7ca9221105ddade3d5b4ddcbe (diff) | |
download | CWebHook-4d8e92033a32e17b0f5875aa67f0ccc67a212cc7.tar.gz CWebHook-4d8e92033a32e17b0f5875aa67f0ccc67a212cc7.tar.zst |
Added string copy function that only copies specified amount of characters
-rw-r--r-- | cwebhook.c | 1 | ||||
-rw-r--r-- | http_headers.h | 15 | ||||
-rw-r--r-- | str_fns.h | 20 |
3 files changed, 31 insertions, 5 deletions
@@ -18,6 +18,7 @@ #include <openssl/ssl.h> #include <cjson/cJSON.h> #include "structs.h" +#include "str_fns.h" #include "https_server_structs.h" #include "log.h" #include "load_config.h" diff --git a/http_headers.h b/http_headers.h index 3503bf1..71d376a 100644 --- a/http_headers.h +++ b/http_headers.h @@ -8,7 +8,8 @@ int get_request_data( http_request_data_t *hdt, char *req_contents ){ hdt->request_copy = (char*)calloc( hdt->request_size, sizeof(char) ); if ( hdt->request_copy == NULL ) return REQ_READ_ERROR; - strncpy( hdt->request_copy, req_contents, hdt->request_size ); + if ( !str_copy(hdt->request_copy, req_contents, hdt->request_size) ) + return REQ_READ_ERROR; if ( (size_t)strnlen(hdt->request_copy, (size_t)MAX_RECV_BYTES) != hdt->request_size ) return REQ_READ_ERROR; hdt->tok_current = strtok_r( hdt->request_copy, "\r\n", &hdt->tok_prev ); @@ -22,7 +23,8 @@ int get_request_data( http_request_data_t *hdt, char *req_contents ){ hdt->request_method = (char*)calloc( (size_t)strnlen(hdt->tok_current, (size_t)MAX_HEADER_LENGTH), sizeof(char) ); if ( hdt->request_method == NULL ) return REQ_READ_ERROR; - strncpy( hdt->request_method, hdt->tok_current, (size_t)strnlen(hdt->tok_current, (size_t)MAX_HEADER_LENGTH) ); + if ( !str_copy(hdt->request_method, hdt->tok_current, (size_t)strnlen(hdt->tok_current, (size_t)MAX_HEADER_LENGTH)) ) + return REQ_READ_ERROR; if ( (size_t)strnlen(hdt->request_method, MAX_HEADER_LENGTH) != (size_t)strnlen(hdt->tok_current, (size_t)(MAX_HEADER_LENGTH + 1)) ) return REQ_READ_ERROR; hdt->tok_current = strtok_r( NULL, " ", &hdt->tok_prev ); @@ -31,11 +33,13 @@ int get_request_data( http_request_data_t *hdt, char *req_contents ){ hdt->request_path = (char*)calloc( (size_t)strnlen(hdt->tok_current, (size_t)MAX_HEADER_LENGTH), sizeof(char) ); if ( hdt->request_path == NULL ) return REQ_READ_ERROR; - strncpy( hdt->request_path, hdt->tok_current, (size_t)strnlen(hdt->tok_current, (size_t)MAX_HEADER_LENGTH) ); + if ( !str_copy(hdt->request_path, hdt->tok_current, (size_t)strnlen(hdt->tok_current, (size_t)MAX_HEADER_LENGTH)) ) + return REQ_READ_ERROR; if ( (size_t)strnlen(hdt->request_path, (size_t)MAX_HEADER_LENGTH) != (size_t)strnlen(hdt->tok_current, (size_t)(MAX_HEADER_LENGTH + 1)) ) return REQ_READ_ERROR; memset( hdt->request_copy, 0, hdt->request_size ); - strncpy( hdt->request_copy, req_contents, hdt->request_size ); + if ( !str_copy(hdt->request_copy, req_contents, hdt->request_size) ) + return REQ_READ_ERROR; if ( (size_t)strnlen(hdt->request_copy, (size_t)(MAX_RECV_BYTES + 1)) != hdt->request_size ) return REQ_READ_ERROR; char *request_body_begin = strstr( hdt->request_copy, "\r\n\r\n" ); @@ -51,7 +55,8 @@ int get_request_data( http_request_data_t *hdt, char *req_contents ){ hdt->request_body = (char*)calloc( (size_t)strnlen(hdt->tok_current, (size_t)MAX_CONTENT_LENGTH), sizeof(char) ); if ( hdt->request_body == NULL ) return REQ_READ_ERROR; - strncpy( hdt->request_body, hdt->tok_current, (size_t)strnlen(hdt->tok_current, (size_t)MAX_CONTENT_LENGTH) ); + if ( !str_copy(hdt->request_body, hdt->tok_current, (size_t)strnlen(hdt->tok_current, (size_t)MAX_CONTENT_LENGTH)) ) + return REQ_READ_ERROR; if ( (size_t)strnlen(hdt->request_body, (size_t)MAX_CONTENT_LENGTH) != (size_t)strnlen(hdt->tok_current, (size_t)(MAX_CONTENT_LENGTH + 1)) ) return REQ_READ_ERROR; return REQ_READ_SUCCESS; diff --git a/str_fns.h b/str_fns.h new file mode 100644 index 0000000..d580231 --- /dev/null +++ b/str_fns.h @@ -0,0 +1,20 @@ +bool str_copy( char *dststr, char *srcstr, size_t max_chars ){ + if ( max_chars <= 0 ) + return false; + if ( srcstr == NULL ) + return false; + size_t src_len = (size_t)strnlen( srcstr, max_chars ); + if ( src_len <= 0 ) + return false; + bool need_null = false; + if ( src_len >= max_chars ) + need_null = true; + size_t copied_chars = 0; + for ( ; srcstr[copied_chars] != '\0' && copied_chars < max_chars; copied_chars++ ) + dststr[copied_chars] = srcstr[copied_chars]; + if ( need_null ) + dststr[copied_chars + 1] = '\0'; + if ( strncmp((const char*)dststr, (const char*)srcstr, max_chars) != 0 ) + return false; + return true; +} |