From 70aa970c32c667e12322d3fb838706940d12502c Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Fri, 3 Mar 2023 19:36:28 +0900 Subject: [PATCH] [gamecode] Make modules responsible for freeing resources It should have been this way all along, and it seems I thought they were when I did rua_gui.c as it already freed its resource block, which would have been a double free (oops). Fixes an invalid write when shutting down progs in qwaq-cmd (relevant change not committed). --- libs/console/bi_inputline.c | 1 + libs/gamecode/pr_debug.c | 2 ++ libs/gamecode/pr_resource.c | 1 - libs/gamecode/pr_strings.c | 2 ++ libs/gib/bi_gib.c | 3 ++- libs/ruamoko/rua_cbuf.c | 1 + libs/ruamoko/rua_cmd.c | 1 + libs/ruamoko/rua_cvar.c | 1 + libs/ruamoko/rua_hash.c | 1 + libs/ruamoko/rua_input.c | 2 ++ libs/ruamoko/rua_mersenne.c | 1 + libs/ruamoko/rua_model.c | 1 + libs/ruamoko/rua_msgbuf.c | 1 + libs/ruamoko/rua_obj.c | 2 ++ libs/ruamoko/rua_plist.c | 1 + libs/ruamoko/rua_qfile.c | 1 + libs/ruamoko/rua_scene.c | 1 + libs/ruamoko/rua_script.c | 1 + libs/ruamoko/rua_set.c | 1 + libs/video/renderer/r_progs.c | 2 ++ ruamoko/qwaq/builtins/curses.c | 1 + ruamoko/qwaq/builtins/debug.c | 2 ++ ruamoko/qwaq/builtins/editbuffer.c | 1 + ruamoko/qwaq/builtins/main.c | 1 + ruamoko/qwaq/builtins/term-input.c | 1 + 25 files changed, 31 insertions(+), 2 deletions(-) diff --git a/libs/console/bi_inputline.c b/libs/console/bi_inputline.c index 9111945ea..4b6764e3a 100644 --- a/libs/console/bi_inputline.c +++ b/libs/console/bi_inputline.c @@ -105,6 +105,7 @@ bi_il_clear (progs_t *pr, void *_res) static void bi_il_destroy (progs_t *pr, void *_res) { + free (_res); } static il_data_t * __attribute__((pure)) diff --git a/libs/gamecode/pr_debug.c b/libs/gamecode/pr_debug.c index 7f62831fc..683fa4bf7 100644 --- a/libs/gamecode/pr_debug.c +++ b/libs/gamecode/pr_debug.c @@ -407,6 +407,8 @@ pr_debug_destroy (progs_t *pr, void *_res) Hash_DelTable (res->compunits); pr->pr_debug_resources = 0; + + free (res); } static file_t * diff --git a/libs/gamecode/pr_resource.c b/libs/gamecode/pr_resource.c index 0b0c52ec7..6b51a4f64 100644 --- a/libs/gamecode/pr_resource.c +++ b/libs/gamecode/pr_resource.c @@ -79,7 +79,6 @@ PR_Resources_Shutdown (progs_t *pr) while (res) { pr_resource_t *t = res->next; res->destroy (pr, res->data); - free (res->data); free (res); res = t; } diff --git a/libs/gamecode/pr_strings.c b/libs/gamecode/pr_strings.c index d4f423c4e..5bd8ad9c2 100644 --- a/libs/gamecode/pr_strings.c +++ b/libs/gamecode/pr_strings.c @@ -241,6 +241,8 @@ pr_strings_destroy (progs_t *pr, void *_res) dstring_delete (res->print_str); Hash_DelTable (res->strref_hash); pr->pr_string_resources = 0; + + free (res); } VISIBLE int diff --git a/libs/gib/bi_gib.c b/libs/gib/bi_gib.c index 0a55e8a2d..049507e16 100644 --- a/libs/gib/bi_gib.c +++ b/libs/gib/bi_gib.c @@ -116,8 +116,9 @@ bi_gib_builtin_clear (progs_t *progs, void *_res) static void bi_gib_builtin_destroy (progs_t *progs, void *_res) { - //bi_gib_resources_t *res = (bi_gib_resources_t *) _res; + bi_gib_resources_t *res = (bi_gib_resources_t *) _res; Hash_DelTable (bi_gib_builtins); + free (res); } static void diff --git a/libs/ruamoko/rua_cbuf.c b/libs/ruamoko/rua_cbuf.c index c727ae891..5380810b2 100644 --- a/libs/ruamoko/rua_cbuf.c +++ b/libs/ruamoko/rua_cbuf.c @@ -96,6 +96,7 @@ bi_cbuf_clear (progs_t *pr, void *data) static void bi_cbuf_destroy (progs_t *pr, void *data) { + free (data); } #define bi(x,np,params...) {#x, bi_##x, -1, np, {params}} diff --git a/libs/ruamoko/rua_cmd.c b/libs/ruamoko/rua_cmd.c index 50e4ba2eb..96025fc65 100644 --- a/libs/ruamoko/rua_cmd.c +++ b/libs/ruamoko/rua_cmd.c @@ -130,6 +130,7 @@ bi_cmd_destroy (progs_t *pr, void *data) if (!--bi_cmds_refs) { Hash_DelTable (bi_cmds); } + free (data); } static void diff --git a/libs/ruamoko/rua_cvar.c b/libs/ruamoko/rua_cvar.c index 716e16172..79ebffda2 100644 --- a/libs/ruamoko/rua_cvar.c +++ b/libs/ruamoko/rua_cvar.c @@ -79,6 +79,7 @@ bi_cvar_clear (progs_t *pr, void *_res) static void bi_cvar_destroy (progs_t *pr, void *_res) { + free (_res); } static void diff --git a/libs/ruamoko/rua_hash.c b/libs/ruamoko/rua_hash.c index 8cb1e55a8..cd8e2b235 100644 --- a/libs/ruamoko/rua_hash.c +++ b/libs/ruamoko/rua_hash.c @@ -385,6 +385,7 @@ bi_hash_clear (progs_t *pr, void *_res) static void bi_hash_destroy (progs_t *pr, void *_res) { + free (_res); } #define bi(x,np,params...) {#x, bi_##x, -1, np, {params}} diff --git a/libs/ruamoko/rua_input.c b/libs/ruamoko/rua_input.c index c7e8e97bc..1c6153b55 100644 --- a/libs/ruamoko/rua_input.c +++ b/libs/ruamoko/rua_input.c @@ -486,6 +486,8 @@ bi_input_destroy (progs_t *pr, void *_res) input_resources_t *res = _res; Hash_DelTable (res->cookies); delete_memsuper (res->cookie_super); + + free (res); } static uintptr_t diff --git a/libs/ruamoko/rua_mersenne.c b/libs/ruamoko/rua_mersenne.c index 632aa8ccc..15ce3e41c 100644 --- a/libs/ruamoko/rua_mersenne.c +++ b/libs/ruamoko/rua_mersenne.c @@ -150,6 +150,7 @@ bi_mtwist_clear (progs_t *pr, void *_res) static void bi_mtwist_destroy (progs_t *pr, void *_res) { + free (_res); } #define bi(x,np,params...) {#x, bi_##x, -1, np, {params}} diff --git a/libs/ruamoko/rua_model.c b/libs/ruamoko/rua_model.c index 4ea5b564a..a19fa8f92 100644 --- a/libs/ruamoko/rua_model.c +++ b/libs/ruamoko/rua_model.c @@ -106,6 +106,7 @@ bi_rua_model_clear (progs_t *pr, void *_res) static void bi_rua_model_destroy (progs_t *pr, void *_res) { + free (_res); } static int diff --git a/libs/ruamoko/rua_msgbuf.c b/libs/ruamoko/rua_msgbuf.c index cf1db72e9..b8300d382 100644 --- a/libs/ruamoko/rua_msgbuf.c +++ b/libs/ruamoko/rua_msgbuf.c @@ -97,6 +97,7 @@ bi_msgbuf_clear (progs_t *pr, void *data) static void bi_msgbuf_destroy (progs_t *pr, void *_res) { + free (_res); } static int diff --git a/libs/ruamoko/rua_obj.c b/libs/ruamoko/rua_obj.c index 1d0872798..15b4d114f 100644 --- a/libs/ruamoko/rua_obj.c +++ b/libs/ruamoko/rua_obj.c @@ -2332,6 +2332,8 @@ rua_obj_destroy (progs_t *pr, void *_res) Hash_DelTable (probj->classes); Hash_DelTable (probj->protocols); Hash_DelTable (probj->load_methods); + + free (probj); } void diff --git a/libs/ruamoko/rua_plist.c b/libs/ruamoko/rua_plist.c index f146dbb92..e3d5cb9eb 100644 --- a/libs/ruamoko/rua_plist.c +++ b/libs/ruamoko/rua_plist.c @@ -111,6 +111,7 @@ bi_plist_destroy (progs_t *pr, void *_res) { __auto_type res = (plist_resources_t *) _res; Hash_DelTable (res->plist_tab); + free (res); } static inline int diff --git a/libs/ruamoko/rua_qfile.c b/libs/ruamoko/rua_qfile.c index 98636cb36..c702cbb5a 100644 --- a/libs/ruamoko/rua_qfile.c +++ b/libs/ruamoko/rua_qfile.c @@ -98,6 +98,7 @@ bi_qfile_clear (progs_t *pr, void *_res) static void bi_qfile_destroy (progs_t *pr, void *_res) { + free (_res); } static int diff --git a/libs/ruamoko/rua_scene.c b/libs/ruamoko/rua_scene.c index 0993480a2..384c3bfea 100644 --- a/libs/ruamoko/rua_scene.c +++ b/libs/ruamoko/rua_scene.c @@ -657,6 +657,7 @@ bi_scene_clear (progs_t *pr, void *_res) static void bi_scene_destroy (progs_t *pr, void *_res) { + free (_res); } void diff --git a/libs/ruamoko/rua_script.c b/libs/ruamoko/rua_script.c index d11b41b49..5bcc262f2 100644 --- a/libs/ruamoko/rua_script.c +++ b/libs/ruamoko/rua_script.c @@ -94,6 +94,7 @@ bi_script_clear (progs_t *pr, void *_res) static void bi_script_destroy (progs_t *pr, void *_res) { + free (_res); } static void diff --git a/libs/ruamoko/rua_set.c b/libs/ruamoko/rua_set.c index 4e7ea12b6..161cfa8c2 100644 --- a/libs/ruamoko/rua_set.c +++ b/libs/ruamoko/rua_set.c @@ -806,6 +806,7 @@ res_set_clear (progs_t *pr, void *_res) static void res_set_destroy (progs_t *pr, void *_res) { + free (_res); } #define bi(x,np,params...) {#x, bi_##x, -1, np, {params}} diff --git a/libs/video/renderer/r_progs.c b/libs/video/renderer/r_progs.c index 2a22dd05d..4a295e4d8 100644 --- a/libs/video/renderer/r_progs.c +++ b/libs/video/renderer/r_progs.c @@ -491,6 +491,8 @@ bi_draw_destroy (progs_t *pr, void *_res) { draw_resources_t *res = (draw_resources_t *) _res; Hash_DelTable (res->pic_hash); + + free (res); } #define bi(x,np,params...) {#x, bi_##x, -1, np, {params}} diff --git a/ruamoko/qwaq/builtins/curses.c b/ruamoko/qwaq/builtins/curses.c index 4c85b115a..e97e140db 100644 --- a/ruamoko/qwaq/builtins/curses.c +++ b/ruamoko/qwaq/builtins/curses.c @@ -1864,6 +1864,7 @@ bi_curses_clear (progs_t *pr, void *_res) static void bi_curses_destroy (progs_t *pr, void *_res) { + free (_res); } #define bi(x,np,params...) {#x, bi_##x, -1, np, {params}} diff --git a/ruamoko/qwaq/builtins/debug.c b/ruamoko/qwaq/builtins/debug.c index 4e3420e1c..6148076bc 100644 --- a/ruamoko/qwaq/builtins/debug.c +++ b/ruamoko/qwaq/builtins/debug.c @@ -167,6 +167,7 @@ qwaq_debug_clear (progs_t *pr, void *_res) static void qwaq_debug_destroy (progs_t *pr, void *_res) { + free (_res); } static void @@ -181,6 +182,7 @@ qwaq_target_clear (progs_t *pr, void *_res) static void qwaq_target_destroy (progs_t *pr, void *_res) { + // no block to free } static int diff --git a/ruamoko/qwaq/builtins/editbuffer.c b/ruamoko/qwaq/builtins/editbuffer.c index f7f33db8e..0631d3c49 100644 --- a/ruamoko/qwaq/builtins/editbuffer.c +++ b/ruamoko/qwaq/builtins/editbuffer.c @@ -1020,6 +1020,7 @@ qwaq_ebresources_clear (progs_t *pr, void *_res) static void qwaq_ebresources_destroy (progs_t *pr, void *_res) { + free (_res); } #define bi(x,n,np,params...) {#x, bi_##x, n, np, {params}} diff --git a/ruamoko/qwaq/builtins/main.c b/ruamoko/qwaq/builtins/main.c index 3ef07279f..9bab54404 100644 --- a/ruamoko/qwaq/builtins/main.c +++ b/ruamoko/qwaq/builtins/main.c @@ -200,6 +200,7 @@ qwaq_thread_clear (progs_t *pr, void *_thread) static void qwaq_thread_destroy (progs_t *pr, void *_res) { + // resource block is the thread data: don't own it } static progs_t * diff --git a/ruamoko/qwaq/builtins/term-input.c b/ruamoko/qwaq/builtins/term-input.c index 544cd3841..72de4fb74 100644 --- a/ruamoko/qwaq/builtins/term-input.c +++ b/ruamoko/qwaq/builtins/term-input.c @@ -892,6 +892,7 @@ bi_input_clear (progs_t *pr, void *_res) static void bi_input_destroy (progs_t *pr, void *_res) { + free (_res); } static void