Skip to content

Commit d410a6e

Browse files
anchaoxiaoxiang781216
authored andcommitted
libc/realpath: allocate link buffer of pseudofs to save stack
The link buffer of pseudofs will occupy too much of stack, allocate from the heap to save the stack usage. Signed-off-by: chao an <anchao@xiaomi.com>
1 parent 4d65d99 commit d410a6e

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

libs/libc/stdlib/lib_realpath.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
FAR char *realpath(FAR const char *path, FAR char *resolved)
4040
{
4141
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
42-
char wbuf[2][PATH_MAX];
42+
FAR char *wbuf[2] =
43+
{
44+
};
45+
4346
int nlnk = 0;
4447
int idx = 0;
4548
ssize_t n;
@@ -117,6 +120,14 @@ FAR char *realpath(FAR const char *path, FAR char *resolved)
117120
}
118121

119122
*p = '\0';
123+
124+
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
125+
if (wbuf[0] != NULL)
126+
{
127+
lib_free(wbuf[0]);
128+
}
129+
#endif
130+
120131
return resolved;
121132
}
122133

@@ -186,7 +197,19 @@ FAR char *realpath(FAR const char *path, FAR char *resolved)
186197
goto out;
187198
}
188199

189-
n = readlink(resolved, wbuf[idx], sizeof(wbuf[0]) - 1);
200+
if (wbuf[0] == NULL)
201+
{
202+
wbuf[0] = lib_calloc(2, PATH_MAX);
203+
if (wbuf[0] == NULL)
204+
{
205+
set_errno(ENOMEM);
206+
goto out;
207+
}
208+
209+
wbuf[1] = wbuf[0] + PATH_MAX;
210+
}
211+
212+
n = readlink(resolved, wbuf[idx], PATH_MAX - 1);
190213
if (n <= 0)
191214
{
192215
if (n == 0)
@@ -199,7 +222,7 @@ FAR char *realpath(FAR const char *path, FAR char *resolved)
199222

200223
/* Append unresolved path to link target and switch to it. */
201224

202-
if (n + (len = strlen(q)) + 1 > sizeof(wbuf[0]))
225+
if (n + (len = strlen(q)) + 1 > PATH_MAX)
203226
{
204227
set_errno(ENAMETOOLONG);
205228
goto out;
@@ -234,5 +257,12 @@ FAR char *realpath(FAR const char *path, FAR char *resolved)
234257

235258
out:
236259
lib_free(fres);
260+
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
261+
if (wbuf[0] != NULL)
262+
{
263+
lib_free(wbuf[0]);
264+
}
265+
#endif
266+
237267
return NULL;
238268
}

0 commit comments

Comments
 (0)