@@ -49,13 +49,25 @@ int dfs_romfs_ioctl(struct dfs_fd *file, int cmd, void *args)
49
49
return - DFS_STATUS_EIO ;
50
50
}
51
51
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
+
52
60
struct romfs_dirent * dfs_romfs_lookup (struct romfs_dirent * root_dirent , const char * path , rt_size_t * size )
53
61
{
54
62
rt_size_t index , found ;
55
63
const char * subpath , * subpath_end ;
56
64
struct romfs_dirent * dirent ;
57
65
rt_size_t dirent_size ;
58
66
67
+ /* Check the root_dirent. */
68
+ if (check_dirent (root_dirent ) != 0 )
69
+ return RT_NULL ;
70
+
59
71
if (path [0 ] == '/' && path [1 ] == '\0' )
60
72
{
61
73
* size = root_dirent -> size ;
@@ -82,6 +94,8 @@ struct romfs_dirent *dfs_romfs_lookup(struct romfs_dirent *root_dirent, const ch
82
94
/* search in folder */
83
95
for (index = 0 ; index < dirent_size ; index ++ )
84
96
{
97
+ if (check_dirent (& dirent [index ]) != 0 )
98
+ return RT_NULL ;
85
99
if (rt_strncmp (dirent [index ].name , subpath , (subpath_end - subpath )) == 0 )
86
100
{
87
101
dirent_size = dirent [index ].size ;
@@ -133,6 +147,11 @@ int dfs_romfs_read(struct dfs_fd *file, void *buf, rt_size_t count)
133
147
dirent = (struct romfs_dirent * )file -> data ;
134
148
RT_ASSERT (dirent != RT_NULL );
135
149
150
+ if (check_dirent (dirent ) != 0 )
151
+ {
152
+ return - DFS_STATUS_EIO ;
153
+ }
154
+
136
155
if (count < file -> size - file -> pos )
137
156
length = count ;
138
157
else
@@ -172,6 +191,9 @@ int dfs_romfs_open(struct dfs_fd *file)
172
191
173
192
root_dirent = (struct romfs_dirent * )file -> fs -> data ;
174
193
194
+ if (check_dirent (dirent ) != 0 )
195
+ return - DFS_STATUS_EIO ;
196
+
175
197
if (file -> flags & (DFS_O_CREAT | DFS_O_WRONLY | DFS_O_APPEND | DFS_O_TRUNC | DFS_O_RDWR ))
176
198
return - DFS_STATUS_EINVAL ;
177
199
@@ -236,16 +258,18 @@ int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t cou
236
258
struct romfs_dirent * dirent , * sub_dirent ;
237
259
238
260
dirent = (struct romfs_dirent * )file -> data ;
261
+ if (check_dirent (dirent ) != 0 )
262
+ return - DFS_STATUS_EIO ;
239
263
RT_ASSERT (dirent -> type == ROMFS_DIRENT_DIR );
240
264
241
265
/* enter directory */
242
266
dirent = (struct romfs_dirent * )dirent -> data ;
243
-
267
+
244
268
/* make integer count */
245
269
count = (count / sizeof (struct dirent ));
246
270
if (count == 0 )
247
271
return - DFS_STATUS_EINVAL ;
248
-
272
+
249
273
index = 0 ;
250
274
for (index = 0 ; index < count && file -> pos < file -> size ; index ++ )
251
275
{
@@ -265,13 +289,13 @@ int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t cou
265
289
rt_strncpy (d -> d_name , name , rt_strlen (name ) + 1 );
266
290
267
291
/* move to next position */
268
- ++ file -> pos ;
292
+ ++ file -> pos ;
269
293
}
270
294
271
295
return index * sizeof (struct dirent );
272
296
}
273
297
274
- static const struct dfs_filesystem_operation _romfs =
298
+ static const struct dfs_filesystem_operation _romfs =
275
299
{
276
300
"rom" ,
277
301
DFS_FS_FLAG_DEFAULT ,
0 commit comments