]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
sandbox/fs: Use correct size path name buffer
authorStefan Brüns <stefan.bruens@rwth-aachen.de>
Sat, 1 Oct 2016 18:41:40 +0000 (20:41 +0200)
committerSimon Glass <sjg@chromium.org>
Tue, 11 Oct 2016 16:17:07 +0000 (10:17 -0600)
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>
arch/sandbox/cpu/os.c

index c71882a731778e9c94daeff74739e3d484f6c94b..16af3f5eea8b4cca7cc34b817a848ea56a7da032 100644 (file)
@@ -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;