From 36c05601df7ec7b0c77ce91d9f0f53c88064c51c Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Sun, 16 Oct 2022 12:23:10 +0800 Subject: [PATCH] fs/procfs: Fix warning when all CONFIG_FS_PROCFS_EXCLUDE_xxx are enabled procfs/fs_procfs.c: In function 'procfs_readdir': Error: procfs/fs_procfs.c:719:9: error: unused variable 'pid' [-Werror=unused-variable] 719 | pid_t pid; | ^~~ Error: procfs/fs_procfs.c:716:21: error: unused variable 'tcb' [-Werror=unused-variable] 716 | FAR struct tcb_s *tcb; | ^~~ procfs/fs_procfs.c: At top level: Error: procfs/fs_procfs.c:206:16: error: 'procfs_enum' declared 'static' but never defined [-Werror=unused-function] 206 | static void procfs_enum(FAR struct tcb_s *tcb, FAR void *arg); | ^~~~~~~~~~~ procfs/fs_procfs.c: In function 'procfs_readdir': Error: procfs/fs_procfs.c:878:35: error: array subscript is outside array bounds of 'const struct procfs_entry_s[0]' [-Werror=array-bounds] 878 | if (strncmp(g_procfs_entries[level1->base.index].pathpattern, | ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~ procfs/fs_procfs.c:98:36: note: while referencing 'g_procfs_entries' 98 | static const struct procfs_entry_s g_procfs_entries[] = | ^~~~~~~~~~~~~~~~ Error: procfs/fs_procfs.c:879:35: error: array subscript is outside array bounds of 'const struct procfs_entry_s[0]' [-Werror=array-bounds] 879 | g_procfs_entries[level1->firstindex].pathpattern, | ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~ procfs/fs_procfs.c:98:36: note: while referencing 'g_procfs_entries' 98 | static const struct procfs_entry_s g_procfs_entries[] = | ^~~~~~~~~~~~~~~~ Error: procfs/fs_procfs.c:745:24: error: array subscript is outside array bounds of 'const struct procfs_entry_s[0]' [-Werror=array-bounds] 745 | pentry = &g_procfs_entries[index - priv->nentries]; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ procfs/fs_procfs.c:98:36: note: while referencing 'g_procfs_entries' 98 | static const struct procfs_entry_s g_procfs_entries[] = | ^~~~~~~~~~~~~~~~ Error: procfs/fs_procfs.c:745:41: error: array subscript is outside array bounds of 'const struct procfs_entry_s[0]' [-Werror=array-bounds] 745 | pentry = &g_procfs_entries[index - priv->nentries]; | ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~ Signed-off-by: Xiang Xiao --- fs/procfs/fs_procfs.c | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/fs/procfs/fs_procfs.c b/fs/procfs/fs_procfs.c index 79aa78b08d..41ecc05a04 100644 --- a/fs/procfs/fs_procfs.c +++ b/fs/procfs/fs_procfs.c @@ -193,10 +193,6 @@ static const uint8_t g_procfs_entrycount = sizeof(g_procfs_entries) / * Private Function Prototypes ****************************************************************************/ -/* Helpers */ - -static void procfs_enum(FAR struct tcb_s *tcb, FAR void *arg); - /* File system methods */ static int procfs_open(FAR struct file *filep, FAR const char *relpath, @@ -697,10 +693,8 @@ static int procfs_readdir(FAR struct inode *mountpt, FAR const struct procfs_entry_s *pentry = NULL; FAR struct procfs_dir_priv_s *priv; FAR struct procfs_level0_s *level0; - FAR struct tcb_s *tcb; FAR const char *name = NULL; unsigned int index; - pid_t pid; int ret = -ENOENT; DEBUGASSERT(mountpt && dir); @@ -719,14 +713,16 @@ static int procfs_readdir(FAR struct inode *mountpt, index = priv->index; if (index >= priv->nentries) { + index -= priv->nentries; + /* We must report the next static entry ... no more PID entries. * skip any entries with wildcards in the first segment of the * directory name. */ - while (index < priv->nentries + g_procfs_entrycount) + while (index < g_procfs_entrycount) { - pentry = &g_procfs_entries[index - priv->nentries]; + pentry = &g_procfs_entries[index]; name = pentry->pathpattern; while (*name != '/' && *name != '\0') @@ -757,11 +753,9 @@ static int procfs_readdir(FAR struct inode *mountpt, * fs/nxffs */ - name = - g_procfs_entries[index - priv->nentries].pathpattern; - - if (!level0->lastlen || (strncmp(name, level0->lastread, - level0->lastlen) != 0)) + name = g_procfs_entries[index].pathpattern; + if (!level0->lastlen || + strncmp(name, level0->lastread, level0->lastlen) != 0) { /* Not a duplicate, return the first segment of this * entry @@ -780,16 +774,7 @@ static int procfs_readdir(FAR struct inode *mountpt, /* Test if we are at the end of the directory */ - if (index >= priv->nentries + g_procfs_entrycount) - { - /* We signal the end of the directory by returning the special - * error -ENOENT - */ - - finfo("Entry %d: End of directory\n", index); - ret = -ENOENT; - } - else + if (index < g_procfs_entrycount) { /* Report the next static entry */ @@ -814,7 +799,7 @@ static int procfs_readdir(FAR struct inode *mountpt, /* Advance to next entry for the next read */ - priv->index = index; + priv->index = priv->nentries + index; ret = OK; } } @@ -823,8 +808,8 @@ static int procfs_readdir(FAR struct inode *mountpt, { /* Verify that the pid still refers to an active task/thread */ - pid = level0->pid[index]; - tcb = nxsched_get_tcb(pid); + pid_t pid = level0->pid[index]; + FAR struct tcb_s *tcb = nxsched_get_tcb(pid); if (!tcb) { ferr("ERROR: PID %d is no longer valid\n", (int)pid); @@ -858,7 +843,9 @@ static int procfs_readdir(FAR struct inode *mountpt, * subdirectory are listed in order in the procfs_entry array. */ - if (strncmp(g_procfs_entries[level1->base.index].pathpattern, + if (level1->base.index < g_procfs_entrycount && + level1->firstindex < g_procfs_entrycount && + strncmp(g_procfs_entries[level1->base.index].pathpattern, g_procfs_entries[level1->firstindex].pathpattern, level1->subdirlen) == 0) {