summaryrefslogtreecommitdiff
path: root/libc/sysdeps/linux/common/getcwd.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2002-12-04 00:08:52 +0000
committerEric Andersen <andersen@codepoet.org>2002-12-04 00:08:52 +0000
commitbe4e6bbed0ccd09c605bff2671cd9c79b9c82217 (patch)
treebf3dcb2258f18e74218f9e2068862734839a0fbd /libc/sysdeps/linux/common/getcwd.c
parente766a1819da2331c964793379d24e204e3c0fa69 (diff)
Properly allocate memory when size is 0, but so is buf
Diffstat (limited to 'libc/sysdeps/linux/common/getcwd.c')
-rw-r--r--libc/sysdeps/linux/common/getcwd.c53
1 files changed, 28 insertions, 25 deletions
diff --git a/libc/sysdeps/linux/common/getcwd.c b/libc/sysdeps/linux/common/getcwd.c
index 9cdd84b47..9e64a3dd8 100644
--- a/libc/sysdeps/linux/common/getcwd.c
+++ b/libc/sysdeps/linux/common/getcwd.c
@@ -17,33 +17,36 @@ static inline _syscall2(int, __syscall_getcwd, char *, buf, unsigned long, size)
char *getcwd(char *buf, int size)
{
- int olderrno, ret;
- char *allocbuf;
-
- if (size == 0) {
- __set_errno(EINVAL);
- return NULL;
- }
- if (size < 3) {
- __set_errno(ERANGE);
- return NULL;
- }
- allocbuf=NULL;
- olderrno = errno;
- if (buf == NULL) {
- buf = allocbuf = malloc (size);
- if (buf == NULL)
- return NULL;
+ int olderrno, ret;
+ char *allocbuf;
+
+ if (size == 0) {
+ if (buf != NULL) {
+ __set_errno(EINVAL);
+ return NULL;
}
- ret = INLINE_SYSCALL(getcwd, 2, buf, size);
- if (ret < 0) {
- if (allocbuf) {
- free(allocbuf);
- }
+ size = PATH_MAX;
+ }
+ if (size < 3) {
+ __set_errno(ERANGE);
+ return NULL;
+ }
+ allocbuf=NULL;
+ olderrno = errno;
+ if (buf == NULL) {
+ buf = allocbuf = malloc (size);
+ if (buf == NULL)
return NULL;
- }
- __set_errno(olderrno);
- return buf;
+ }
+ ret = INLINE_SYSCALL(getcwd, 2, buf, size);
+ if (ret < 0) {
+ if (allocbuf) {
+ free(allocbuf);
+ }
+ return NULL;
+ }
+ __set_errno(olderrno);
+ return buf;
}
#else