aboutsummaryrefslogtreecommitdiffstats
path: root/libc/bionic/getcwd.cpp
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2012-10-09 17:14:56 -0700
committerElliott Hughes <enh@google.com>2012-10-09 17:17:24 -0700
commit156da966214957c5616a0b83cc84686eedfc4e31 (patch)
tree86ff70465d7ef24b343d0cf29724b50ace96aaad /libc/bionic/getcwd.cpp
parent9a7366e8943990619c494548050aa906f1971332 (diff)
downloadandroid_bionic-156da966214957c5616a0b83cc84686eedfc4e31.tar.gz
android_bionic-156da966214957c5616a0b83cc84686eedfc4e31.tar.bz2
android_bionic-156da966214957c5616a0b83cc84686eedfc4e31.zip
Fix a getcwd(3) bug and make our tests run correctly under valgrind.
The getcwd(3) bug was found by valgrind. Bug: 7291287 Change-Id: I59f3bff1c1392a408b905934eebcd5d894d37492
Diffstat (limited to 'libc/bionic/getcwd.cpp')
-rw-r--r--libc/bionic/getcwd.cpp9
1 files changed, 5 insertions, 4 deletions
diff --git a/libc/bionic/getcwd.cpp b/libc/bionic/getcwd.cpp
index 2ff22dbca..47c807f01 100644
--- a/libc/bionic/getcwd.cpp
+++ b/libc/bionic/getcwd.cpp
@@ -40,22 +40,23 @@ char* getcwd(char* buf, size_t size) {
// Allocate a buffer if necessary.
char* allocated_buf = NULL;
+ size_t allocated_size = size;
if (buf == NULL) {
- size_t allocated_size = size;
if (size == 0) {
// The Linux kernel won't return more than a page, so translate size 0 to 4KiB.
// TODO: if we need to support paths longer than that, we'll have to walk the tree ourselves.
- size = getpagesize();
+ allocated_size = getpagesize();
}
buf = allocated_buf = static_cast<char*>(malloc(allocated_size));
if (buf == NULL) {
- // malloc set errno.
+ // malloc should set errno, but valgrind's malloc wrapper doesn't.
+ errno = ENOMEM;
return NULL;
}
}
// Ask the kernel to fill our buffer.
- int rc = __getcwd(buf, size);
+ int rc = __getcwd(buf, allocated_size);
if (rc == -1) {
free(allocated_buf);
// __getcwd set errno.