mirror of
https://github.com/apache/nuttx.git
synced 2025-12-13 23:17:11 +08:00
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>
This commit is contained in:
@@ -39,7 +39,10 @@
|
|||||||
FAR char *realpath(FAR const char *path, FAR char *resolved)
|
FAR char *realpath(FAR const char *path, FAR char *resolved)
|
||||||
{
|
{
|
||||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||||
char wbuf[2][PATH_MAX];
|
FAR char *wbuf[2] =
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
int nlnk = 0;
|
int nlnk = 0;
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
ssize_t n;
|
ssize_t n;
|
||||||
@@ -117,6 +120,14 @@ loop:
|
|||||||
}
|
}
|
||||||
|
|
||||||
*p = '\0';
|
*p = '\0';
|
||||||
|
|
||||||
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||||
|
if (wbuf[0] != NULL)
|
||||||
|
{
|
||||||
|
lib_free(wbuf[0]);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return resolved;
|
return resolved;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,7 +197,19 @@ loop:
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
n = readlink(resolved, wbuf[idx], sizeof(wbuf[0]) - 1);
|
if (wbuf[0] == NULL)
|
||||||
|
{
|
||||||
|
wbuf[0] = lib_calloc(2, PATH_MAX);
|
||||||
|
if (wbuf[0] == NULL)
|
||||||
|
{
|
||||||
|
set_errno(ENOMEM);
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
wbuf[1] = wbuf[0] + PATH_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
|
n = readlink(resolved, wbuf[idx], PATH_MAX - 1);
|
||||||
if (n <= 0)
|
if (n <= 0)
|
||||||
{
|
{
|
||||||
if (n == 0)
|
if (n == 0)
|
||||||
@@ -199,7 +222,7 @@ loop:
|
|||||||
|
|
||||||
/* Append unresolved path to link target and switch to it. */
|
/* Append unresolved path to link target and switch to it. */
|
||||||
|
|
||||||
if (n + (len = strlen(q)) + 1 > sizeof(wbuf[0]))
|
if (n + (len = strlen(q)) + 1 > PATH_MAX)
|
||||||
{
|
{
|
||||||
set_errno(ENAMETOOLONG);
|
set_errno(ENAMETOOLONG);
|
||||||
goto out;
|
goto out;
|
||||||
@@ -234,5 +257,12 @@ loop:
|
|||||||
|
|
||||||
out:
|
out:
|
||||||
lib_free(fres);
|
lib_free(fres);
|
||||||
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||||
|
if (wbuf[0] != NULL)
|
||||||
|
{
|
||||||
|
lib_free(wbuf[0]);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user