From e5f4b7980953ab6445db07e141032e903623aba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Wed, 20 Jan 2016 09:52:54 +0100 Subject: [PATCH] unix, win: consistently null-terminate buffers libuv has multiple functions which return buffers. Make them consistent with the following rules: the returned size *does not* include the null byte, but the buffer *is* null terminated. There is only one exception to the above: Linux abstract sockets, because null bytes are not used as string terminators in those. Refs: https://github.com/libuv/libuv/pull/674 PR-URL: https://github.com/libuv/libuv/pull/690 Reviewed-By: Colin Ihrig --- src/fs-poll.c | 3 ++- src/unix/pipe.c | 6 +++++- src/uv-common.c | 3 ++- src/win/pipe.c | 3 ++- test/test-fs-event.c | 1 + test/test-fs-poll.c | 1 + test/test-pipe-getsockname.c | 1 + 7 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/fs-poll.c b/src/fs-poll.c index 44d47b88..570c9547 100644 --- a/src/fs-poll.c +++ b/src/fs-poll.c @@ -138,13 +138,14 @@ int uv_fs_poll_getpath(uv_fs_poll_t* handle, char* buffer, size_t* size) { assert(ctx != NULL); required_len = strlen(ctx->path); - if (required_len > *size) { + if (required_len >= *size) { *size = required_len; return UV_ENOBUFS; } memcpy(buffer, ctx->path, required_len); *size = required_len; + buffer[required_len] = '\0'; return 0; } diff --git a/src/unix/pipe.c b/src/unix/pipe.c index 7f87a713..228f1f77 100644 --- a/src/unix/pipe.c +++ b/src/unix/pipe.c @@ -234,7 +234,7 @@ static int uv__pipe_getsockpeername(const uv_pipe_t* handle, addrlen = strlen(sa.sun_path); - if (addrlen > *size) { + if (addrlen >= *size) { *size = addrlen; return UV_ENOBUFS; } @@ -242,6 +242,10 @@ static int uv__pipe_getsockpeername(const uv_pipe_t* handle, memcpy(buffer, sa.sun_path, addrlen); *size = addrlen; + /* only null-terminate if it's not an abstract socket */ + if (buffer[0] != '\0') + buffer[addrlen] = '\0'; + return 0; } diff --git a/src/uv-common.c b/src/uv-common.c index 40ed28fe..09979536 100644 --- a/src/uv-common.c +++ b/src/uv-common.c @@ -451,13 +451,14 @@ int uv_fs_event_getpath(uv_fs_event_t* handle, char* buffer, size_t* size) { } required_len = strlen(handle->path); - if (required_len > *size) { + if (required_len >= *size) { *size = required_len; return UV_ENOBUFS; } memcpy(buffer, handle->path, required_len); *size = required_len; + buffer[required_len] = '\0'; return 0; } diff --git a/src/win/pipe.c b/src/win/pipe.c index bcce80c7..a0948ded 100644 --- a/src/win/pipe.c +++ b/src/win/pipe.c @@ -2038,7 +2038,7 @@ static int uv__pipe_getname(const uv_pipe_t* handle, char* buffer, size_t* size) *size = 0; err = uv_translate_sys_error(GetLastError()); goto error; - } else if (pipe_prefix_len + addrlen > *size) { + } else if (pipe_prefix_len + addrlen >= *size) { /* "\\\\.\\pipe" + name */ *size = pipe_prefix_len + addrlen; err = UV_ENOBUFS; @@ -2062,6 +2062,7 @@ static int uv__pipe_getname(const uv_pipe_t* handle, char* buffer, size_t* size) addrlen += pipe_prefix_len; *size = addrlen; + buffer[addrlen] = '\0'; err = 0; goto cleanup; diff --git a/test/test-fs-event.c b/test/test-fs-event.c index 267533dd..35583529 100644 --- a/test/test-fs-event.c +++ b/test/test-fs-event.c @@ -819,6 +819,7 @@ TEST_IMPL(fs_event_getpath) { r = uv_fs_event_getpath(&fs_event, buf, &len); ASSERT(r == 0); ASSERT(buf[len - 1] != 0); + ASSERT(buf[len] == '\0'); ASSERT(memcmp(buf, "watch_dir", len) == 0); r = uv_fs_event_stop(&fs_event); ASSERT(r == 0); diff --git a/test/test-fs-poll.c b/test/test-fs-poll.c index dbc1515b..737d50df 100644 --- a/test/test-fs-poll.c +++ b/test/test-fs-poll.c @@ -173,6 +173,7 @@ TEST_IMPL(fs_poll_getpath) { len = sizeof buf; ASSERT(0 == uv_fs_poll_getpath(&poll_handle, buf, &len)); ASSERT(buf[len - 1] != 0); + ASSERT(buf[len] == '\0'); ASSERT(0 == memcmp(buf, FIXTURE, len)); uv_close((uv_handle_t*) &poll_handle, close_cb); diff --git a/test/test-pipe-getsockname.c b/test/test-pipe-getsockname.c index 5e036f9d..58041c02 100644 --- a/test/test-pipe-getsockname.c +++ b/test/test-pipe-getsockname.c @@ -114,6 +114,7 @@ TEST_IMPL(pipe_getsockname) { ASSERT(r == 0); ASSERT(buf[len - 1] != 0); + ASSERT(buf[len] == '\0'); ASSERT(memcmp(buf, TEST_PIPENAME, len) == 0); len = sizeof buf;