[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
This commit is contained in:
William Wilgus 2023-02-15 01:15:59 -05:00
parent 3bb75e0039
commit 91c8633e8c
3 changed files with 24 additions and 31 deletions

View file

@ -929,11 +929,9 @@ void settings_apply(bool read_disk)
if (global_settings.font_file[0] if (global_settings.font_file[0]
&& global_settings.font_file[0] != '-') { && global_settings.font_file[0] != '-') {
int font_ui = screens[SCREEN_MAIN].getuifont(); int font_ui = screens[SCREEN_MAIN].getuifont();
const char* loaded_font = font_filename(font_ui);
snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt", snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
global_settings.font_file); 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); CHART2(">font_load ", global_settings.font_file);
if (font_ui >= 0) if (font_ui >= 0)
@ -948,10 +946,9 @@ void settings_apply(bool read_disk)
if ( global_settings.remote_font_file[0] if ( global_settings.remote_font_file[0]
&& global_settings.remote_font_file[0] != '-') { && global_settings.remote_font_file[0] != '-') {
int font_ui = screens[SCREEN_REMOTE].getuifont(); int font_ui = screens[SCREEN_REMOTE].getuifont();
const char* loaded_font = font_filename(font_ui);
snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt", snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
global_settings.remote_font_file); 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); CHART2(">font_load_remoteui ", global_settings.remote_font_file);
if (font_ui >= 0) if (font_ui >= 0)

View file

@ -117,7 +117,7 @@ struct font {
/* font routines*/ /* font routines*/
void font_init(void) INIT_ATTR; 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(const char *path);
int font_load_ex(const char *path, size_t buffer_size, int glyphs); int font_load_ex(const char *path, size_t buffer_size, int glyphs);
void font_unload(int font_id); void font_unload(int font_id);

View file

@ -152,13 +152,6 @@ void font_lock(int font_id, bool lock)
static struct buflib_callbacks buflibops = {buflibmove_callback, NULL, NULL }; 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) static inline unsigned char *buffer_from_handle(int handle)
{ {
struct buflib_alloc_data *alloc = core_get_data(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; 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 ) if ( font_id < 0 || font_id >= MAXFONTS )
return NULL; return NULL;
@ -367,10 +360,10 @@ const char* font_filename(int font_id)
{ {
struct buflib_alloc_data *data = core_get_data(handle); struct buflib_alloc_data *data = core_get_data(handle);
logf("%s id: [%d], %s", __func__, font_id, data->path); 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) 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]; int handle = buflib_allocations[font_id];
if ( handle < 0 ) if ( handle < 0 )
return; 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; struct font *pf = &pdata->font;
if (pf->disabled && pf->fd < 0) if (pf->disabled && pf->fd < 0)
{ {
const char *filename = font_filename(font_id); pf->fd = open(pdata->path, O_RDONLY);
pf->fd = open(filename, O_RDONLY);
pf->disabled = false; pf->disabled = false;
} }
core_put_data_pinned(pdata);
} }
void font_enable_all(void) void font_enable_all(void)
@ -932,25 +925,28 @@ static void glyph_cache_save(int font_id)
if ( handle < 0 ) if ( handle < 0 )
return; 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) if(pf && pf->fd >= 0)
{ {
char filename[MAX_PATH]; 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); fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
if (fd < 0) 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)
{ {
close(cache_fd); cache_pf = pf;
cache_fd = -1; 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; return;
} }