Skip to content

Commit

Permalink
unix,fs: fix realpath calls that use the system allocator
Browse files Browse the repository at this point in the history
Make sure we allocate the memory with uv__malloc so it's in turn freed
with uv__free.

Fixes: libuv#4329
  • Loading branch information
saghul committed Mar 8, 2024
1 parent 2c15345 commit 6912038
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/unix/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 7 additions & 2 deletions src/unix/fsevents.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 */
Expand Down

0 comments on commit 6912038

Please sign in to comment.