11/*
2- * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+ * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33 *
44 * SPDX-License-Identifier: Apache-2.0
55 */
@@ -395,20 +395,30 @@ static esp_err_t esp_vfs_make_fs_ops(const esp_vfs_t *vfs, esp_vfs_fs_ops_t **mi
395395 return ESP_ERR_NO_MEM ;
396396}
397397
398- static esp_err_t esp_vfs_register_fs_common (const char * base_path , size_t len , const esp_vfs_fs_ops_t * vfs , int flags , void * ctx , int * vfs_index )
398+ static esp_err_t esp_vfs_register_fs_common (
399+ const char * base_path ,
400+ size_t base_path_len ,
401+ const esp_vfs_fs_ops_t * vfs ,
402+ int flags ,
403+ void * ctx ,
404+ int * vfs_index )
399405{
406+ if (s_vfs_count >= VFS_MAX_COUNT ) {
407+ return ESP_ERR_NO_MEM ;
408+ }
409+
400410 if (vfs == NULL ) {
401411 ESP_LOGE (TAG , "VFS is NULL" );
402412 return ESP_ERR_INVALID_ARG ;
403413 }
404414
405- if (len != LEN_PATH_PREFIX_IGNORED ) {
415+ if (base_path_len != LEN_PATH_PREFIX_IGNORED ) {
406416 /* empty prefix is allowed, "/" is not allowed */
407- if ((len == 1 ) || (len > ESP_VFS_PATH_MAX )) {
417+ if ((base_path_len == 1 ) || (base_path_len > ESP_VFS_PATH_MAX )) {
408418 return ESP_ERR_INVALID_ARG ;
409419 }
410420 /* prefix has to start with "/" and not end with "/" */
411- if (len >= 2 && ((base_path [0 ] != '/' ) || (base_path [len - 1 ] == '/' ))) {
421+ if (base_path_len >= 2 && ((base_path [0 ] != '/' ) || (base_path [base_path_len - 1 ] == '/' ))) {
412422 return ESP_ERR_INVALID_ARG ;
413423 }
414424 }
@@ -426,23 +436,26 @@ static esp_err_t esp_vfs_register_fs_common(const char* base_path, size_t len, c
426436 s_vfs_count ++ ;
427437 }
428438
429- vfs_entry_t * entry = (vfs_entry_t * ) heap_caps_malloc (sizeof (vfs_entry_t ), VFS_MALLOC_FLAGS );
439+ size_t alloc_size = sizeof (vfs_entry_t )
440+ + (base_path_len == LEN_PATH_PREFIX_IGNORED ? 0 : base_path_len + 1 );
441+
442+ vfs_entry_t * entry = (vfs_entry_t * ) heap_caps_malloc (alloc_size , VFS_MALLOC_FLAGS );
430443 if (entry == NULL ) {
431444 return ESP_ERR_NO_MEM ;
432445 }
433446
434447 s_vfs [index ] = entry ;
435- if (len != LEN_PATH_PREFIX_IGNORED ) {
436- strcpy (entry -> path_prefix , base_path ); // we have already verified argument length
437- } else {
438- bzero (entry -> path_prefix , sizeof (entry -> path_prefix ));
439- }
440- entry -> path_prefix_len = len ;
448+
449+ entry -> path_prefix_len = base_path_len ;
441450 entry -> vfs = vfs ;
442451 entry -> ctx = ctx ;
443452 entry -> offset = index ;
444453 entry -> flags = flags ;
445454
455+ if (base_path_len != LEN_PATH_PREFIX_IGNORED ) {
456+ memcpy ((char * )(entry -> path_prefix ), base_path , base_path_len + 1 );
457+ }
458+
446459 if (vfs_index ) {
447460 * vfs_index = index ;
448461 }
0 commit comments