mirror of
https://github.com/u-boot/u-boot.git
synced 2024-12-02 09:03:27 +08:00
sandbox/fs: Use correct size path name buffer
The readdir linux manpage explicitly states (quoting POSIX.1) that sizeof(d_name) is not correct for determining the required size, but to always use strlen. Grow the buffer if needed. Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Acked-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
parent
ce2ec19c56
commit
f189899c2f
@ -320,14 +320,16 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
|
|||||||
int ret;
|
int ret;
|
||||||
char *fname;
|
char *fname;
|
||||||
int len;
|
int len;
|
||||||
|
int dirlen;
|
||||||
|
|
||||||
*headp = NULL;
|
*headp = NULL;
|
||||||
dir = opendir(dirname);
|
dir = opendir(dirname);
|
||||||
if (!dir)
|
if (!dir)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
/* Create a buffer for the maximum filename length */
|
/* Create a buffer upfront, with typically sufficient size */
|
||||||
len = sizeof(entry.d_name) + strlen(dirname) + 2;
|
dirlen = strlen(dirname) + 2;
|
||||||
|
len = dirlen + 256;
|
||||||
fname = malloc(len);
|
fname = malloc(len);
|
||||||
if (!fname) {
|
if (!fname) {
|
||||||
ret = -ENOMEM;
|
ret = -ENOMEM;
|
||||||
@ -339,7 +341,12 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
|
|||||||
if (ret || !result)
|
if (ret || !result)
|
||||||
break;
|
break;
|
||||||
next = malloc(sizeof(*node) + strlen(entry.d_name) + 1);
|
next = malloc(sizeof(*node) + strlen(entry.d_name) + 1);
|
||||||
if (!next) {
|
if (dirlen + strlen(entry.d_name) > len) {
|
||||||
|
len = dirlen + strlen(entry.d_name);
|
||||||
|
fname = realloc(fname, len);
|
||||||
|
}
|
||||||
|
if (!next || !fname) {
|
||||||
|
free(next);
|
||||||
os_dirent_free(head);
|
os_dirent_free(head);
|
||||||
ret = -ENOMEM;
|
ret = -ENOMEM;
|
||||||
goto done;
|
goto done;
|
||||||
|
Loading…
Reference in New Issue
Block a user