about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2018-05-01 14:46:59 -0400
committerRich Felker <dalias@aerifal.cx>2018-05-01 14:46:59 -0400
commit375840c7d8e1f58a9bf40ced72fbe82635f49489 (patch)
treeed205abe92c31fcc9ee61d16fdc232a1f6e82589 /src
parente3c682ab5257aaa6739ef242a9676d897370e78e (diff)
downloadmusl-375840c7d8e1f58a9bf40ced72fbe82635f49489.tar.gz
musl-375840c7d8e1f58a9bf40ced72fbe82635f49489.tar.xz
musl-375840c7d8e1f58a9bf40ced72fbe82635f49489.zip
avoid excessive stack usage in getcwd
to support the GNU extension of allocating a buffer for getcwd's
result when a null pointer is passed without incurring a link
dependency on free, we use a PATH_MAX-sized buffer on the stack and
only duplicate it to allocated storage after the operation succeeds.
unfortunately this imposed excessive stack usage on all callers,
including those not making use of the GNU extension.

instead, use a VLA to make stack allocation conditional.
Diffstat (limited to 'src')
-rw-r--r--src/unistd/getcwd.c4
1 files changed, 2 insertions, 2 deletions
diff --git a/src/unistd/getcwd.c b/src/unistd/getcwd.c
index 103fbbb5..f407ffe0 100644
--- a/src/unistd/getcwd.c
+++ b/src/unistd/getcwd.c
@@ -6,10 +6,10 @@
 
 char *getcwd(char *buf, size_t size)
 {
-	char tmp[PATH_MAX];
+	char tmp[buf ? 1 : PATH_MAX];
 	if (!buf) {
 		buf = tmp;
-		size = PATH_MAX;
+		size = sizeof tmp;
 	} else if (!size) {
 		errno = EINVAL;
 		return 0;