summaryrefslogtreecommitdiff
path: root/arch/sandbox
diff options
context:
space:
mode:
authorStefan Brüns <stefan.bruens@rwth-aachen.de>2016-10-01 20:41:40 +0200
committerSimon Glass <sjg@chromium.org>2016-10-11 10:17:07 -0600
commitf189899c2f9ae2266ea4814cf14f138cc47e319f (patch)
treef2cad4a04e2c8c7970bb524f56ed771950e5b599 /arch/sandbox
parentce2ec19c56d646656e64a4b8e0279820337f089e (diff)
downloadu-boot-imx-f189899c2f9ae2266ea4814cf14f138cc47e319f.zip
u-boot-imx-f189899c2f9ae2266ea4814cf14f138cc47e319f.tar.gz
u-boot-imx-f189899c2f9ae2266ea4814cf14f138cc47e319f.tar.bz2
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>
Diffstat (limited to 'arch/sandbox')
-rw-r--r--arch/sandbox/cpu/os.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index c71882a..16af3f5 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -320,14 +320,16 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
int ret;
char *fname;
int len;
+ int dirlen;
*headp = NULL;
dir = opendir(dirname);
if (!dir)
return -1;
- /* Create a buffer for the maximum filename length */
- len = sizeof(entry.d_name) + strlen(dirname) + 2;
+ /* Create a buffer upfront, with typically sufficient size */
+ dirlen = strlen(dirname) + 2;
+ len = dirlen + 256;
fname = malloc(len);
if (!fname) {
ret = -ENOMEM;
@@ -339,7 +341,12 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
if (ret || !result)
break;
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);
ret = -ENOMEM;
goto done;