move buflib_free invalid handle check to the function

allow buflib_free to check for invalid or already freed handles
within the function -- remove all the invalid handle guards thru core_free

Change-Id: Ibdcbc82760fc93b674c42283fca420d94907df8e
This commit is contained in:
William Wilgus 2022-10-15 09:08:09 -04:00 committed by William Wilgus
parent 9d3d925295
commit 12ef045fdf
18 changed files with 32 additions and 62 deletions

View file

@ -1219,7 +1219,6 @@ int action_set_keymap_handle(int handle, int count)
return -1; return -1;
#else #else
/* free an existing remap */ /* free an existing remap */
if (action_last.key_remap > 0)
action_last.key_remap = core_free(action_last.key_remap); action_last.key_remap = core_free(action_last.key_remap);
/* if clearing the remap, we're done */ /* if clearing the remap, we're done */

View file

@ -520,7 +520,6 @@ static int bf_action_cb(int action, struct gui_synclist* list)
/* for some reason simplelist doesn't allow adding items here if /* for some reason simplelist doesn't allow adding items here if
* info.get_name is given, so use normal list api */ * info.get_name is given, so use normal list api */
gui_synclist_set_nb_items(list, core_get_num_blocks()); gui_synclist_set_nb_items(list, core_get_num_blocks());
if (handle > 0)
core_free(handle); core_free(handle);
} }
action = ACTION_REDRAW; action = ACTION_REDRAW;

View file

@ -267,7 +267,6 @@ void skin_backdrop_unload(int backdrop_id)
backdrops[backdrop_id].ref_count--; backdrops[backdrop_id].ref_count--;
if (backdrops[backdrop_id].ref_count <= 0) if (backdrops[backdrop_id].ref_count <= 0)
{ {
if (backdrops[backdrop_id].buflib_handle > 0)
core_free(backdrops[backdrop_id].buflib_handle); core_free(backdrops[backdrop_id].buflib_handle);
backdrops[backdrop_id].buffer = NULL; backdrops[backdrop_id].buffer = NULL;
backdrops[backdrop_id].buflib_handle = -1; backdrops[backdrop_id].buflib_handle = -1;

View file

@ -1829,9 +1829,7 @@ void skin_data_free_buflib_allocs(struct wps_data *wps_data)
abort: abort:
wps_data->font_ids = PTRTOSKINOFFSET(skin_buffer, NULL); /* Safe if skin_buffer is NULL */ wps_data->font_ids = PTRTOSKINOFFSET(skin_buffer, NULL); /* Safe if skin_buffer is NULL */
wps_data->images = PTRTOSKINOFFSET(skin_buffer, NULL); wps_data->images = PTRTOSKINOFFSET(skin_buffer, NULL);
if (wps_data->buflib_handle > 0) wps_data->buflib_handle = core_free(wps_data->buflib_handle);
core_free(wps_data->buflib_handle);
wps_data->buflib_handle = -1;
#endif #endif
} }

View file

@ -3683,7 +3683,6 @@ void audio_hard_stop(void)
#ifdef PLAYBACK_VOICE #ifdef PLAYBACK_VOICE
voice_stop(); voice_stop();
#endif #endif
if (audiobuf_handle > 0)
audiobuf_handle = core_free(audiobuf_handle); audiobuf_handle = core_free(audiobuf_handle);
} }

View file

@ -839,7 +839,6 @@ int plugin_load(const char* plugin, const void* parameter)
} }
lc_close(current_plugin_handle); lc_close(current_plugin_handle);
current_plugin_handle = pfn_tsr_exit = NULL; current_plugin_handle = pfn_tsr_exit = NULL;
if (plugin_buffer_handle > 0)
plugin_buffer_handle = core_free(plugin_buffer_handle); plugin_buffer_handle = core_free(plugin_buffer_handle);
} }
@ -921,7 +920,6 @@ int plugin_load(const char* plugin, const void* parameter)
{ /* close handle if plugin is no tsr one */ { /* close handle if plugin is no tsr one */
lc_close(current_plugin_handle); lc_close(current_plugin_handle);
current_plugin_handle = NULL; current_plugin_handle = NULL;
if (plugin_buffer_handle > 0)
plugin_buffer_handle = core_free(plugin_buffer_handle); plugin_buffer_handle = core_free(plugin_buffer_handle);
} }

