From 91c8633e8ca7757731d6f5a10ff7c85738333cba Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Wed, 15 Feb 2023 01:15:59 -0500 Subject: [PATCH] [BugFix] font.c filename was movable stupid bug on my part font_filename was pointing to a movable alloc therefore that data could be moved out from under the returned pointer instead pin the internal calls to core_get_data and remove font_filename in favor of a function that checks if a filename matches the loaded font as this was all it was used for anyway Change-Id: Iebb47ffe3f81aa9d5e3968975f26d64283633ffc --- apps/settings.c | 7 ++----- firmware/export/font.h | 2 +- firmware/font.c | 46 +++++++++++++++++++----------------------- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/apps/settings.c b/apps/settings.c index c19a0b9b65..0f060a41e6 100644 --- a/apps/settings.c +++ b/apps/settings.c @@ -929,11 +929,9 @@ void settings_apply(bool read_disk) if (global_settings.font_file[0] && global_settings.font_file[0] != '-') { int font_ui = screens[SCREEN_MAIN].getuifont(); - const char* loaded_font = font_filename(font_ui); - snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt", global_settings.font_file); - if (!loaded_font || strcmp(loaded_font, buf)) + if (!font_filename_matches_loaded_id(font_ui, buf)) { CHART2(">font_load ", global_settings.font_file); if (font_ui >= 0) @@ -948,10 +946,9 @@ void settings_apply(bool read_disk) if ( global_settings.remote_font_file[0] && global_settings.remote_font_file[0] != '-') { int font_ui = screens[SCREEN_REMOTE].getuifont(); - const char* loaded_font = font_filename(font_ui); snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt", global_settings.remote_font_file); - if (!loaded_font || strcmp(loaded_font, buf)) + if (!font_filename_matches_loaded_id(font_ui, buf)) { CHART2(">font_load_remoteui ", global_settings.remote_font_file); if (font_ui >= 0) diff --git a/firmware/export/font.h b/firmware/export/font.h index 38e30187b3..a0a7c37ba9 100644 --- a/firmware/export/font.h +++ b/firmware/export/font.h @@ -117,7 +117,7 @@ struct font { /* font routines*/ void font_init(void) INIT_ATTR; -const char* font_filename(int font_id); +bool font_filename_matches_loaded_id(int font_id, char *filename); int font_load(const char *path); int font_load_ex(const char *path, size_t buffer_size, int glyphs); void font_unload(int font_id); diff --git a/firmware/font.c b/firmware/font.c index 69c4fd40f8..8c268bea5f 100644 --- a/firmware/font.c +++ b/firmware/font.c @@ -152,13 +152,6 @@ void font_lock(int font_id, bool lock) static struct buflib_callbacks buflibops = {buflibmove_callback, NULL, NULL }; -static inline struct font *pf_from_handle(int handle) -{ - struct buflib_alloc_data *alloc = core_get_data(handle); - struct font *pf = &alloc->font; - return pf; -} - static inline unsigned char *buffer_from_handle(int handle) { struct buflib_alloc_data *alloc = core_get_data(handle); @@ -358,7 +351,7 @@ static int find_font_index(const char* path) return FONT_SYSFIXED; } -const char* font_filename(int font_id) +bool font_filename_matches_loaded_id(int font_id, char *filename) { if ( font_id < 0 || font_id >= MAXFONTS ) return NULL; @@ -367,10 +360,10 @@ const char* font_filename(int font_id) { struct buflib_alloc_data *data = core_get_data(handle); logf("%s id: [%d], %s", __func__, font_id, data->path); - return data->path; + return strcmp(data->path, filename) == 0; } - return NULL; + return false; } static size_t font_glyphs_to_bufsize(struct font *pf, int glyphs) @@ -688,15 +681,15 @@ static void font_enable(int font_id) int handle = buflib_allocations[font_id]; if ( handle < 0 ) return; - struct buflib_alloc_data *pdata = core_get_data(handle); + struct buflib_alloc_data *pdata = core_get_data_pinned(handle); struct font *pf = &pdata->font; if (pf->disabled && pf->fd < 0) { - const char *filename = font_filename(font_id); - pf->fd = open(filename, O_RDONLY); + pf->fd = open(pdata->path, O_RDONLY); pf->disabled = false; } + core_put_data_pinned(pdata); } void font_enable_all(void) @@ -932,25 +925,28 @@ static void glyph_cache_save(int font_id) if ( handle < 0 ) return; - struct font *pf = pf_from_handle(handle); + struct buflib_alloc_data *pdata = core_get_data_pinned(handle); + struct font *pf = &pdata->font; + if(pf && pf->fd >= 0) { char filename[MAX_PATH]; - font_path_to_glyph_path(font_filename(font_id), filename); + font_path_to_glyph_path(pdata->path, filename); fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666); - if (fd < 0) - return; - - cache_pf = pf; - cache_fd = fd; - lru_traverse(&cache_pf->cache._lru, glyph_file_write); - glyph_file_write(NULL); - if (cache_fd >= 0) + if (fd >= 0) { - close(cache_fd); - cache_fd = -1; + cache_pf = pf; + cache_fd = fd; + lru_traverse(&cache_pf->cache._lru, glyph_file_write); + glyph_file_write(NULL); + if (cache_fd >= 0) + { + close(cache_fd); + cache_fd = -1; + } } } + core_put_data_pinned(pdata); return; }