linux: get rid of strncpy() call

While correctly used, it looks suspect and is slightly less efficient
because the string is scanned twice for its terminating nul byte.

PR-URL: https://github.com/libuv/libuv/pull/2065
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
This commit is contained in:
Ben Noordhuis 2018-12-03 09:29:23 +01:00
parent 8972e65bf5
commit 3909e7f6c6

View File

@ -278,6 +278,7 @@ int uv_fs_event_start(uv_fs_event_t* handle,
const char* path,
unsigned int flags) {
struct watcher_list* w;
size_t len;
int events;
int err;
int wd;
@ -306,12 +307,13 @@ int uv_fs_event_start(uv_fs_event_t* handle,
if (w)
goto no_insert;
w = uv__malloc(sizeof(*w) + strlen(path) + 1);
len = strlen(path) + 1;
w = uv__malloc(sizeof(*w) + len);
if (w == NULL)
return UV_ENOMEM;
w->wd = wd;
w->path = strcpy((char*)(w + 1), path);
w->path = memcpy(w + 1, path, len);
QUEUE_INIT(&w->watchers);
w->iterating = 0;
RB_INSERT(watcher_root, CAST(&handle->loop->inotify_watchers), w);