View file

@ -87,10 +87,7 @@ void tdspeed_free_buffers(int32_t **buffers, int nbuf)
{ {
for (int i = 0; i < nbuf; i++) for (int i = 0; i < nbuf; i++)
{ {
if (handles[i] > 0) handles[i] = core_free(handles[i]);
core_free(handles[i]);
handles[i] = 0;
buffers[i] = NULL; buffers[i] = NULL;
} }
} }

View file

@ -1437,7 +1437,6 @@ static void on_close_recording(void)
audio_set_output_source(AUDIO_SRC_PLAYBACK); audio_set_output_source(AUDIO_SRC_PLAYBACK);
pcm_apply_settings(); pcm_apply_settings();
if (pcmrec_handle > 0)
pcmrec_handle = core_free(pcmrec_handle); pcmrec_handle = core_free(pcmrec_handle);
talk_buffer_set_policy(TALK_BUFFER_DEFAULT); talk_buffer_set_policy(TALK_BUFFER_DEFAULT);

View file

@ -226,7 +226,7 @@ static int shrink_callback(int handle, unsigned hints, void *start, size_t old_s
mutex_lock(&read_buffer_mutex); mutex_lock(&read_buffer_mutex);
/* the clip buffer isn't usable without index table */ /* the clip buffer isn't usable without index table */
if (handle == index_handle && talk_handle > 0) if (handle == index_handle)
talk_handle = core_free(talk_handle); talk_handle = core_free(talk_handle);
if (h) if (h)
*h = core_free(handle); *h = core_free(handle);
@ -546,7 +546,6 @@ static bool create_clip_buffer(size_t max_size)
alloc_err: alloc_err:
talk_status = TALK_STATUS_ERR_ALLOC; talk_status = TALK_STATUS_ERR_ALLOC;
if (index_handle > 0)
index_handle = core_free(index_handle); index_handle = core_free(index_handle);
return false; return false;
} }
@ -621,7 +620,6 @@ static bool load_voicefile_data(int fd)
/* just allocate, populate on an as-needed basis later /* just allocate, populate on an as-needed basis later
* re-create the clip buffer to ensure clip_ctx is up-to-date */ * re-create the clip buffer to ensure clip_ctx is up-to-date */
if (talk_handle > 0)
talk_handle = core_free(talk_handle); talk_handle = core_free(talk_handle);
if (!create_clip_buffer(voicebuf_size)) if (!create_clip_buffer(voicebuf_size))
return false; return false;
@ -823,8 +821,8 @@ void talk_init(void)
voicefile_size = has_voicefile = 0; voicefile_size = has_voicefile = 0;
/* need to free these as their size depends on the voice file, and /* need to free these as their size depends on the voice file, and
* this function is called when the talk voice file changes */ * this function is called when the talk voice file changes */
if (index_handle > 0) index_handle = core_free(index_handle); index_handle = core_free(index_handle);
if (talk_handle > 0) talk_handle = core_free(talk_handle); talk_handle = core_free(talk_handle);
/* don't free thumb handle, it doesn't depend on the actual voice file /* don't free thumb handle, it doesn't depend on the actual voice file
* and so we can re-use it if it's already allocated in any event */ * and so we can re-use it if it's already allocated in any event */

View file

