Skip to content

Commit

Permalink
Make readdir reentrant
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kjbracey committed Sep 4, 2020
1 parent 54f0f56 commit 5ab6f1c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
5 changes: 1 addition & 4 deletions platform/include/platform/mbed_retarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 21 additions & 9 deletions platform/source/mbed_retarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@

static SingletonPtr<PlatformMutex> _mutex;

/* DIR is typedeffed to struct DIR_impl in header */
struct DIR_impl {
mbed::DirHandle *handle;
struct dirent entry;
};

#if defined(__ARMCC_VERSION)
# include <arm_compat.h>
# include <rt_sys.h>
Expand Down Expand Up @@ -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;
}
Expand All @@ -1309,21 +1321,21 @@ 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;
}
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;
Expand All @@ -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)
Expand Down

0 comments on commit 5ab6f1c

Please sign in to comment.