summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorotto <otto@openbsd.org>2005-01-05 19:48:08 +0000
committerotto <otto@openbsd.org>2005-01-05 19:48:08 +0000
commitc1303ccb8cbab2809c6e4eb45dab8ea8773cb8b5 (patch)
treee21fb4981a4dff206656d21574eee37b5f1a3ddb
parentPartially revert some of these changes - misunderstanding between drahn@ and I (diff)
downloadwireguard-openbsd-c1303ccb8cbab2809c6e4eb45dab8ea8773cb8b5.tar.xz
wireguard-openbsd-c1303ccb8cbab2809c6e4eb45dab8ea8773cb8b5.zip
Fix handling of memory allocation. Both the initial value of eup
and the new value of bup after realloc() were bogus. This bug has been here since the net.2 days. Additionally, make the initial size of the malloc'ed pieces of mem more sane and kill a redundant test before free(). getcwd(3) is now able to return really long paths. Problem spotted by Peter Philipp <philipp at scan-plus dot de> ok millert@ deraadt@
-rw-r--r--lib/libc/gen/getcwd.c15
1 files changed, 7 insertions, 8 deletions
diff --git a/lib/libc/gen/getcwd.c b/lib/libc/gen/getcwd.c
index 42a197b1efa..bbc753b6ec0 100644
--- a/lib/libc/gen/getcwd.c
+++ b/lib/libc/gen/getcwd.c
@@ -28,7 +28,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char rcsid[] = "$OpenBSD: getcwd.c,v 1.9 2003/06/11 21:03:10 deraadt Exp $";
+static char rcsid[] = "$OpenBSD: getcwd.c,v 1.10 2005/01/05 19:48:08 otto Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
@@ -74,7 +74,7 @@ getcwd(char *pt, size_t size)
}
ept = pt + size;
} else {
- if ((pt = malloc(ptsize = 1024 - 4)) == NULL)
+ if ((pt = malloc(ptsize = MAXPATHLEN)) == NULL)
return (NULL);
ept = pt + ptsize;
}
@@ -82,13 +82,13 @@ getcwd(char *pt, size_t size)
*bpt = '\0';
/*
- * Allocate bytes (1024 - malloc space) for the string of "../"'s.
+ * Allocate bytes for the string of "../"'s.
* Should always be enough (it's 340 levels). If it's not, allocate
* as necessary. Special * case the first stat, it's ".", not "..".
*/
- if ((up = malloc(upsize = 1024 - 4)) == NULL)
+ if ((up = malloc(upsize = MAXPATHLEN)) == NULL)
goto err;
- eup = up + MAXPATHLEN;
+ eup = up + upsize;
bup = up;
up[0] = '.';
up[1] = '\0';
@@ -133,8 +133,8 @@ getcwd(char *pt, size_t size)
if ((nup = realloc(up, upsize *= 2)) == NULL)
goto err;
+ bup = nup + (bup - up);
up = nup;
- bup = up;
eup = up + upsize;
}
*bup++ = '.';
@@ -224,8 +224,7 @@ notfound:
err:
if (ptsize)
free(pt);
- if (up)
- free(up);
+ free(up);
if (dir)
(void)closedir(dir);
return (NULL);