Add limitation for file size to not crash

This commit is contained in:
Aleksandr Skoriy 2022-10-28 08:05:31 +03:00
parent 8e362b087a
commit ba69ee88f3

View file

@ -4,6 +4,8 @@
#define TAG "Archive" #define TAG "Archive"
#define SHOW_MAX_FILE_SIZE 20000
void archive_scene_show_widget_callback(GuiButtonType result, InputType type, void* context) { void archive_scene_show_widget_callback(GuiButtonType result, InputType type, void* context) {
furi_assert(context); furi_assert(context);
ArchiveApp* app = (ArchiveApp*)context; ArchiveApp* app = (ArchiveApp*)context;
@ -29,6 +31,7 @@ void archive_scene_show_on_enter(void* context) {
FileInfo fileinfo; FileInfo fileinfo;
storage_common_stat(fs_api, furi_string_get_cstr(current->path), &fileinfo); storage_common_stat(fs_api, furi_string_get_cstr(current->path), &fileinfo);
if (fileinfo.size < SHOW_MAX_FILE_SIZE) {
storage_file_open(file, furi_string_get_cstr(current->path), FSAM_READ, FSOM_OPEN_EXISTING); storage_file_open(file, furi_string_get_cstr(current->path), FSAM_READ, FSOM_OPEN_EXISTING);
char* content = malloc(fileinfo.size + 1); char* content = malloc(fileinfo.size + 1);
@ -38,14 +41,20 @@ void archive_scene_show_on_enter(void* context) {
widget_add_text_scroll_element( widget_add_text_scroll_element(
instance->widget, 0, 0, 128, 64, content); instance->widget, 0, 0, 128, 64, content);
free(content);
storage_file_close(file);
} else {
widget_add_text_box_element(
instance->widget, 0, 0, 128, 64, AlignLeft, AlignCenter,
"\e#Error:\nFile is too large to show\e#", false);
}
path_extract_filename(current->path, filename, false); path_extract_filename(current->path, filename, false);
// This one to return and cursor select this file // This one to return and cursor select this file
path_extract_filename_no_ext(furi_string_get_cstr(current->path), filename); path_extract_filename_no_ext(furi_string_get_cstr(current->path), filename);
strlcpy(instance->text_store, furi_string_get_cstr(filename), MAX_NAME_LEN); strlcpy(instance->text_store, furi_string_get_cstr(filename), MAX_NAME_LEN);
free(content);
storage_file_close(file);
storage_file_free(file); storage_file_free(file);
furi_string_free(filename); furi_string_free(filename);