From 817e3cd9522e39642a2d809442fd269c4712844c Mon Sep 17 00:00:00 2001 From: Ramakrishna Pallala Date: Tue, 29 May 2018 10:49:48 +0530 Subject: [PATCH] lib: posix: Make sure the name string is NULL terminated Make sure the name string is NULL terminated in the readdir(). CID: 186037 Fixes Issue #7733 Signed-off-by: Ramakrishna Pallala --- lib/posix/fs.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/posix/fs.c b/lib/posix/fs.c index 70be3728e0d..460de8dc242 100644 --- a/lib/posix/fs.c +++ b/lib/posix/fs.c @@ -11,6 +11,9 @@ #include #include +BUILD_ASSERT_MSG(PATH_MAX > MAX_FILE_NAME, + "PATH_MAX is less than MAX_FILE_NAME"); + union file_desc { struct fs_file_t file; struct fs_dir_t dir; @@ -290,8 +293,11 @@ struct dirent *readdir(DIR *dirp) } rc = strlen(fdirent.name); - memcpy(pdirent.d_name, fdirent.name, - rc <= PATH_MAX ? rc : PATH_MAX); + rc = (rc <= PATH_MAX) ? rc : PATH_MAX; + memcpy(pdirent.d_name, fdirent.name, rc); + + /* Make sure the name is NULL terminated */ + pdirent.d_name[rc + 1] = '\0'; return &pdirent; }