From 5cf1193902a259392e8c3d6991b6739815f87040 Mon Sep 17 00:00:00 2001 From: Rico Ganahl Date: Mon, 3 Jan 2022 12:44:16 +0100 Subject: [PATCH] lib/posix/fs: fix end-of-dir in readdir POSIX readdir should return NULL if end of dir is reached and leave errno untouched. Signed-off-by: Rico Ganahl --- lib/posix/fs.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/posix/fs.c b/lib/posix/fs.c index 0b479039b55..04e4b872019 100644 --- a/lib/posix/fs.c +++ b/lib/posix/fs.c @@ -290,6 +290,11 @@ struct dirent *readdir(DIR *dirp) return NULL; } + if (fdirent.name[0] == 0) { + /* assume end-of-dir, leave errno untouched */ + return NULL; + } + rc = strlen(fdirent.name); rc = (rc < MAX_FILE_NAME) ? rc : (MAX_FILE_NAME - 1); (void)memcpy(pdirent.d_name, fdirent.name, rc);