From 6242c05d6225a17df5cf45dafa5e998ec4bf9b2d Mon Sep 17 00:00:00 2001 From: jblanked <82678820+jblanked@users.noreply.github.com> Date: Mon, 13 Jan 2025 23:39:49 -0500 Subject: [PATCH] Updated to ensure the flip_social data folder is created when saving settings. --- application.fam | 2 +- assets/CHANGELOG.md | 3 +++ flip_social.h | 2 +- flip_storage/flip_social_storage.c | 6 +++++- jsmn/jsmn.c | 19 ++++++++++++++++++- jsmn/jsmn_furi.c | 19 ++++++++++++++++++- jsmn/jsmn_h.c | 1 + jsmn/jsmn_h.h | 5 ++++- 8 files changed, 51 insertions(+), 6 deletions(-) diff --git a/application.fam b/application.fam index 9e53a1d4b4c..8c9e5fbf0bc 100644 --- a/application.fam +++ b/application.fam @@ -9,6 +9,6 @@ App( fap_icon_assets="assets", fap_author="JBlanked", fap_weburl="https://github.com/jblanked/FlipSocial", - fap_version="1.0.2", + fap_version="1.0.3", fap_description="Social media platform for the Flipper Zero.", ) diff --git a/assets/CHANGELOG.md b/assets/CHANGELOG.md index b7c7ca5e2ae..9b54a1a07e0 100644 --- a/assets/CHANGELOG.md +++ b/assets/CHANGELOG.md @@ -1,3 +1,6 @@ +## 1.0.3 +- Updated to ensure the flip_social data folder is created when saving settings. + ## 1.0.2 - Fixed the Feed Type and Notifications toggles, in the User Settings, to work as intended. - Added flip status to each feed post. diff --git a/flip_social.h b/flip_social.h index 86d0f36b08b..e2cf21fc9fc 100644 --- a/flip_social.h +++ b/flip_social.h @@ -10,7 +10,7 @@ #include #define TAG "FlipSocial" -#define VERSION_TAG TAG " v1.0.2" +#define VERSION_TAG TAG " v1.0.3" #define MAX_PRE_SAVED_MESSAGES 20 // Maximum number of pre-saved messages #define MAX_MESSAGE_LENGTH 100 // Maximum length of a message in the feed diff --git a/flip_storage/flip_social_storage.c b/flip_storage/flip_social_storage.c index 9dcfcbd056d..8edfdc49e4f 100644 --- a/flip_storage/flip_social_storage.c +++ b/flip_storage/flip_social_storage.c @@ -389,6 +389,8 @@ bool flip_social_save_post(const char *post_id, const char *json_feed_data) // Create the directory for saving the feed char directory_path[128]; + snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social"); + storage_common_mkdir(storage, directory_path); snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/feed"); storage_common_mkdir(storage, directory_path); @@ -427,11 +429,13 @@ bool save_char( } // Create the directory for saving settings char directory_path[256]; - snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/data"); + snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social"); // Create the directory Storage *storage = furi_record_open(RECORD_STORAGE); storage_common_mkdir(storage, directory_path); + snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/data"); + storage_common_mkdir(storage, directory_path); // Open the settings file File *file = storage_file_alloc(storage); diff --git a/jsmn/jsmn.c b/jsmn/jsmn.c index 88b88284633..d101530c6d0 100644 --- a/jsmn/jsmn.c +++ b/jsmn/jsmn.c @@ -454,6 +454,11 @@ char *get_json_value(char *key, const char *json_data) jsmn_parser parser; jsmn_init(&parser); uint32_t max_tokens = json_token_count(json_data); + if (!jsmn_memory_check(max_tokens)) + { + FURI_LOG_E("JSMM.H", "Insufficient memory for JSON tokens."); + return NULL; + } // Allocate tokens array on the heap jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * max_tokens); if (tokens == NULL) @@ -572,6 +577,12 @@ char *get_json_array_value(char *key, uint32_t index, const char *json_data) return NULL; } uint32_t max_tokens = json_token_count(array_str); + if (!jsmn_memory_check(max_tokens)) + { + FURI_LOG_E("JSMM.H", "Insufficient memory for JSON tokens."); + free(array_str); + return NULL; + } jsmn_parser parser; jsmn_init(&parser); @@ -602,7 +613,7 @@ char *get_json_array_value(char *key, uint32_t index, const char *json_data) if (index >= (uint32_t)tokens[0].size) { - FURI_LOG_E("JSMM.H", "Index %lu out of bounds for array with size %u.", index, tokens[0].size); + // FURI_LOG_E("JSMM.H", "Index %lu out of bounds for array with size %u.", index, tokens[0].size); free(tokens); free(array_str); return NULL; @@ -654,6 +665,12 @@ char **get_json_array_values(char *key, char *json_data, int *num_values) return NULL; } uint32_t max_tokens = json_token_count(array_str); + if (!jsmn_memory_check(max_tokens)) + { + FURI_LOG_E("JSMM.H", "Insufficient memory for JSON tokens."); + free(array_str); + return NULL; + } // Initialize the JSON parser jsmn_parser parser; jsmn_init(&parser); diff --git a/jsmn/jsmn_furi.c b/jsmn/jsmn_furi.c index ae4adc38de0..d3bde9365fe 100644 --- a/jsmn/jsmn_furi.c +++ b/jsmn/jsmn_furi.c @@ -481,6 +481,11 @@ FuriString *get_json_value_furi(const char *key, const FuriString *json_data) return NULL; } uint32_t max_tokens = json_token_count_furi(json_data); + if (!jsmn_memory_check(max_tokens)) + { + FURI_LOG_E("JSMM.H", "Insufficient memory for JSON tokens."); + return NULL; + } // Create a temporary FuriString from key FuriString *key_str = furi_string_alloc(); furi_string_cat_str(key_str, key); @@ -546,6 +551,12 @@ FuriString *get_json_array_value_furi(const char *key, uint32_t index, const Fur return NULL; } uint32_t max_tokens = json_token_count_furi(array_str); + if (!jsmn_memory_check(max_tokens)) + { + FURI_LOG_E("JSMM.H", "Insufficient memory for JSON tokens."); + furi_string_free(array_str); + return NULL; + } jsmn_parser parser; jsmn_init_furi(&parser); @@ -576,7 +587,7 @@ FuriString *get_json_array_value_furi(const char *key, uint32_t index, const Fur if (index >= (uint32_t)tokens[0].size) { - FURI_LOG_E("JSMM.H", "Index %lu out of bounds for array with size %u.", index, tokens[0].size); + // FURI_LOG_E("JSMM.H", "Index %lu out of bounds for array with size %u.", index, tokens[0].size); free(tokens); furi_string_free(array_str); return NULL; @@ -622,6 +633,12 @@ FuriString **get_json_array_values_furi(const char *key, const FuriString *json_ } uint32_t max_tokens = json_token_count_furi(array_str); + if (!jsmn_memory_check(max_tokens)) + { + FURI_LOG_E("JSMM.H", "Insufficient memory for JSON tokens."); + furi_string_free(array_str); + return NULL; + } jsmn_parser parser; jsmn_init_furi(&parser); diff --git a/jsmn/jsmn_h.c b/jsmn/jsmn_h.c index 62b0b08a66d..59480e6e6bd 100644 --- a/jsmn/jsmn_h.c +++ b/jsmn/jsmn_h.c @@ -12,3 +12,4 @@ FuriString *char_to_furi_string(const char *str) } return furi_str; } +bool jsmn_memory_check(size_t heap_size) { return memmgr_get_free_heap() > (heap_size + 1024); } \ No newline at end of file diff --git a/jsmn/jsmn_h.h b/jsmn/jsmn_h.h index 0d0c2131a41..97d53e7ffef 100644 --- a/jsmn/jsmn_h.h +++ b/jsmn/jsmn_h.h @@ -50,4 +50,7 @@ typedef struct FuriString *value; } FuriJSON; -FuriString *char_to_furi_string(const char *str); \ No newline at end of file +FuriString *char_to_furi_string(const char *str); + +// check memory +bool jsmn_memory_check(size_t heap_size);