Skip to content

Commit 6912038

Browse files
committed
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: libuv#4329
1 parent 2c15345 commit 6912038

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/unix/fs.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,14 +691,23 @@ static ssize_t uv__fs_readlink(uv_fs_t* req) {
691691

692692
static ssize_t uv__fs_realpath(uv_fs_t* req) {
693693
char* buf;
694+
char* tmp;
694695

695696
#if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L
696-
buf = realpath(req->path, NULL);
697-
if (buf == NULL)
697+
tmp = realpath(req->path, NULL);
698+
if (tmp == NULL)
698699
return -1;
700+
buf = uv__strdup(tmp);
701+
free(tmp); /* _Not_ uv__free. */
702+
if (buf == NULL) {
703+
errno = ENOMEM;
704+
return -1;
705+
}
699706
#else
700707
ssize_t len;
701708

709+
(void)tmp;
710+
702711
len = uv__fs_pathmax_size(req->path);
703712
buf = uv__malloc(len + 1);
704713

src/unix/fsevents.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,7 @@ int uv__cf_loop_signal(uv_loop_t* loop,
793793

794794
/* Runs in UV loop to initialize handle */
795795
int uv__fsevents_init(uv_fs_event_t* handle) {
796+
char* buf;
796797
int err;
797798
uv__cf_loop_state_t* state;
798799

@@ -801,9 +802,13 @@ int uv__fsevents_init(uv_fs_event_t* handle) {
801802
return err;
802803

803804
/* Get absolute path to file */
804-
handle->realpath = realpath(handle->path, NULL);
805-
if (handle->realpath == NULL)
805+
buf = realpath(handle->path, NULL);
806+
if (buf == NULL)
806807
return UV__ERR(errno);
808+
handle->realpath = uv__strdup(buf);
809+
free(buf); /* _Not_ uv__free. */
810+
if (handle->realpath == NULL)
811+
return UV_ENOMEM;
807812
handle->realpath_len = strlen(handle->realpath);
808813

809814
/* Initialize event queue */

0 commit comments

Comments
 (0)