@@ -71,8 +71,11 @@ int vfs_resolve_mount(const char* path, vfs_mount_res_t* out) {
7171 return 0 ;
7272}
7373
74- static void vfs_normalize_path (const char * in , char * out ) {
75- memset (out , 0 , 256 );
74+ int vfs_normalize_path (const char * in , char * out , size_t out_sz ) {
75+ if (!in || !out || out_sz < 2 )
76+ return -1 ;
77+
78+ memset (out , 0 , out_sz );
7679 char tmp [256 ];
7780
7881 // Start with absolute or relative
@@ -113,16 +116,17 @@ static void vfs_normalize_path(const char* in, char* out) {
113116
114117 // Copy next component
115118 while (* p && * p != '/' ) {
116- if (oi >= 255 ) {
117- out [255 ] = '\0' ;
118- return ;
119+ if (( size_t ) oi >= out_sz - 1 ) {
120+ out [out_sz - 1 ] = '\0' ;
121+ return -2 ;
119122 }
120123 out [oi ++ ] = * p ++ ;
121124 }
122125 }
123126
124127 if (oi == 0 ) out [oi ++ ] = '/' ;
125128 out [oi ] = '\0' ;
129+ return 0 ;
126130}
127131
128132int vfs_read (vfs_file_t * file , uint8_t * buf , uint32_t size )
@@ -215,7 +219,8 @@ int vfs_ls(const char* path)
215219 }
216220
217221 char norm [256 ];
218- vfs_normalize_path (path , norm );
222+ if (vfs_normalize_path (path , norm , sizeof (norm )) != 0 )
223+ return -1 ;
219224
220225 vfs_mount_res_t res ;
221226 if (vfs_resolve_mount (norm , & res ) != 0 )
@@ -322,14 +327,13 @@ int vfs_open(const char* path, int flags, vfs_file_t* out)
322327 }
323328
324329 char norm [256 ];
325- vfs_normalize_path (path , norm );
330+ if (vfs_normalize_path (path , norm , sizeof (norm )) != 0 )
331+ return -1 ;
326332
327333 vfs_mount_res_t res ;
328334 if (vfs_resolve_mount (norm , & res ) != 0 )
329335 return -2 ;
330336
331- printf ("[OPEN] %s" , norm );
332-
333337 if (res .mnt -> type == FS_PROC ) {
334338 strncpy (out -> rel_path , res .rel_path , sizeof (out -> rel_path ));
335339 out -> mnt = res .mnt ;
@@ -454,7 +458,8 @@ int vfs_mkdir(const char* path) {
454458 }
455459
456460 char norm [256 ];
457- vfs_normalize_path (path , norm );
461+ if (vfs_normalize_path (path , norm , sizeof (norm )) != 0 )
462+ return -1 ;
458463
459464 vfs_mount_res_t res ;
460465 if (vfs_resolve_mount (norm , & res ) != 0 ) return -1 ;
@@ -477,7 +482,8 @@ int vfs_mkdir(const char* path) {
477482int vfs_rm_recursive (const char * path )
478483{
479484 char norm [256 ];
480- vfs_normalize_path (path , norm );
485+ if (vfs_normalize_path (path , norm , sizeof (norm )) != 0 )
486+ return -1 ;
481487
482488 vfs_mount_res_t res ;
483489 if (vfs_resolve_mount (norm , & res ) != 0 )
@@ -545,7 +551,8 @@ int vfs_cd(const char* path)
545551 }
546552
547553 char norm [256 ];
548- vfs_normalize_path (path , norm );
554+ if (vfs_normalize_path (path , norm , sizeof (norm )) != 0 )
555+ return -1 ;
549556
550557 vfs_mount_res_t res ;
551558 if (vfs_resolve_mount (norm , & res ) != 0 )
@@ -633,7 +640,8 @@ int vfs_create_path(const char* path, uint8_t attr) {
633640 }
634641
635642 char norm [256 ];
636- vfs_normalize_path (path , norm );
643+ if (vfs_normalize_path (path , norm , sizeof (norm )) != 0 )
644+ return -1 ;
637645
638646 vfs_mount_res_t res ;
639647 if (vfs_resolve_mount (norm , & res ) != 0 ) return -1 ;
@@ -659,7 +667,8 @@ int vfs_unlink(const char* path)
659667 }
660668 /* Normalize */
661669 char norm [256 ];
662- vfs_normalize_path (path , norm );
670+ if (vfs_normalize_path (path , norm , sizeof (norm )) != 0 )
671+ return -1 ;
663672
664673 /* Resolve mount */
665674 vfs_mount_res_t res ;
@@ -717,8 +726,10 @@ int vfs_unlink(const char* path)
717726int vfs_mv (const char * src , const char * dst )
718727{
719728 char src_norm [256 ], dst_norm [256 ];
720- vfs_normalize_path (src , src_norm );
721- vfs_normalize_path (dst , dst_norm );
729+ if (vfs_normalize_path (src , src_norm , sizeof (src_norm )) != 0 )
730+ return -1 ;
731+ if (vfs_normalize_path (dst , dst_norm , sizeof (dst_norm )) != 0 )
732+ return -1 ;
722733
723734 vfs_mount_res_t src_res , dst_res ;
724735 if (vfs_resolve_mount (src_norm , & src_res ) != 0 ) return -1 ;
@@ -825,4 +836,4 @@ const char* vfs_basename(const char* path) {
825836 path ++ ;
826837 }
827838 return last ;
828- }
839+ }
0 commit comments