From c74f58786d0a031a184b0933ddd19f7c48031bcc Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 13 Sep 2012 10:46:09 -0700 Subject: Fix memory leaks and one potential NULL dereference Always make sure to clear the memory allocated at the "existing_filename" pointer when setting it to a new address or NULL. Signed-off-by: Lubomir I. Ivanov Lifted these changes from a larger commit. The other changes I'll reimplement in the next commit. Signed-off-by: Dirk Hohndel --- gtk-gui.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/gtk-gui.c b/gtk-gui.c index bfb40a7bc..5c8d960b2 100644 --- a/gtk-gui.c +++ b/gtk-gui.c @@ -305,6 +305,9 @@ static void file_close(GtkWidget *w, gpointer data) if (unsaved_changes()) if (ask_save_changes() == FALSE) return; + + if (existing_filename) + free(existing_filename); existing_filename = NULL; /* free the dives and trips */ @@ -682,8 +685,12 @@ static void preferences_dialog(GtkWidget *w, gpointer data) /* if we opened the default file and are changing its name, * update existing_filename */ - if (strcmp(current_default, existing_filename) == 0) - existing_filename = (char *)new_default; + if (existing_filename) { + if (strcmp(current_default, existing_filename) == 0) { + free(existing_filename); + existing_filename = strdup(new_default); + } + } if (strcmp(current_default, new_default)) { subsurface_set_conf("default_filename", PREF_STRING, new_default); @@ -1091,6 +1098,8 @@ void exit_ui(void) subsurface_close_conf(); if (default_filename) free((char *)default_filename); + if (existing_filename) + free(existing_filename); } typedef struct { -- cgit v1.2.3-70-g09d2