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 <ramakrishna.pallala@intel.com>
This commit is contained in:
Ramakrishna Pallala 2018-05-29 10:49:48 +05:30 committed by Anas Nashif
commit 817e3cd952

View file

@ -11,6 +11,9 @@
#include <posix/unistd.h>
#include <string.h>
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;
}