linux: fix uv_exepath(size=1) UV_EINVAL error

uv_exepath() should write as much as possible to the output buffer.
In the case of size=1, skip the call to readlink() because it will
fail with EINVAL; just write the terminating zero byte.

PR-URL: https://github.com/libuv/libuv/pull/104
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
This commit is contained in:
Ben Noordhuis 2015-01-03 09:47:44 +01:00
parent d17a8e45f5
commit 555a9647a0
2 changed files with 13 additions and 1 deletions

View File

@ -381,7 +381,10 @@ int uv_exepath(char* buffer, size_t* size) {
if (buffer == NULL || size == NULL || *size == 0)
return -EINVAL;
n = readlink("/proc/self/exe", buffer, *size - 1);
n = *size - 1;
if (n > 0)
n = readlink("/proc/self/exe", buffer, n);
if (n == -1)
return -errno;

View File

@ -73,5 +73,14 @@ TEST_IMPL(get_currentexe) {
ASSERT(size == 0);
ASSERT(buffer[0] == '\0');
memset(buffer, -1, sizeof(buffer));
size = 2;
r = uv_exepath(buffer, &size);
ASSERT(r == 0);
ASSERT(size == 1);
ASSERT(buffer[0] != '\0');
ASSERT(buffer[1] == '\0');
return 0;
}