From 4ecab40fdeb1795edeaf4180ce131c68d95bd485 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Tue, 13 Jun 2023 03:10:10 +0100 Subject: [PATCH] Max file name length is actually 253 + null term More than that and fatfs will truncate like name~1 Also dir_read already accounts for null term in buf size, dont malloc +1 --- applications/debug/unit_tests/rpc/rpc_test.c | 6 +++--- .../scenes/wifi_marauder_scene_script_select.c | 8 +++++--- applications/main/archive/views/archive_browser_view.h | 2 +- applications/services/gui/modules/file_browser_worker.c | 2 +- applications/services/rpc/rpc_storage.c | 4 ++-- applications/services/storage/storage_cli.c | 2 +- applications/services/storage/storage_external_api.c | 4 ++-- lib/flipper_application/plugins/plugin_manager.c | 4 +++- lib/toolbox/dir_walk.c | 8 +++++--- lib/toolbox/tar/tar_archive.c | 2 +- lib/update_util/update_operation.h | 2 +- 11 files changed, 25 insertions(+), 19 deletions(-) diff --git a/applications/debug/unit_tests/rpc/rpc_test.c b/applications/debug/unit_tests/rpc/rpc_test.c index 167266a84..e01f56be1 100644 --- a/applications/debug/unit_tests/rpc/rpc_test.c +++ b/applications/debug/unit_tests/rpc/rpc_test.c @@ -43,7 +43,7 @@ static RpcSessionContext rpc_session[TEST_RPC_SESSIONS]; #define TAG "UnitTestsRpc" #define MAX_RECEIVE_OUTPUT_TIMEOUT 3000 -#define MAX_NAME_LENGTH 255 +#define MAX_NAME_LENGTH 254 #define MAX_DATA_SIZE 512u // have to be exact as in rpc_storage.c #define TEST_DIR TEST_DIR_NAME "/" #define TEST_DIR_NAME EXT_PATH("unit_tests_tmp") @@ -186,7 +186,7 @@ static void clean_directory(Storage* fs_api, const char* clean_dir) { File* dir = storage_file_alloc(fs_api); if(storage_dir_open(dir, clean_dir)) { FileInfo fileinfo; - char* name = malloc(MAX_NAME_LENGTH + 1); + char* name = malloc(MAX_NAME_LENGTH); while(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) { size_t size = strlen(clean_dir) + strlen(name) + 1 + 1; char* fullname = malloc(size); @@ -598,7 +598,7 @@ static void test_rpc_storage_list_create_expected_list( while(!finish) { FileInfo fileinfo; - char* name = malloc(MAX_NAME_LENGTH + 1); + char* name = malloc(MAX_NAME_LENGTH); if(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) { if(i == COUNT_OF(list->file)) { list->file_count = i; diff --git a/applications/external/wifi_marauder_companion/scenes/wifi_marauder_scene_script_select.c b/applications/external/wifi_marauder_companion/scenes/wifi_marauder_scene_script_select.c index bc0746858..ffdc2ded0 100644 --- a/applications/external/wifi_marauder_companion/scenes/wifi_marauder_scene_script_select.c +++ b/applications/external/wifi_marauder_companion/scenes/wifi_marauder_scene_script_select.c @@ -1,5 +1,7 @@ #include "../wifi_marauder_app_i.h" +#define MAX_NAME_LEN 254 + static void wifi_marauder_scene_script_select_callback(void* context, uint32_t index) { WifiMarauderApp* app = context; @@ -35,10 +37,10 @@ void wifi_marauder_scene_script_select_on_enter(void* context) { File* dir_scripts = storage_file_alloc(app->storage); if(storage_dir_open(dir_scripts, MARAUDER_APP_FOLDER_SCRIPTS)) { FileInfo file_info; - char file_path[255]; + char file_path[MAX_NAME_LEN]; app->script_list_count = 0; // Goes through the files in the folder counting the ones that end with the json extension - while(storage_dir_read(dir_scripts, &file_info, file_path, 255)) { + while(storage_dir_read(dir_scripts, &file_info, file_path, MAX_NAME_LEN)) { app->script_list_count++; } if(app->script_list_count > 0) { @@ -48,7 +50,7 @@ void wifi_marauder_scene_script_select_on_enter(void* context) { storage_dir_open(dir_scripts, MARAUDER_APP_FOLDER_SCRIPTS); // Read the files again from the beginning, adding the scripts in the list int script_index = 0; - while(storage_dir_read(dir_scripts, &file_info, file_path, 255)) { + while(storage_dir_read(dir_scripts, &file_info, file_path, MAX_NAME_LEN)) { app->script_list[script_index] = furi_string_alloc(); path_extract_filename_no_ext(file_path, app->script_list[script_index]); submenu_add_item( diff --git a/applications/main/archive/views/archive_browser_view.h b/applications/main/archive/views/archive_browser_view.h index 7d8eef65c..383da4769 100644 --- a/applications/main/archive/views/archive_browser_view.h +++ b/applications/main/archive/views/archive_browser_view.h @@ -15,7 +15,7 @@ #include "gui/modules/file_browser_worker.h" #define MAX_LEN_PX 110 -#define MAX_NAME_LEN 255 +#define MAX_NAME_LEN 254 #define FRAME_HEIGHT 12 #define MENU_ITEMS 5u #define MOVE_OFFSET 5u diff --git a/applications/services/gui/modules/file_browser_worker.c b/applications/services/gui/modules/file_browser_worker.c index 326040020..bde6c02a7 100644 --- a/applications/services/gui/modules/file_browser_worker.c +++ b/applications/services/gui/modules/file_browser_worker.c @@ -16,7 +16,7 @@ #define ASSETS_DIR "assets" #define BROWSER_ROOT STORAGE_ANY_PATH_PREFIX -#define FILE_NAME_LEN_MAX 256 +#define FILE_NAME_LEN_MAX 254 #define LONG_LOAD_THRESHOLD 100 typedef enum { diff --git a/applications/services/rpc/rpc_storage.c b/applications/services/rpc/rpc_storage.c index c3a4a0470..96f43e1d7 100644 --- a/applications/services/rpc/rpc_storage.c +++ b/applications/services/rpc/rpc_storage.c @@ -15,7 +15,7 @@ #define TAG "RpcStorage" -#define MAX_NAME_LENGTH 255 +#define MAX_NAME_LENGTH 254 static const size_t MAX_DATA_SIZE = 512; @@ -282,7 +282,7 @@ static void rpc_system_storage_list_process(const PB_Main* request, void* contex while(!finish) { FileInfo fileinfo; - char* name = malloc(MAX_NAME_LENGTH + 1); + char* name = malloc(MAX_NAME_LENGTH); if(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) { if(path_contains_only_ascii(name)) { if(i == COUNT_OF(list->file)) { diff --git a/applications/services/storage/storage_cli.c b/applications/services/storage/storage_cli.c index d720de9d1..096060d82 100644 --- a/applications/services/storage/storage_cli.c +++ b/applications/services/storage/storage_cli.c @@ -9,7 +9,7 @@ #include #include -#define MAX_NAME_LENGTH 255 +#define MAX_NAME_LENGTH 254 static void storage_cli_print_usage() { printf("Usage:\r\n"); diff --git a/applications/services/storage/storage_external_api.c b/applications/services/storage/storage_external_api.c index 38ff80252..d2c98f290 100644 --- a/applications/services/storage/storage_external_api.c +++ b/applications/services/storage/storage_external_api.c @@ -7,7 +7,7 @@ #include #include "toolbox/path.h" -#define MAX_NAME_LENGTH 256 +#define MAX_NAME_LENGTH 254 #define FILE_BUFFER_SIZE 512 #define TAG "StorageAPI" @@ -893,7 +893,7 @@ bool storage_simply_remove_recursive(Storage* storage, const char* path) { return true; } - char* name = malloc(MAX_NAME_LENGTH + 1); //-V799 + char* name = malloc(MAX_NAME_LENGTH); //-V799 File* dir = storage_file_alloc(storage); cur_dir = furi_string_alloc_set(path); bool go_deeper = false; diff --git a/lib/flipper_application/plugins/plugin_manager.c b/lib/flipper_application/plugins/plugin_manager.c index 101471dc5..ca4d99dcb 100644 --- a/lib/flipper_application/plugins/plugin_manager.c +++ b/lib/flipper_application/plugins/plugin_manager.c @@ -11,6 +11,8 @@ #define TAG "libmgr" +#define MAX_NAME_LEN 254 + ARRAY_DEF(FlipperApplicationList, FlipperApplication*, M_PTR_OPLIST) #define M_OPL_FlipperApplicationList_t() ARRAY_OPLIST(FlipperApplicationList, M_PTR_OPLIST) @@ -103,7 +105,7 @@ PluginManagerError plugin_manager_load_single(PluginManager* manager, const char PluginManagerError plugin_manager_load_all(PluginManager* manager, const char* path) { File* directory = storage_file_alloc(manager->storage); - char file_name_buffer[256]; + char file_name_buffer[MAX_NAME_LEN]; FuriString* file_name = furi_string_alloc(); do { if(!storage_dir_open(directory, path)) { diff --git a/lib/toolbox/dir_walk.c b/lib/toolbox/dir_walk.c index 509ceb5b4..adfeeaea5 100644 --- a/lib/toolbox/dir_walk.c +++ b/lib/toolbox/dir_walk.c @@ -1,6 +1,8 @@ #include "dir_walk.h" #include +#define MAX_NAME_LEN 254 + LIST_DEF(DirIndexList, uint32_t); struct DirWalk { @@ -56,12 +58,12 @@ static bool dir_walk_filter(DirWalk* dir_walk, const char* name, FileInfo* filei static DirWalkResult dir_walk_iter(DirWalk* dir_walk, FuriString* return_path, FileInfo* fileinfo) { DirWalkResult result = DirWalkError; - char* name = malloc(256); // FIXME: remove magic number + char* name = malloc(MAX_NAME_LEN); // FIXME: remove magic number FileInfo info; bool end = false; while(!end) { - storage_dir_read(dir_walk->file, &info, name, 255); + storage_dir_read(dir_walk->file, &info, name, MAX_NAME_LEN); if(storage_file_get_error(dir_walk->file) == FSE_OK) { result = DirWalkOK; @@ -119,7 +121,7 @@ static DirWalkResult break; } - if(!storage_dir_read(dir_walk->file, &info, name, 255)) { + if(!storage_dir_read(dir_walk->file, &info, name, MAX_NAME_LEN)) { result = DirWalkError; end = true; break; diff --git a/lib/toolbox/tar/tar_archive.c b/lib/toolbox/tar/tar_archive.c index fcfc22a70..c8abbd6d4 100644 --- a/lib/toolbox/tar/tar_archive.c +++ b/lib/toolbox/tar/tar_archive.c @@ -6,7 +6,7 @@ #include #define TAG "TarArch" -#define MAX_NAME_LEN 255 +#define MAX_NAME_LEN 254 #define FILE_BLOCK_SIZE 512 #define FILE_OPEN_NTRIES 10 diff --git a/lib/update_util/update_operation.h b/lib/update_util/update_operation.h index 65abf8e15..270d60760 100644 --- a/lib/update_util/update_operation.h +++ b/lib/update_util/update_operation.h @@ -8,7 +8,7 @@ extern "C" { #endif #define UPDATE_OPERATION_ROOT_DIR_PACKAGE_MAGIC 0 -#define UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN 255u +#define UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN 254u #define UPDATE_OPERATION_MIN_MANIFEST_VERSION 2 /*