From 4994b4375b11e9d7848108eaf2c71bb05aa5b773 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Tue, 11 May 2021 22:10:02 +0200 Subject: [PATCH] Replace strncpy with snprintf where necessary --- src/client.c | 2 +- src/lib/config.c | 2 +- src/lib/http.c | 2 +- src/lib/http.h | 2 +- src/lib/rev_proxy.c | 3 ++- src/lib/utils.c | 2 +- 6 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/client.c b/src/client.c index 99bf9db..51cc18f 100644 --- a/src/client.c +++ b/src/client.c @@ -682,7 +682,7 @@ int client_connection_handler(sock *client, unsigned long client_num) { if (pos != NULL) { pos = strstr(pos, "\"iso_code\":"); pos += 12; - strncpy(client_cc, pos, 2); + snprintf(client_cc, sizeof(client_cc), "%s", pos); } } diff --git a/src/lib/config.c b/src/lib/config.c index e642df2..15dad5c 100644 --- a/src/lib/config.c +++ b/src/lib/config.c @@ -85,7 +85,7 @@ int config_load(const char *filename) { len = strlen(ptr); if (ptr[0] == '[') { if (ptr[len - 1] != ']') goto err; - strncpy(tmp_config[i].name, ptr + 1, len - 2); + snprintf(tmp_config[i].name, sizeof(tmp_config[i].name), "%.*s", (int) len - 2, ptr + 1); i++; continue; } else if (i == 0) { diff --git a/src/lib/http.c b/src/lib/http.c index b723b63..94c7e36 100644 --- a/src/lib/http.c +++ b/src/lib/http.c @@ -125,7 +125,7 @@ int http_receive_request(sock *client, http_req *req) { return 2; } } - strncpy(req->method, ptr, pos1 - ptr - 1); + snprintf(req->method, sizeof(req->method), "%.*s", (int) (pos1 - ptr - 1), ptr); pos2 = memchr(pos1, ' ', rcv_len - (pos1 - buf)) + 1; if (pos2 == NULL) { diff --git a/src/lib/http.h b/src/lib/http.h index 15afc06..93da970 100644 --- a/src/lib/http.h +++ b/src/lib/http.h @@ -42,7 +42,7 @@ typedef struct { unsigned short code; char type[16]; - char msg[32]; + char msg[64]; } http_status; typedef struct { diff --git a/src/lib/rev_proxy.c b/src/lib/rev_proxy.c index e950867..e8e8d6e 100644 --- a/src/lib/rev_proxy.c +++ b/src/lib/rev_proxy.c @@ -341,7 +341,8 @@ int rev_proxy_init(http_req *req, http_res *res, host_config *conf, sock *client if (res->status == NULL && status_code >= 100 && status_code <= 999) { custom_status->code = status_code; strcpy(custom_status->type, ""); - strncpy(custom_status->msg, ptr + 13, strchr(ptr, '\r') - ptr - 13); + snprintf(custom_status->msg, sizeof(custom_status->msg), "%.*s", + (int) (strchr(ptr, '\r') - ptr - 13), ptr + 13); res->status = custom_status; } else if (res->status == NULL) { res->status = http_get_status(502); diff --git a/src/lib/utils.c b/src/lib/utils.c index 4c3acb8..8a0db6f 100644 --- a/src/lib/utils.c +++ b/src/lib/utils.c @@ -106,7 +106,7 @@ int url_decode(const char *str, char *dec, long *size) { int mime_is_compressible(const char *type) { if (type == NULL) return 0; char type_parsed[64]; - strncpy(type_parsed, type, sizeof(type_parsed) - 1); + snprintf(type_parsed, sizeof(type_parsed), "%s", type); char *pos = strchr(type_parsed, ';'); if (pos != NULL) pos[0] = 0; return