@@ -49,13 +49,25 @@ int dfs_romfs_ioctl(struct dfs_fd *file, int cmd, void *args)
4949 return - DFS_STATUS_EIO ;
5050}
5151
52+ rt_inline int check_dirent (struct romfs_dirent * dirent )
53+ {
54+ if (!(dirent -> type == ROMFS_DIRENT_FILE || dirent -> type == ROMFS_DIRENT_DIR ) ||
55+ (dirent -> size == 0 || dirent -> size == ~0 ))
56+ return -1 ;
57+ return 0 ;
58+ }
59+
5260struct romfs_dirent * dfs_romfs_lookup (struct romfs_dirent * root_dirent , const char * path , rt_size_t * size )
5361{
5462 rt_size_t index , found ;
5563 const char * subpath , * subpath_end ;
5664 struct romfs_dirent * dirent ;
5765 rt_size_t dirent_size ;
5866
67+ /* Check the root_dirent. */
68+ if (check_dirent (root_dirent ) != 0 )
69+ return RT_NULL ;
70+
5971 if (path [0 ] == '/' && path [1 ] == '\0' )
6072 {
6173 * size = root_dirent -> size ;
@@ -82,6 +94,8 @@ struct romfs_dirent *dfs_romfs_lookup(struct romfs_dirent *root_dirent, const ch
8294 /* search in folder */
8395 for (index = 0 ; index < dirent_size ; index ++ )
8496 {
97+ if (check_dirent (& dirent [index ]) != 0 )
98+ return RT_NULL ;
8599 if (rt_strncmp (dirent [index ].name , subpath , (subpath_end - subpath )) == 0 )
86100 {
87101 dirent_size = dirent [index ].size ;
@@ -133,6 +147,11 @@ int dfs_romfs_read(struct dfs_fd *file, void *buf, rt_size_t count)
133147 dirent = (struct romfs_dirent * )file -> data ;
134148 RT_ASSERT (dirent != RT_NULL );
135149
150+ if (check_dirent (dirent ) != 0 )
151+ {
152+ return - DFS_STATUS_EIO ;
153+ }
154+
136155 if (count < file -> size - file -> pos )
137156 length = count ;
138157 else
@@ -172,6 +191,9 @@ int dfs_romfs_open(struct dfs_fd *file)
172191
173192 root_dirent = (struct romfs_dirent * )file -> fs -> data ;
174193
194+ if (check_dirent (dirent ) != 0 )
195+ return - DFS_STATUS_EIO ;
196+
175197 if (file -> flags & (DFS_O_CREAT | DFS_O_WRONLY | DFS_O_APPEND | DFS_O_TRUNC | DFS_O_RDWR ))
176198 return - DFS_STATUS_EINVAL ;
177199
@@ -236,16 +258,18 @@ int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t cou
236258 struct romfs_dirent * dirent , * sub_dirent ;
237259
238260 dirent = (struct romfs_dirent * )file -> data ;
261+ if (check_dirent (dirent ) != 0 )
262+ return - DFS_STATUS_EIO ;
239263 RT_ASSERT (dirent -> type == ROMFS_DIRENT_DIR );
240264
241265 /* enter directory */
242266 dirent = (struct romfs_dirent * )dirent -> data ;
243-
267+
244268 /* make integer count */
245269 count = (count / sizeof (struct dirent ));
246270 if (count == 0 )
247271 return - DFS_STATUS_EINVAL ;
248-
272+
249273 index = 0 ;
250274 for (index = 0 ; index < count && file -> pos < file -> size ; index ++ )
251275 {
@@ -265,13 +289,13 @@ int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t cou
265289 rt_strncpy (d -> d_name , name , rt_strlen (name ) + 1 );
266290
267291 /* move to next position */
268- ++ file -> pos ;
292+ ++ file -> pos ;
269293 }
270294
271295 return index * sizeof (struct dirent );
272296}
273297
274- static const struct dfs_filesystem_operation _romfs =
298+ static const struct dfs_filesystem_operation _romfs =
275299{
276300 "rom" ,
277301 DFS_FS_FLAG_DEFAULT ,
0 commit comments