From 375840c7d8e1f58a9bf40ced72fbe82635f49489 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Tue, 1 May 2018 14:46:59 -0400 Subject: 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. --- src/unistd/getcwd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') 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; -- cgit v1.2.1