From 6912038d7281b8cd7f4647a7007771eff4b3fb39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Fri, 8 Mar 2024 09:38:22 +0100 Subject: [PATCH] unix,fs: fix realpath calls that use the system allocator Make sure we allocate the memory with uv__malloc so it's in turn freed with uv__free. Fixes: https://github.com/libuv/libuv/issues/4329 --- src/unix/fs.c | 13 +++++++++++-- src/unix/fsevents.c | 9 +++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/unix/fs.c b/src/unix/fs.c index 3a74350f0e5..7105f4cbea9 100644 --- a/src/unix/fs.c +++ b/src/unix/fs.c @@ -691,14 +691,23 @@ static ssize_t uv__fs_readlink(uv_fs_t* req) { static ssize_t uv__fs_realpath(uv_fs_t* req) { char* buf; + char* tmp; #if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L - buf = realpath(req->path, NULL); - if (buf == NULL) + tmp = realpath(req->path, NULL); + if (tmp == NULL) return -1; + buf = uv__strdup(tmp); + free(tmp); /* _Not_ uv__free. */ + if (buf == NULL) { + errno = ENOMEM; + return -1; + } #else ssize_t len; + (void)tmp; + len = uv__fs_pathmax_size(req->path); buf = uv__malloc(len + 1); diff --git a/src/unix/fsevents.c b/src/unix/fsevents.c index df703f3635f..5efe495e694 100644 --- a/src/unix/fsevents.c +++ b/src/unix/fsevents.c @@ -793,6 +793,7 @@ int uv__cf_loop_signal(uv_loop_t* loop, /* Runs in UV loop to initialize handle */ int uv__fsevents_init(uv_fs_event_t* handle) { + char* buf; int err; uv__cf_loop_state_t* state; @@ -801,9 +802,13 @@ int uv__fsevents_init(uv_fs_event_t* handle) { return err; /* Get absolute path to file */ - handle->realpath = realpath(handle->path, NULL); - if (handle->realpath == NULL) + buf = realpath(handle->path, NULL); + if (buf == NULL) return UV__ERR(errno); + handle->realpath = uv__strdup(buf); + free(buf); /* _Not_ uv__free. */ + if (handle->realpath == NULL) + return UV_ENOMEM; handle->realpath_len = strlen(handle->realpath); /* Initialize event queue */