From cddc9bf83eb5cd2c69626b31ae7373342523b626 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 16 Mar 2025 23:29:30 +0300 Subject: free some global objects even in the error case, in order to appease Valgrind * If building with --enable-debug, we should always free all heap objects, even if they would be freed on program termination anway, so they won't appear as "possibly lost" in Valgrind. I had this missing if munged or evaled macro failed, which resulted in lots of false positives when running the testsuite under Valgrind. * Also fixes possible crashes due to reusing already set GError variables. This could theoretically happen if a munged script terminates with ^C and its "quit" ED-hook would also throw any error. --- src/main.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/main.c b/src/main.c index 0a33d64..08e9315 100644 --- a/src/main.c +++ b/src/main.c @@ -433,9 +433,8 @@ main(int argc, char **argv) teco_qreg_table_init_locals(&local_qregs, TRUE); if (!teco_ring_edit_by_name(NULL, &error)) { - g_fprintf(stderr, "Error editing unnamed file: %s\n", - error->message); - exit(EXIT_FAILURE); + g_prefix_error_literal(&error, "Error editing unnamed file: "); + goto cleanup; } /* @@ -458,10 +457,12 @@ main(int argc, char **argv) &local_qregs, &error) && !g_error_matches(error, TECO_ERROR, TECO_ERROR_QUIT)) { teco_error_add_frame_toplevel(); - goto error; + goto cleanup; } + g_clear_error(&error); + if (!teco_ed_hook(TECO_ED_HOOK_QUIT, &error)) - goto error; + goto cleanup; goto cleanup; } @@ -494,11 +495,12 @@ main(int argc, char **argv) */ if (!teco_execute_file(mung_filename, &local_qregs, &error) && !g_error_matches(error, TECO_ERROR, TECO_ERROR_QUIT)) - goto error; + goto cleanup; + g_clear_error(&error); if (teco_quit_requested) { if (!teco_ed_hook(TECO_ED_HOOK_QUIT, &error)) - goto error; + goto cleanup; goto cleanup; } } @@ -527,10 +529,10 @@ main(int argc, char **argv) if (!teco_cmdline_keypress(teco_fake_cmdline, strlen(teco_fake_cmdline), &error) && !g_error_matches(error, TECO_ERROR, TECO_ERROR_QUIT)) { teco_error_add_frame_toplevel(); - goto error; + goto cleanup; } } else if (!teco_interface_event_loop(&error)) { - goto error; + goto cleanup; } teco_machine_main_clear(&teco_cmdline.machine); @@ -548,7 +550,7 @@ main(int argc, char **argv) teco_view_set_scintilla_undo(teco_qreg_view, FALSE); if (!teco_ed_hook(TECO_ED_HOOK_QUIT, &error)) - goto error; + goto cleanup; cleanup: #ifndef NDEBUG @@ -559,9 +561,11 @@ cleanup: teco_view_free(teco_qreg_view); #endif teco_interface_cleanup(); - return 0; -error: - teco_error_display_full(error); - return EXIT_FAILURE; + if (error != NULL) { + teco_error_display_full(error); + return EXIT_FAILURE; + } + + return 0; } -- cgit v1.2.3