@ -820,6 +820,8 @@ find_block_before(struct buflib_context *ctx, union buflib_data* block,
int int
buflib_free(struct buflib_context *ctx, int handle_num) buflib_free(struct buflib_context *ctx, int handle_num)
{ {
if (handle_num <= 0) /* invalid or already free */
return handle_num;
union buflib_data *handle = ctx->handle_table - handle_num, union buflib_data *handle = ctx->handle_table - handle_num,
*freed_block = handle_to_block(ctx, handle_num), *freed_block = handle_to_block(ctx, handle_num),
*block, *next_block; *block, *next_block;

View file

@ -1963,7 +1963,6 @@ static int prepare_build(bool *realloced)
int handle = reset_buffer(); int handle = reset_buffer();
dircache_unlock(); dircache_unlock();
if (handle > 0)
core_free(handle); core_free(handle);
handle = alloc_cache(size); handle = alloc_cache(size);
@ -2164,7 +2163,6 @@ static void dircache_suspend_internal(bool freeit)
dircache_unlock(); dircache_unlock();
if (handle > 0)
core_free(handle); core_free(handle);
thread_wait(thread_id); thread_wait(thread_id);
@ -3179,7 +3177,7 @@ error:
dircache_unlock(); dircache_unlock();
error_nolock: error_nolock:
if (rc < 0 && handle > 0) if (rc < 0)
core_free(handle); core_free(handle);
if (fd >= 0) if (fd >= 0)

View file

@ -237,7 +237,6 @@ static int zip_read_ed(struct zip* z) {
rv = 0; rv = 0;
bail: bail:
if (mem_handle >= 0)
core_free(mem_handle); core_free(mem_handle);
return rv; return rv;
} }
@ -337,9 +336,8 @@ static int zip_read_cd(struct zip* z, bool use_cb) {
rv = 0; rv = 0;
bail: bail:
if (rv != 0 && cds_handle >= 0) if (rv != 0)
core_free(cds_handle); core_free(cds_handle);
if (mem_handle >= 0)
core_free(mem_handle); core_free(mem_handle);
return rv; return rv;
} }
@ -497,7 +495,6 @@ static int zip_read_entries(struct zip* z) {
rv = 0; rv = 0;
bail: bail:
if (mem_handle >= 0)
core_free(mem_handle); core_free(mem_handle);
return rv; return rv;
} }
@ -754,9 +751,7 @@ struct zip* zip_open(const char* name, bool try_mem) {
bail: bail:
if (file >= 0) if (file >= 0)
close(file); close(file);
if (mem_handle >= 0)
core_free(mem_handle); core_free(mem_handle);
if (zip_handle >= 0)
core_free(zip_handle); core_free(zip_handle);
return NULL; return NULL;
} }
@ -875,7 +870,6 @@ void zip_close(struct zip* z) {
z->close(z); z->close(z);
if (z->cds_handle >= 0)
core_free(z->cds_handle); core_free(z->cds_handle);
core_free(z->zip_handle); core_free(z->zip_handle);

View file

@ -604,7 +604,6 @@ void font_unload(int font_id)
glyph_cache_save(font_id); glyph_cache_save(font_id);
close(pf->fd); close(pf->fd);
} }
if (handle > 0)
core_free(handle); core_free(handle);
buflib_allocations[font_id] = -1; buflib_allocations[font_id] = -1;

View file

@ -221,7 +221,6 @@ int uimage_load(struct uimage_header* uh, size_t* out_size,
ret = 0; ret = 0;
err: err:
if(state_h > 0)
core_free(state_h); core_free(state_h);
if(out_h > 0) { if(out_h > 0) {
if(ret == 0) if(ret == 0)

View file

@ -178,7 +178,6 @@ static void updater_cleanup(struct updater* u)
if(u->tar && mtar_is_open(u->tar)) if(u->tar && mtar_is_open(u->tar))
mtar_close(u->tar); mtar_close(u->tar);
if(u->buf_hnd >= 0)
core_free(u->buf_hnd); core_free(u->buf_hnd);
if(u->ndrv) { if(u->ndrv) {

View file

@ -481,7 +481,6 @@ void usb_storage_init_connection(void)
void usb_storage_disconnect(void) void usb_storage_disconnect(void)
{ {
if (usb_handle > 0)
usb_handle = core_free(usb_handle); usb_handle = core_free(usb_handle);
} }

View file

@ -75,10 +75,7 @@ int xf_nandio_init(struct xf_nandio* nio)
void xf_nandio_destroy(struct xf_nandio* nio) void xf_nandio_destroy(struct xf_nandio* nio)
{ {
if(nio->alloc_handle > 0) { nio->alloc_handle = core_free(nio->alloc_handle);
core_free(nio->alloc_handle);
nio->alloc_handle = 0;
}
if(nio->ndrv) { if(nio->ndrv) {
nand_lock(nio->ndrv); nand_lock(nio->ndrv);

View file

@ -257,8 +257,5 @@ void xf_package_close(struct xf_package* pkg)
if(mtar_is_open(pkg->tar)) if(mtar_is_open(pkg->tar))
mtar_close(pkg->tar); mtar_close(pkg->tar);
if(pkg->alloc_handle > 0) { pkg->alloc_handle = core_free(pkg->alloc_handle);
core_free(pkg->alloc_handle);
pkg->alloc_handle = 0;
}
} }