From 879888b158376f1ea2c92dd49e0c7617d07fd5b2 Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Sat, 15 Oct 2022 23:30:38 +0100 Subject: Avoid using buflib names for storing font paths Naming your allocations is more of a debugging feature; it's a bad idea to abuse this feature for storing some random string. The font code does exactly that to remember the path used to load a font, and it's the only code that uses buflib names in this way. Store the path inside the font allocation instead as a regular char array. For simplicity it's MAX_PATH sized, so this'll use a bit more memory than the buflib name method, maybe 1-2 KiB if a few fonts are loaded. Change-Id: Ie286a1a273d6477af9e5d3f76e0534b62f72e8f7 --- firmware/font.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/firmware/font.c b/firmware/font.c index d7473346da..8f0808ba87 100644 --- a/firmware/font.c +++ b/firmware/font.c @@ -90,6 +90,7 @@ extern struct font sysfont; struct buflib_alloc_data { struct font font; /* must be the first member! */ + char path[MAX_PATH]; /* font path and filename */ int refcount; /* how many times has this font been loaded? */ unsigned char buffer[]; }; @@ -331,8 +332,13 @@ static int find_font_index(const char* path) while (index < MAXFONTS) { handle = buflib_allocations[index]; - if (handle > 0 && !strcmp(core_get_name(handle), path)) - return index; + if (handle > 0) + { + struct buflib_alloc_data *data = core_get_data(handle); + if(!strcmp(data->path, path)) + return index; + } + index++; } return FONT_SYSFIXED; @@ -344,7 +350,11 @@ const char* font_filename(int font_id) return NULL; int handle = buflib_allocations[font_id]; if (handle > 0) - return core_get_name(handle); + { + struct buflib_alloc_data *data = core_get_data(handle); + return data->path; + } + return NULL; } @@ -402,6 +412,11 @@ static struct font* font_load_header(int fd, struct font *pheader, /* load a font with room for glyphs, limited to bufsize if not zero */ int font_load_ex( const char *path, size_t buf_size, int glyphs ) { + /* needed to handle the font properly after it's loaded */ + size_t path_len = strlen(path); + if ( path_len >= MAX_PATH ) + return -1; + //printf("\nfont_load_ex(%s, %d, %d)\n", path, buf_size, glyphs); int fd = open(path, O_RDONLY|O_BINARY); if ( fd < 0 ) @@ -510,7 +525,7 @@ int font_load_ex( const char *path, size_t buf_size, int glyphs ) font_id = open_slot; /* allocate mem */ - int handle = core_alloc_ex( path, + int handle = core_alloc_ex( NULL, bufsize + sizeof( struct buflib_alloc_data ), &buflibops ); if ( handle <= 0 ) @@ -522,6 +537,9 @@ int font_load_ex( const char *path, size_t buf_size, int glyphs ) pdata = core_get_data(handle); pdata->refcount = 1; + /* save load path so we can recognize this font later */ + memcpy(pdata->path, path, path_len+1); + /* load and init */ struct font *pf = &pdata->font; memcpy(pf, &f, sizeof( struct font) ); @@ -594,7 +612,6 @@ void font_unload(int font_id) pdata->refcount--; if (pdata->refcount < 1) { - //printf("freeing id: %d %s\n", font_id, core_get_name(*handle)); if (pf && pf->fd >= 0) { glyph_cache_save(font_id); -- cgit v1.2.3