diff options
author | hamaji@chromium.org <hamaji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-23 17:09:07 +0000 |
---|---|---|
committer | hamaji@chromium.org <hamaji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-23 17:09:07 +0000 |
commit | 1f3d3a8e2329b4b7b76f0003ed94146395f15b44 (patch) | |
tree | 3a84a45f9d74f64c0f18cfcbefffb7effba57383 /native_client_sdk | |
parent | 8469c8bd78104229ef956c3a1ab4d05cfaeace7c (diff) | |
download | chromium_src-1f3d3a8e2329b4b7b76f0003ed94146395f15b44.zip chromium_src-1f3d3a8e2329b4b7b76f0003ed94146395f15b44.tar.gz chromium_src-1f3d3a8e2329b4b7b76f0003ed94146395f15b44.tar.bz2 |
Fix getcwd in nacl_io for glibc
__nacl_irt_getcwd returns int, not char*, and it does not
allocate buffer for NULL argument. It is libc's responsibility
to adjust the arguments of getcwd. So we should not adjust
the length argument either. See NaClSysGetcwd.
TEST=./build_tools/build_sdk.py
TEST=./build_tools/test_sdk.py
TEST=Now bash in naclports successfully calls getcwd.
Review URL: https://codereview.chromium.org/140993003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@246629 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'native_client_sdk')
4 files changed, 34 insertions, 13 deletions
diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_proxy.cc b/native_client_sdk/src/libraries/nacl_io/kernel_proxy.cc index 6ddffd3..615e7d2 100644 --- a/native_client_sdk/src/libraries/nacl_io/kernel_proxy.cc +++ b/native_client_sdk/src/libraries/nacl_io/kernel_proxy.cc @@ -269,24 +269,19 @@ int KernelProxy::chdir(const char* path) { } char* KernelProxy::getcwd(char* buf, size_t size) { - std::string cwd = GetCWD(); - - // If size is 0, allocate as much as we need. - if (size == 0) { - size = cwd.size() + 1; + if (NULL == buf) { + errno = EFAULT; + return NULL; } + std::string cwd = GetCWD(); + // Verify the buffer is large enough if (size <= cwd.size()) { errno = ERANGE; return NULL; } - // Allocate the buffer if needed - if (buf == NULL) { - buf = static_cast<char*>(malloc(size)); - } - strcpy(buf, cwd.c_str()); return buf; } diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc b/native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc index fc3eb54..d6e44bd 100644 --- a/native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc +++ b/native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc @@ -173,8 +173,10 @@ int WRAP(fstat)(int fd, struct nacl_abi_stat *nacl_buf) { return 0; } -char* WRAP(getcwd)(char* buf, size_t size) { - return ki_getcwd(buf, size); +int WRAP(getcwd)(char* buf, size_t size) { + if (ki_getcwd(buf, size) == NULL) + return errno; + return 0; } int WRAP(getdents)(int fd, dirent* nacl_buf, size_t nacl_count, size_t *nread) { diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_wrap_win.cc b/native_client_sdk/src/libraries/nacl_io/kernel_wrap_win.cc index cf35f96..d617788 100644 --- a/native_client_sdk/src/libraries/nacl_io/kernel_wrap_win.cc +++ b/native_client_sdk/src/libraries/nacl_io/kernel_wrap_win.cc @@ -102,6 +102,17 @@ int _fstat64i32(int fd, struct _stat64i32* buf) { } char* _getcwd(char* buf, int size) { + // If size is 0, allocate as much as we need. + if (size == 0) { + char stack_buf[MAX_PATH + 1]; + if (!ki_getcwd(stack_buf, MAX_PATH)) + return NULL; + size = strlen(stack_buf) + 1; + } + // Allocate the buffer if needed + if (buf == NULL) { + buf = static_cast<char*>(malloc(size)); + } return ki_getcwd(buf, size); } @@ -225,4 +236,3 @@ void kernel_wrap_uninit() { EXTERN_C_END #endif // defined(WIN32) - diff --git a/native_client_sdk/src/libraries/nacl_io/syscalls/getcwd.c b/native_client_sdk/src/libraries/nacl_io/syscalls/getcwd.c index e9ca705..aff35d8 100644 --- a/native_client_sdk/src/libraries/nacl_io/syscalls/getcwd.c +++ b/native_client_sdk/src/libraries/nacl_io/syscalls/getcwd.c @@ -2,9 +2,23 @@ * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ +#include <limits.h> +#include <string.h> + #include "nacl_io/kernel_intercept.h" #include "nacl_io/kernel_wrap.h" char* getcwd(char* buf, size_t size) { + // If size is 0, allocate as much as we need. + if (size == 0) { + char stack_buf[PATH_MAX + 1]; + if (!ki_getcwd(stack_buf, PATH_MAX)) + return NULL; + size = strlen(stack_buf) + 1; + } + // Allocate the buffer if needed + if (buf == NULL) { + buf = (char*)malloc(size); + } return ki_getcwd(buf, size); } |