commit c5b8ed870b8ec5c7b1199bce7a31abbda2edbf92
parent ce5c7111f4050f168ddf4806287b4c384264b0fa
Author: Sathya Pramodh <94102031+sathya-pramodh@users.noreply.github.com>
Date: Mon, 23 Feb 2026 14:48:20 +0530
fix(coverity/644326): NULL ptr deref in socket_address_is_tcp #38010
Diffstat:
3 files changed, 11 insertions(+), 11 deletions(-)
diff --git 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
@@ -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
@@ -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);