From 79ae05b3efe7bc8b15351bc4a5e784148dfdf51c Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Thu, 6 Jul 2023 17:28:59 +0200 Subject: [PATCH] network: Fix read-only strings being overwritten POSIX sucks and here we have another example. strchr() / strrchr() take a pointer to a const string, and return a pointer to the character found as a non-const "char *". The compiler and static analyzers would then detect the follow-up code, which would overwrite the static string through that non-const pointer, as perfectly valid C. Address this issue by copying the const string into a temporary buffer. Signed-off-by: Paul Cercueil --- network.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/network.c b/network.c index cabc6fa2c..44f8d0cbe 100644 --- a/network.c +++ b/network.c @@ -1060,7 +1060,7 @@ static bool msg_trunc_supported(struct iiod_client_pdata *io_ctx) } #endif -struct iio_context * network_create_context(const char *host) +struct iio_context * network_create_context(const char *hostname) { struct addrinfo hints, *res; struct iio_context *ctx; @@ -1072,6 +1072,10 @@ struct iio_context * network_create_context(const char *host) char *description, *uri, *end, *port = NULL; char port_str[6]; uint16_t port_num = IIOD_PORT; + char host_buf[FQDN_LEN + sizeof(":65535") + 1]; + char *host = hostname ? host_buf : NULL; + + iio_strlcpy(host_buf, hostname, sizeof(host_buf)); #ifdef _WIN32 WSADATA wsaData;