diff --git a/src/nvim/event/socket.c b/src/nvim/event/socket.c index bc92044d83..e55ff5813c 100644 --- a/src/nvim/event/socket.c +++ b/src/nvim/event/socket.c @@ -23,24 +23,24 @@ #include "event/socket.c.generated.h" -/// Checks if an address string looks like a TCP endpoint. +/// Checks if an address string looks like a TCP endpoint, and returns the end of the host part. /// /// @param address Address string -/// @return true if address looks like a TCP endpoint, false otherwise -bool socket_address_is_tcp(const char *address) +/// @return pointer to the end of the host part of the address, or NULL if it is not a TCP address +char *socket_address_tcp_host_end(const char *address) { if (address == NULL) { - return false; + return NULL; } // Windows drive letter path: "X:\..." or "X:/..." is a local path, not TCP. if (ASCII_ISALPHA((uint8_t)address[0]) && address[1] == ':' && (address[2] == '\\' || address[2] == '/')) { - return false; + return NULL; } - const char *colon = strrchr(address, ':'); - return colon != NULL && colon != address; + char *colon = strrchr(address, ':'); + return colon != NULL && colon != address ? colon : NULL; } int socket_watcher_init(Loop *loop, SocketWatcher *watcher, const char *endpoint) @@ -48,9 +48,9 @@ int socket_watcher_init(Loop *loop, SocketWatcher *watcher, const char *endpoint { xstrlcpy(watcher->addr, endpoint, sizeof(watcher->addr)); char *addr = watcher->addr; - char *host_end = strrchr(addr, ':'); + char *host_end = socket_address_tcp_host_end(addr); - if (socket_address_is_tcp(addr)) { + if (host_end) { // Split user specified address into two strings, addr (hostname) and port. // The port part in watcher->addr will be updated later. *host_end = NUL; diff --git a/src/nvim/main.c b/src/nvim/main.c index e8249e8470..7087dd47d2 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -928,7 +928,7 @@ static uint64_t server_connect(char *server_addr, const char **errmsg) } CallbackReader on_data = CALLBACK_READER_INIT; const char *error = NULL; - bool is_tcp = socket_address_is_tcp(server_addr); + bool is_tcp = socket_address_tcp_host_end(server_addr) != NULL; // connected to channel uint64_t chan = channel_connect(is_tcp, server_addr, true, on_data, 500, &error); if (error) { diff --git a/src/nvim/ui_client.c b/src/nvim/ui_client.c index 594d71cf06..07d22d49c1 100644 --- a/src/nvim/ui_client.c +++ b/src/nvim/ui_client.c @@ -298,7 +298,7 @@ static void channel_connect_event(void **argv) char *server_addr = argv[0]; const char *err = ""; - bool is_tcp = socket_address_is_tcp(server_addr); + bool is_tcp = socket_address_tcp_host_end(server_addr) != NULL; CallbackReader on_data = CALLBACK_READER_INIT; uint64_t chan = channel_connect(is_tcp, server_addr, true, on_data, 50, &err);