From 5ab6f1cef68349c717b7e4e77652ffe89f56b7c3 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Fri, 4 Sep 2020 12:50:52 +0300 Subject: [PATCH] Make readdir reentrant Remove the static allocation for the dirent, and allocate it from the heap during opendir(). Removing the static data can reduce RAM usage on some toolchains when directories are not being used. The static allocation sometimes is combined with the file handle array and can't be dropped by the linker. Original readdir() was not thread-safe at all. This was in violation of POSIX which states the result of readdir "is not overwritten by another call to readdir() on a different directory stream." POSIX also defines readdir_r() as separate totally reentrant form where the caller allocates the dirent, but this is generally deprecated as it opens the door for an inadequate allocation causing a stack smash. Full reentrancy is not typically necessary - having readdir()'s buffer data be per-DIR is generally sufficient. --- platform/include/platform/mbed_retarget.h | 5 +--- platform/source/mbed_retarget.cpp | 30 ++++++++++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/platform/include/platform/mbed_retarget.h b/platform/include/platform/mbed_retarget.h index 380c8d25e6..b40125fcf3 100644 --- a/platform/include/platform/mbed_retarget.h +++ b/platform/include/platform/mbed_retarget.h @@ -184,11 +184,8 @@ FileHandle *mbed_override_console(int fd); */ FileHandle *mbed_file_handle(int fd); } - -typedef mbed::DirHandle DIR; -#else -typedef struct Dir DIR; #endif +typedef struct DIR_impl DIR; #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY /* The intent of this section is to unify the errno error values to match diff --git a/platform/source/mbed_retarget.cpp b/platform/source/mbed_retarget.cpp index f2f7774ea6..44b517d934 100644 --- a/platform/source/mbed_retarget.cpp +++ b/platform/source/mbed_retarget.cpp @@ -46,6 +46,12 @@ static SingletonPtr _mutex; +/* DIR is typedeffed to struct DIR_impl in header */ +struct DIR_impl { + mbed::DirHandle *handle; + struct dirent entry; +}; + #if defined(__ARMCC_VERSION) # include # include @@ -1297,9 +1303,15 @@ extern "C" DIR *opendir(const char *path) return NULL; } - DirHandle *dir; - int err = fs->open(&dir, fp.fileName()); + DIR *dir = new (std::nothrow) DIR; + if (!dir) { + errno = ENOMEM; + return NULL; + } + + int err = fs->open(&dir->handle, fp.fileName()); if (err < 0) { + delete dir; errno = -err; return NULL; } @@ -1309,8 +1321,7 @@ extern "C" DIR *opendir(const char *path) extern "C" struct dirent *readdir(DIR *dir) { - static struct dirent ent; - int err = dir->read(&ent); + int err = dir->handle->read(&dir->entry); if (err < 1) { if (err < 0) { errno = -err; @@ -1318,12 +1329,13 @@ extern "C" struct dirent *readdir(DIR *dir) return NULL; } - return &ent; + return &dir->entry; } extern "C" int closedir(DIR *dir) { - int err = dir->close(); + int err = dir->handle->close(); + delete dir; if (err < 0) { errno = -err; return -1; @@ -1334,17 +1346,17 @@ extern "C" int closedir(DIR *dir) extern "C" void rewinddir(DIR *dir) { - dir->rewind(); + dir->handle->rewind(); } extern "C" off_t telldir(DIR *dir) { - return dir->tell(); + return dir->handle->tell(); } extern "C" void seekdir(DIR *dir, off_t off) { - dir->seek(off); + dir->handle->seek(off); } extern "C" int mkdir(const char *path, mode_t mode)