summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Wilgus <wilgus.william@gmail.com>2022-10-15 09:08:09 -0400
committerWilliam Wilgus <me.theuser@yahoo.com>2022-10-15 09:26:58 -0400
commit12ef045fdf5a9e2b4d5618cc3b7ba50ecc0ccf69 (patch)
treeb4885951a5cba1b78c21b30ce37a1d6f1574aa5f
parent9d3d925295112a0080bc1d70fad170db9e1af2a9 (diff)
downloadrockbox-12ef045fdf5a9e2b4d5618cc3b7ba50ecc0ccf69.tar.gz
rockbox-12ef045fdf5a9e2b4d5618cc3b7ba50ecc0ccf69.zip
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
-rw-r--r--apps/action.c3
-rw-r--r--apps/debug_menu.c3
-rw-r--r--apps/gui/skin_engine/skin_backdrops.c3
-rw-r--r--apps/gui/skin_engine/skin_parser.c4
-rw-r--r--apps/playback.c3
-rw-r--r--apps/plugin.c6
-rw-r--r--apps/rbcodec_helpers.c5
-rw-r--r--apps/recorder/pcm_record.c3
-rw-r--r--apps/talk.c12
-rw-r--r--firmware/buflib.c2
-rw-r--r--firmware/common/dircache.c8
-rw-r--r--firmware/common/zip.c20
-rw-r--r--firmware/font.c3
-rw-r--r--firmware/linuxboot.c3
-rw-r--r--firmware/target/mips/ingenic_x1000/installer-x1000.c3
-rw-r--r--firmware/usbstack/usb_storage.c3
-rw-r--r--lib/x1000-installer/src/xf_nandio.c5
-rw-r--r--lib/x1000-installer/src/xf_package.c5
18 files changed, 32 insertions, 62 deletions
diff --git a/apps/action.c b/apps/action.c
index da08e29dbb..8c03ca4d65 100644
--- a/apps/action.c
+++ b/apps/action.c
@@ -1219,8 +1219,7 @@ int action_set_keymap_handle(int handle, int count)
1219 return -1; 1219 return -1;
1220#else 1220#else
1221 /* free an existing remap */ 1221 /* free an existing remap */
1222 if (action_last.key_remap > 0) 1222 action_last.key_remap = core_free(action_last.key_remap);
1223 action_last.key_remap = core_free(action_last.key_remap);
1224 1223
1225 /* if clearing the remap, we're done */ 1224 /* if clearing the remap, we're done */
1226 if (count <= 0 || handle <= 0) 1225 if (count <= 0 || handle <= 0)
diff --git a/apps/debug_menu.c b/apps/debug_menu.c
index b034f25df0..d17668ade5 100644
--- a/apps/debug_menu.c
+++ b/apps/debug_menu.c
@@ -520,8 +520,7 @@ static int bf_action_cb(int action, struct gui_synclist* list)
520 /* for some reason simplelist doesn't allow adding items here if 520 /* for some reason simplelist doesn't allow adding items here if
521 * info.get_name is given, so use normal list api */ 521 * info.get_name is given, so use normal list api */
522 gui_synclist_set_nb_items(list, core_get_num_blocks()); 522 gui_synclist_set_nb_items(list, core_get_num_blocks());
523 if (handle > 0) 523 core_free(handle);
524 core_free(handle);
525 } 524 }
526 action = ACTION_REDRAW; 525 action = ACTION_REDRAW;
527 } 526 }
diff --git a/apps/gui/skin_engine/skin_backdrops.c b/apps/gui/skin_engine/skin_backdrops.c
index 146dccb18a..1def0812da 100644
--- a/apps/gui/skin_engine/skin_backdrops.c
+++ b/apps/gui/skin_engine/skin_backdrops.c
@@ -267,8 +267,7 @@ void skin_backdrop_unload(int backdrop_id)
267 backdrops[backdrop_id].ref_count--; 267 backdrops[backdrop_id].ref_count--;
268 if (backdrops[backdrop_id].ref_count <= 0) 268 if (backdrops[backdrop_id].ref_count <= 0)
269 { 269 {
270 if (backdrops[backdrop_id].buflib_handle > 0) 270 core_free(backdrops[backdrop_id].buflib_handle);
271 core_free(backdrops[backdrop_id].buflib_handle);
272 backdrops[backdrop_id].buffer = NULL; 271 backdrops[backdrop_id].buffer = NULL;
273 backdrops[backdrop_id].buflib_handle = -1; 272 backdrops[backdrop_id].buflib_handle = -1;
274 backdrops[backdrop_id].loaded = false; 273 backdrops[backdrop_id].loaded = false;
diff --git a/apps/gui/skin_engine/skin_parser.c b/apps/gui/skin_engine/skin_parser.c
index 6021f0647c..26a251bb4b 100644
--- a/apps/gui/skin_engine/skin_parser.c
+++ b/apps/gui/skin_engine/skin_parser.c
@@ -1829,9 +1829,7 @@ void skin_data_free_buflib_allocs(struct wps_data *wps_data)
1829abort: 1829abort:
1830 wps_data->font_ids = PTRTOSKINOFFSET(skin_buffer, NULL); /* Safe if skin_buffer is NULL */ 1830 wps_data->font_ids = PTRTOSKINOFFSET(skin_buffer, NULL); /* Safe if skin_buffer is NULL */
1831 wps_data->images = PTRTOSKINOFFSET(skin_buffer, NULL); 1831 wps_data->images = PTRTOSKINOFFSET(skin_buffer, NULL);
1832 if (wps_data->buflib_handle > 0) 1832 wps_data->buflib_handle = core_free(wps_data->buflib_handle);
1833 core_free(wps_data->buflib_handle);
1834 wps_data->buflib_handle = -1;
1835#endif 1833#endif
1836} 1834}
1837 1835
diff --git a/apps/playback.c b/apps/playback.c
index 7fdb302d71..9b67f2ccc7 100644
--- a/apps/playback.c
+++ b/apps/playback.c
@@ -3683,8 +3683,7 @@ void audio_hard_stop(void)
3683#ifdef PLAYBACK_VOICE 3683#ifdef PLAYBACK_VOICE
3684 voice_stop(); 3684 voice_stop();
3685#endif 3685#endif
3686 if (audiobuf_handle > 0) 3686 audiobuf_handle = core_free(audiobuf_handle);
3687 audiobuf_handle = core_free(audiobuf_handle);
3688} 3687}
3689 3688
3690/* Resume playback if paused */ 3689/* Resume playback if paused */
diff --git a/apps/plugin.c b/apps/plugin.c
index 888a9e109c..4016040c5a 100644
--- a/apps/plugin.c
+++ b/apps/plugin.c
@@ -839,8 +839,7 @@ int plugin_load(const char* plugin, const void* parameter)
839 } 839 }
840 lc_close(current_plugin_handle); 840 lc_close(current_plugin_handle);
841 current_plugin_handle = pfn_tsr_exit = NULL; 841 current_plugin_handle = pfn_tsr_exit = NULL;
842 if (plugin_buffer_handle > 0) 842 plugin_buffer_handle = core_free(plugin_buffer_handle);
843 plugin_buffer_handle = core_free(plugin_buffer_handle);
844 } 843 }
845 844
846 splash(0, ID2P(LANG_WAIT)); 845 splash(0, ID2P(LANG_WAIT));
@@ -921,8 +920,7 @@ int plugin_load(const char* plugin, const void* parameter)
921 { /* close handle if plugin is no tsr one */ 920 { /* close handle if plugin is no tsr one */
922 lc_close(current_plugin_handle); 921 lc_close(current_plugin_handle);
923 current_plugin_handle = NULL; 922 current_plugin_handle = NULL;
924 if (plugin_buffer_handle > 0) 923 plugin_buffer_handle = core_free(plugin_buffer_handle);
925 plugin_buffer_handle = core_free(plugin_buffer_handle);
926 } 924 }
927 925
928 talk_buffer_set_policy(TALK_BUFFER_DEFAULT); 926 talk_buffer_set_policy(TALK_BUFFER_DEFAULT);
diff --git a/apps/rbcodec_helpers.c b/apps/rbcodec_helpers.c
index 78e068ded7..b856d7355d 100644
--- a/apps/rbcodec_helpers.c
+++ b/apps/rbcodec_helpers.c
@@ -87,10 +87,7 @@ void tdspeed_free_buffers(int32_t **buffers, int nbuf)
87{ 87{
88 for (int i = 0; i < nbuf; i++) 88 for (int i = 0; i < nbuf; i++)
89 { 89 {
90 if (handles[i] > 0) 90 handles[i] = core_free(handles[i]);
91 core_free(handles[i]);
92
93 handles[i] = 0;
94 buffers[i] = NULL; 91 buffers[i] = NULL;
95 } 92 }
96} 93}
diff --git a/apps/recorder/pcm_record.c b/apps/recorder/pcm_record.c
index 0f6e8469ec..d3d45d3e1c 100644
--- a/apps/recorder/pcm_record.c
+++ b/apps/recorder/pcm_record.c
@@ -1437,8 +1437,7 @@ static void on_close_recording(void)
1437 audio_set_output_source(AUDIO_SRC_PLAYBACK); 1437 audio_set_output_source(AUDIO_SRC_PLAYBACK);
1438 pcm_apply_settings(); 1438 pcm_apply_settings();
1439 1439
1440 if (pcmrec_handle > 0) 1440 pcmrec_handle = core_free(pcmrec_handle);
1441 pcmrec_handle = core_free(pcmrec_handle);
1442 talk_buffer_set_policy(TALK_BUFFER_DEFAULT); 1441 talk_buffer_set_policy(TALK_BUFFER_DEFAULT);
1443 1442
1444 send_event(RECORDING_EVENT_STOP, NULL); 1443 send_event(RECORDING_EVENT_STOP, NULL);
diff --git a/apps/talk.c b/apps/talk.c
index 1be2a041c2..a7af58c273 100644
--- a/apps/talk.c
+++ b/apps/talk.c
@@ -226,7 +226,7 @@ static int shrink_callback(int handle, unsigned hints, void *start, size_t old_s
226 226
227 mutex_lock(&read_buffer_mutex); 227 mutex_lock(&read_buffer_mutex);
228 /* the clip buffer isn't usable without index table */ 228 /* the clip buffer isn't usable without index table */
229 if (handle == index_handle && talk_handle > 0) 229 if (handle == index_handle)
230 talk_handle = core_free(talk_handle); 230 talk_handle = core_free(talk_handle);
231 if (h) 231 if (h)
232 *h = core_free(handle); 232 *h = core_free(handle);
@@ -546,8 +546,7 @@ static bool create_clip_buffer(size_t max_size)
546 546
547alloc_err: 547alloc_err:
548 talk_status = TALK_STATUS_ERR_ALLOC; 548 talk_status = TALK_STATUS_ERR_ALLOC;
549 if (index_handle > 0) 549 index_handle = core_free(index_handle);
550 index_handle = core_free(index_handle);
551 return false; 550 return false;
552} 551}
553static inline int load_voicefile_failure(int fd) 552static inline int load_voicefile_failure(int fd)
@@ -621,8 +620,7 @@ static bool load_voicefile_data(int fd)
621 620
622 /* just allocate, populate on an as-needed basis later 621 /* just allocate, populate on an as-needed basis later
623 * re-create the clip buffer to ensure clip_ctx is up-to-date */ 622 * re-create the clip buffer to ensure clip_ctx is up-to-date */
624 if (talk_handle > 0) 623 talk_handle = core_free(talk_handle);
625 talk_handle = core_free(talk_handle);
626 if (!create_clip_buffer(voicebuf_size)) 624 if (!create_clip_buffer(voicebuf_size))
627 return false; 625 return false;
628 626
@@ -823,8 +821,8 @@ void talk_init(void)
823 voicefile_size = has_voicefile = 0; 821 voicefile_size = has_voicefile = 0;
824 /* need to free these as their size depends on the voice file, and 822 /* need to free these as their size depends on the voice file, and
825 * this function is called when the talk voice file changes */ 823 * this function is called when the talk voice file changes */
826 if (index_handle > 0) index_handle = core_free(index_handle); 824 index_handle = core_free(index_handle);
827 if (talk_handle > 0) talk_handle = core_free(talk_handle); 825 talk_handle = core_free(talk_handle);
828 /* don't free thumb handle, it doesn't depend on the actual voice file 826 /* don't free thumb handle, it doesn't depend on the actual voice file
829 * and so we can re-use it if it's already allocated in any event */ 827 * and so we can re-use it if it's already allocated in any event */
830 828
diff --git a/firmware/buflib.c b/firmware/buflib.c
index 7263f1b95d..3130bc960c 100644
--- a/firmware/buflib.c
+++ b/firmware/buflib.c
@@ -820,6 +820,8 @@ find_block_before(struct buflib_context *ctx, union buflib_data* block,
820int 820int
821buflib_free(struct buflib_context *ctx, int handle_num) 821buflib_free(struct buflib_context *ctx, int handle_num)
822{ 822{
823 if (handle_num <= 0) /* invalid or already free */
824 return handle_num;
823 union buflib_data *handle = ctx->handle_table - handle_num, 825 union buflib_data *handle = ctx->handle_table - handle_num,
824 *freed_block = handle_to_block(ctx, handle_num), 826 *freed_block = handle_to_block(ctx, handle_num),
825 *block, *next_block; 827 *block, *next_block;
diff --git a/firmware/common/dircache.c b/firmware/common/dircache.c
index 7a84b761a0..8917b3348e 100644
--- a/firmware/common/dircache.c
+++ b/firmware/common/dircache.c
@@ -1963,8 +1963,7 @@ static int prepare_build(bool *realloced)
1963 int handle = reset_buffer(); 1963 int handle = reset_buffer();
1964 dircache_unlock(); 1964 dircache_unlock();
1965 1965
1966 if (handle > 0) 1966 core_free(handle);
1967 core_free(handle);
1968 1967
1969 handle = alloc_cache(size); 1968 handle = alloc_cache(size);
1970 1969
@@ -2164,8 +2163,7 @@ static void dircache_suspend_internal(bool freeit)
2164 2163
2165 dircache_unlock(); 2164 dircache_unlock();
2166 2165
2167 if (handle > 0) 2166 core_free(handle);
2168 core_free(handle);
2169 2167
2170 thread_wait(thread_id); 2168 thread_wait(thread_id);
2171 2169
@@ -3179,7 +3177,7 @@ error:
3179 dircache_unlock(); 3177 dircache_unlock();
3180 3178
3181error_nolock: 3179error_nolock:
3182 if (rc < 0 && handle > 0) 3180 if (rc < 0)
3183 core_free(handle); 3181 core_free(handle);
3184 3182
3185 if (fd >= 0) 3183 if (fd >= 0)
diff --git a/firmware/common/zip.c b/firmware/common/zip.c
index 36b90a9223..22c6226e3b 100644
--- a/firmware/common/zip.c
+++ b/firmware/common/zip.c
@@ -237,8 +237,7 @@ static int zip_read_ed(struct zip* z) {
237 rv = 0; 237 rv = 0;
238 238
239bail: 239bail:
240 if (mem_handle >= 0) 240 core_free(mem_handle);
241 core_free(mem_handle);
242 return rv; 241 return rv;
243} 242}
244 243
@@ -337,10 +336,9 @@ static int zip_read_cd(struct zip* z, bool use_cb) {
337 rv = 0; 336 rv = 0;
338 337
339bail: 338bail:
340 if (rv != 0 && cds_handle >= 0) 339 if (rv != 0)
341 core_free(cds_handle); 340 core_free(cds_handle);
342 if (mem_handle >= 0) 341 core_free(mem_handle);
343 core_free(mem_handle);
344 return rv; 342 return rv;
345} 343}
346 344
@@ -497,8 +495,7 @@ static int zip_read_entries(struct zip* z) {
497 rv = 0; 495 rv = 0;
498 496
499bail: 497bail:
500 if (mem_handle >= 0) 498 core_free(mem_handle);
501 core_free(mem_handle);
502 return rv; 499 return rv;
503} 500}
504 501
@@ -754,10 +751,8 @@ struct zip* zip_open(const char* name, bool try_mem) {
754bail: 751bail:
755 if (file >= 0) 752 if (file >= 0)
756 close(file); 753 close(file);
757 if (mem_handle >= 0) 754 core_free(mem_handle);
758 core_free(mem_handle); 755 core_free(zip_handle);
759 if (zip_handle >= 0)
760 core_free(zip_handle);
761 return NULL; 756 return NULL;
762} 757}
763 758
@@ -875,8 +870,7 @@ void zip_close(struct zip* z) {
875 870
876 z->close(z); 871 z->close(z);
877 872
878 if (z->cds_handle >= 0) 873 core_free(z->cds_handle);
879 core_free(z->cds_handle);
880 874
881 core_free(z->zip_handle); 875 core_free(z->zip_handle);
882} 876}
diff --git a/firmware/font.c b/firmware/font.c
index b8fa1c537f..724cb84d57 100644
--- a/firmware/font.c
+++ b/firmware/font.c
@@ -604,8 +604,7 @@ void font_unload(int font_id)
604 glyph_cache_save(font_id); 604 glyph_cache_save(font_id);
605 close(pf->fd); 605 close(pf->fd);
606 } 606 }
607 if (handle > 0) 607 core_free(handle);
608 core_free(handle);
609 buflib_allocations[font_id] = -1; 608 buflib_allocations[font_id] = -1;
610 609
611 } 610 }
diff --git a/firmware/linuxboot.c b/firmware/linuxboot.c
index f9732f6ace..419044b5a9 100644
--- a/firmware/linuxboot.c
+++ b/firmware/linuxboot.c
@@ -221,8 +221,7 @@ int uimage_load(struct uimage_header* uh, size_t* out_size,
221 ret = 0; 221 ret = 0;
222 222
223 err: 223 err:
224 if(state_h > 0) 224 core_free(state_h);
225 core_free(state_h);
226 if(out_h > 0) { 225 if(out_h > 0) {
227 if(ret == 0) 226 if(ret == 0)
228 ret = out_h; 227 ret = out_h;
diff --git a/firmware/target/mips/ingenic_x1000/installer-x1000.c b/firmware/target/mips/ingenic_x1000/installer-x1000.c
index 48850f8a62..ef5bbcd66a 100644
--- a/firmware/target/mips/ingenic_x1000/installer-x1000.c
+++ b/firmware/target/mips/ingenic_x1000/installer-x1000.c
@@ -178,8 +178,7 @@ static void updater_cleanup(struct updater* u)
178 if(u->tar && mtar_is_open(u->tar)) 178 if(u->tar && mtar_is_open(u->tar))
179 mtar_close(u->tar); 179 mtar_close(u->tar);
180 180
181 if(u->buf_hnd >= 0) 181 core_free(u->buf_hnd);
182 core_free(u->buf_hnd);
183 182
184 if(u->ndrv) { 183 if(u->ndrv) {
185 nand_close(u->ndrv); 184 nand_close(u->ndrv);
diff --git a/firmware/usbstack/usb_storage.c b/firmware/usbstack/usb_storage.c
index eb82f72eae..08b1f0b7e7 100644
--- a/firmware/usbstack/usb_storage.c
+++ b/firmware/usbstack/usb_storage.c
@@ -481,8 +481,7 @@ void usb_storage_init_connection(void)
481 481
482void usb_storage_disconnect(void) 482void usb_storage_disconnect(void)
483{ 483{
484 if (usb_handle > 0) 484 usb_handle = core_free(usb_handle);
485 usb_handle = core_free(usb_handle);
486} 485}
487 486
488/* called by usb_core_transfer_complete() */ 487/* called by usb_core_transfer_complete() */
diff --git a/lib/x1000-installer/src/xf_nandio.c b/lib/x1000-installer/src/xf_nandio.c
index 29ff9d9120..6dc87bc420 100644
--- a/lib/x1000-installer/src/xf_nandio.c
+++ b/lib/x1000-installer/src/xf_nandio.c
@@ -75,10 +75,7 @@ int xf_nandio_init(struct xf_nandio* nio)
75 75
76void xf_nandio_destroy(struct xf_nandio* nio) 76void xf_nandio_destroy(struct xf_nandio* nio)
77{ 77{
78 if(nio->alloc_handle > 0) { 78 nio->alloc_handle = core_free(nio->alloc_handle);
79 core_free(nio->alloc_handle);
80 nio->alloc_handle = 0;
81 }
82 79
83 if(nio->ndrv) { 80 if(nio->ndrv) {
84 nand_lock(nio->ndrv); 81 nand_lock(nio->ndrv);
diff --git a/lib/x1000-installer/src/xf_package.c b/lib/x1000-installer/src/xf_package.c
index 04b32cdcb0..fb107aef72 100644
--- a/lib/x1000-installer/src/xf_package.c
+++ b/lib/x1000-installer/src/xf_package.c
@@ -257,8 +257,5 @@ void xf_package_close(struct xf_package* pkg)
257 if(mtar_is_open(pkg->tar)) 257 if(mtar_is_open(pkg->tar))
258 mtar_close(pkg->tar); 258 mtar_close(pkg->tar);
259 259
260 if(pkg->alloc_handle > 0) { 260 pkg->alloc_handle = core_free(pkg->alloc_handle);
261 core_free(pkg->alloc_handle);
262 pkg->alloc_handle = 0;
263 }
264} 261}