unix, win: count null byte on UV_ENOBUFS

If an API function returns UV_ENOBUFS make sure we count the terminating
null, which we need space for. In case of success the null byte is not
included in the count, but the buffer *is* null terminated.

PR-URL: https://github.com/libuv/libuv/pull/690
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Saúl Ibarra Corretgé 2016-01-21 10:00:27 +01:00
parent e5f4b79809
commit 2606ba22a7
7 changed files with 16 additions and 14 deletions

View File

@ -265,8 +265,9 @@ API
`uv_os_homedir()` first checks the `HOME` environment variable using
:man:`getenv(3)`. If `HOME` is not set, :man:`getpwuid_r(3)` is called. The
user's home directory is stored in `buffer`. When `uv_os_homedir()` is
called, `size` indicates the maximum size of `buffer`. On success or
`UV_ENOBUFS` failure, `size` is set to the string length of `buffer`.
called, `size` indicates the maximum size of `buffer`. On success `size` is set
to the string length of `buffer`. On `UV_ENOBUFS` failure `size` is set to the
required length for `buffer`, including the null byte.
.. warning::
`uv_os_homedir()` is not thread safe.
@ -281,8 +282,9 @@ API
If none of these are found, the path `"/tmp"` is used, or, on Android,
`"/data/local/tmp"` is used. The temp directory is stored in `buffer`. When
`uv_os_tmpdir()` is called, `size` indicates the maximum size of `buffer`.
On success or `UV_ENOBUFS` failure, `size` is set to the string length of
`buffer` (which does not include the terminating null).
On success `size` is set to the string length of `buffer` (which does not
include the terminating null). On `UV_ENOBUFS` failure `size` is set to the
required length for `buffer`, including the null byte.
.. warning::
`uv_os_tmpdir()` is not thread safe.

View File

@ -139,7 +139,7 @@ int uv_fs_poll_getpath(uv_fs_poll_t* handle, char* buffer, size_t* size) {
required_len = strlen(ctx->path);
if (required_len >= *size) {
*size = required_len;
*size = required_len + 1;
return UV_ENOBUFS;
}

View File

@ -1036,7 +1036,7 @@ int uv_os_homedir(char* buffer, size_t* size) {
len = strlen(buf);
if (len >= *size) {
*size = len;
*size = len + 1;
return -ENOBUFS;
}
@ -1091,7 +1091,7 @@ int uv_os_homedir(char* buffer, size_t* size) {
len = strlen(pw.pw_dir);
if (len >= *size) {
*size = len;
*size = len + 1;
uv__free(buf);
return -ENOBUFS;
}
@ -1138,7 +1138,7 @@ return_buffer:
len = strlen(buf);
if (len >= *size) {
*size = len;
*size = len + 1;
return -ENOBUFS;
}

View File

@ -235,7 +235,7 @@ static int uv__pipe_getsockpeername(const uv_pipe_t* handle,
if (addrlen >= *size) {
*size = addrlen;
*size = addrlen + 1;
return UV_ENOBUFS;
}

View File

@ -452,7 +452,7 @@ int uv_fs_event_getpath(uv_fs_event_t* handle, char* buffer, size_t* size) {
required_len = strlen(handle->path);
if (required_len >= *size) {
*size = required_len;
*size = required_len + 1;
return UV_ENOBUFS;
}

View File

@ -2040,7 +2040,7 @@ static int uv__pipe_getname(const uv_pipe_t* handle, char* buffer, size_t* size)
goto error;
} else if (pipe_prefix_len + addrlen >= *size) {
/* "\\\\.\\pipe" + name */
*size = pipe_prefix_len + addrlen;
*size = pipe_prefix_len + addrlen + 1;
err = UV_ENOBUFS;
goto error;
}

View File

@ -210,7 +210,7 @@ int uv_cwd(char* buffer, size_t* size) {
if (r == 0) {
return uv_translate_sys_error(GetLastError());
} else if (r > (int) *size) {
*size = r -1;
*size = r;
return UV_ENOBUFS;
}
@ -1218,7 +1218,7 @@ convert_buffer:
if (bufsize == 0) {
return uv_translate_sys_error(GetLastError());
} else if (bufsize > *size) {
*size = bufsize - 1;
*size = bufsize;
return UV_ENOBUFS;
}
@ -1263,7 +1263,7 @@ int uv_os_tmpdir(char* buffer, size_t* size) {
if (bufsize == 0) {
return uv_translate_sys_error(GetLastError());
} else if (bufsize > *size) {
*size = bufsize - 1;
*size = bufsize;
return UV_ENOBUFS;
}