提交 375840c7 编写于 作者: R Rich Felker

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.
上级 e3c682ab
...@@ -6,10 +6,10 @@ ...@@ -6,10 +6,10 @@
char *getcwd(char *buf, size_t size) char *getcwd(char *buf, size_t size)
{ {
char tmp[PATH_MAX]; char tmp[buf ? 1 : PATH_MAX];
if (!buf) { if (!buf) {
buf = tmp; buf = tmp;
size = PATH_MAX; size = sizeof tmp;
} else if (!size) { } else if (!size) {
errno = EINVAL; errno = EINVAL;
return 0; return 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册