Skip to content

Commit

Permalink
network: Fix read-only strings being overwritten
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
pcercuei committed Jul 10, 2023
1 parent 8904ef7 commit 79ae05b
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion network.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 79ae05b

Please sign in to comment.