From 9160eaa4483a692c664612ab7c6afea77abc0f03 Mon Sep 17 00:00:00 2001 From: Siyan Panayotov Date: Sun, 25 Nov 2018 17:09:13 +0100 Subject: [PATCH] Fix minor memory leaks, and UB found by cppcheck --- .gitignore | 2 ++ src/uni-anim-view.c | 14 -------------- src/uni-anim-view.h | 3 +-- src/uni-cache.c | 2 +- src/uni-exiv2.cpp | 14 -------------- src/uni-exiv2.hpp | 2 -- src/uni-image-view.c | 4 ++-- src/uni-nav.c | 2 +- src/vnr-file.c | 18 ++++++------------ src/vnr-prefs.c | 4 +++- src/vnr-tools.c | 3 +-- src/vnr-window.c | 20 +++++++++----------- 12 files changed, 26 insertions(+), 62 deletions(-) diff --git a/.gitignore b/.gitignore index dee44d4..a898f13 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,8 @@ compile libtool *.desktop .deps +.idea +builddir/ viewnior-*.tar.gz *.sublime-project diff --git a/src/uni-anim-view.c b/src/uni-anim-view.c index 7b4af4c..deba498 100755 --- a/src/uni-anim-view.c +++ b/src/uni-anim-view.c @@ -337,17 +337,3 @@ uni_anim_view_set_is_playing (UniAnimView * aview, gboolean playing) else if (playing && aview->anim) uni_anim_view_updator (aview); } - -/** - * uni_anim_view_get_is_playing: - * @aview: A #UniImageView. - * @returns: %TRUE if an animation is playing, %FALSE otherwise. - * - * Returns whether the animation is playing or not. If there is no - * current animation, this method will always returns %FALSE. - **/ -gboolean -uni_anim_view_get_is_playing (UniAnimView * aview) -{ - return aview->timer_id && aview->anim; -} diff --git a/src/uni-anim-view.h b/src/uni-anim-view.h index 55424b0..8dd4325 100755 --- a/src/uni-anim-view.h +++ b/src/uni-anim-view.h @@ -9,6 +9,7 @@ * Viewnior is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. * * Viewnior is distributed in the hope that it will be useful, @@ -82,7 +83,5 @@ void uni_anim_view_set_static (UniAnimView * aview, void uni_anim_view_set_is_playing (UniAnimView * aview, gboolean playing); -gboolean uni_anim_view_get_is_playing (UniAnimView * aview); - G_END_DECLS #endif /* __UNI_ANIM_VIEW_H__ */ diff --git a/src/uni-cache.c b/src/uni-cache.c index 10f8c7a..bc19ef0 100755 --- a/src/uni-cache.c +++ b/src/uni-cache.c @@ -219,7 +219,7 @@ uni_pixbuf_draw_cache_intersect_draw (UniPixbufDrawCache * cache, /* If there is no intersection, we have to scale the whole area from the source pixbuf. */ - GdkRectangle inter; + GdkRectangle inter = {0, 0, 0, 0}; GdkRectangle around[4] = { this, {0, 0, 0, 0}, diff --git a/src/uni-exiv2.cpp b/src/uni-exiv2.cpp index 5e04a13..02fe633 100644 --- a/src/uni-exiv2.cpp +++ b/src/uni-exiv2.cpp @@ -27,20 +27,6 @@ static Exiv2::Image::AutoPtr cached_image; -extern "C" -void -uni_exif_dictionary_map(void (*callback)(const char*, const char*, void*), void *user_data) -{ - uint i; - for( i=0; iis_rendering = TRUE; // Image area is the area on the widget occupied by the pixbuf. - GdkRectangle image_area; + GdkRectangle image_area = {0, 0, 0, 0}; Size alloc = uni_image_view_get_allocated_size (view); uni_image_view_get_draw_rect (view, &image_area); if (image_area.x > 0 || @@ -267,7 +267,7 @@ uni_image_view_repaint_area (UniImageView * view, GdkRectangle * paint_rect) GtkWidget *widget = GTK_WIDGET (view); // Paint area is the area on the widget that should be redrawn. - GdkRectangle paint_area; + GdkRectangle paint_area = {0, 0, 0, 0}; gboolean intersects = gdk_rectangle_intersect (&image_area, paint_rect, &paint_area); diff --git a/src/uni-nav.c b/src/uni-nav.c index ee0b82c..a8c841b 100755 --- a/src/uni-nav.c +++ b/src/uni-nav.c @@ -79,7 +79,7 @@ uni_nav_get_zoom2nav_factor (UniNav * nav) static GdkRectangle gtk_image_get_current_rectangle (UniNav * nav) { - GdkRectangle rect; + GdkRectangle rect = {0, 0, 0, 0}; uni_image_view_get_viewport (nav->view, &rect); /* Convert Zoom space to Nav space coordinates. */ diff --git a/src/vnr-file.c b/src/vnr-file.c index ce5f13d..2649fdc 100644 --- a/src/vnr-file.c +++ b/src/vnr-file.c @@ -45,16 +45,14 @@ static GList * vnr_file_get_supported_mime_types (void) { GSList *format_list, *it; - gchar **mime_types; - int i; if (!supported_mime_types) { format_list = gdk_pixbuf_get_formats (); for (it = format_list; it != NULL; it = it->next) { - mime_types = - gdk_pixbuf_format_get_mime_types ((GdkPixbufFormat *) it->data); + gchar **mime_types = gdk_pixbuf_format_get_mime_types((GdkPixbufFormat *) it->data); + int i; for (i = 0; mime_types[i] != NULL; i++) { supported_mime_types = g_list_prepend (supported_mime_types, @@ -136,8 +134,6 @@ vnr_file_dir_content_to_list(gchar *path, gboolean sort, gboolean include_hidden GFile *file; GFileEnumerator *f_enum ; GFileInfo *file_info; - VnrFile *vnr_file; - const char *mimetype; file = g_file_new_for_path(path); f_enum = g_file_enumerate_children(file, G_FILE_ATTRIBUTE_STANDARD_NAME"," @@ -150,9 +146,9 @@ vnr_file_dir_content_to_list(gchar *path, gboolean sort, gboolean include_hidden while(file_info != NULL){ - vnr_file = vnr_file_new(); + VnrFile *vnr_file = vnr_file_new(); - mimetype =g_file_info_get_content_type(file_info); + const char *mimetype =g_file_info_get_content_type(file_info); if(vnr_file_is_supported_mime_type(mimetype) && (include_hidden || !g_file_info_get_is_hidden (file_info)) ){ vnr_file_set_display_name(vnr_file, (char*)g_file_info_get_display_name (file_info)); @@ -232,16 +228,14 @@ vnr_file_load_single_uri(char *p_path, GList **file_list, gboolean include_hidde void vnr_file_load_uri_list (GSList *uri_list, GList **file_list, gboolean include_hidden, GError **error) { - GFile *file; - GFileInfo *fileinfo; GFileType filetype; gchar *p_path; while(uri_list != NULL) { p_path = uri_list->data; - file = g_file_new_for_path(p_path); - fileinfo = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE"," + GFile *file = g_file_new_for_path(p_path); + GFileInfo *fileinfo = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE"," G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME"," G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE"," G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN, diff --git a/src/vnr-prefs.c b/src/vnr-prefs.c index b3b63f4..a6e1609 100644 --- a/src/vnr-prefs.c +++ b/src/vnr-prefs.c @@ -472,8 +472,10 @@ vnr_prefs_save (VnrPrefs *prefs) if(rcfile != NULL) { - fputs(g_key_file_to_data (conf, NULL, NULL), rcfile); + gchar *data = g_key_file_to_data (conf, NULL, NULL); + fputs(data, rcfile); fclose(rcfile); + g_free(data); } else g_warning("Saving config file: Unable to open the configuration file for writing!\n"); diff --git a/src/vnr-tools.c b/src/vnr-tools.c index bbb9092..76ab6ba 100755 --- a/src/vnr-tools.c +++ b/src/vnr-tools.c @@ -131,12 +131,11 @@ vnr_tools_parse_uri_string_list_to_file_list (const gchar *uri_list) GSList* file_list = NULL; gsize i = 0; gchar **uris; - gchar* current_path; uris = g_uri_list_extract_uris (uri_list); while (uris[i] != NULL) { - current_path = g_file_get_path (g_file_new_for_uri(uris[i])); + gchar* current_path = g_file_get_path (g_file_new_for_uri(uris[i])); if(current_path != NULL) file_list = g_slist_append (file_list, current_path); i++; diff --git a/src/vnr-window.c b/src/vnr-window.c index b6c7289..435b45d 100755 --- a/src/vnr-window.c +++ b/src/vnr-window.c @@ -387,7 +387,7 @@ update_fs_filename_label(VnrWindow *window) return; gint position, total; - char *buf = NULL; + char *buf; get_position_of_element_in_list(window->file_list, &position, &total); buf = g_strdup_printf ("%s - %i/%i", @@ -1141,10 +1141,10 @@ file_open_dialog_response_cb (GtkWidget *dialog, { if (response_id == GTK_RESPONSE_ACCEPT) { - GSList *uri_list = NULL; - uri_list = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (dialog)); + GSList *uri_list = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (dialog)); g_return_if_fail(uri_list != NULL); vnr_window_open_from_list(window, uri_list); + g_slist_free_full(uri_list, g_free); } gtk_widget_destroy (dialog); @@ -1312,8 +1312,8 @@ vnr_window_cmd_open(GtkAction *action, VnrWindow *window) { GtkWidget *dialog; GtkWidget *preview; - GtkFileFilter *img_filter = NULL; - GtkFileFilter *all_filter = NULL; + GtkFileFilter *img_filter; + GtkFileFilter *all_filter; dialog = gtk_file_chooser_dialog_new (_("Open Image"), GTK_WINDOW(window), @@ -1345,10 +1345,9 @@ vnr_window_cmd_open(GtkAction *action, VnrWindow *window) g_signal_connect(GTK_FILE_CHOOSER(dialog), "update-preview", G_CALLBACK(update_preview_cb), preview); - gchar *dirname; if(window->file_list != NULL) { - dirname = g_path_get_dirname (VNR_FILE(window->file_list->data)->path); + gchar *dirname = g_path_get_dirname (VNR_FILE(window->file_list->data)->path); gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(dialog), dirname); g_free(dirname); } @@ -1377,10 +1376,9 @@ vnr_window_cmd_open_dir(GtkAction *action, VnrWindow *window) gtk_window_set_modal (GTK_WINDOW(dialog), FALSE); gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE); - gchar *dirname; if(window->file_list != NULL) { - dirname = g_path_get_dirname (VNR_FILE(window->file_list->data)->path); + gchar *dirname = g_path_get_dirname (VNR_FILE(window->file_list->data)->path); gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(dialog), dirname); g_free(dirname); } @@ -1581,11 +1579,11 @@ vnr_window_cmd_scrollbar (GtkAction *action, VnrWindow *window) static void vnr_window_cmd_slideshow (GtkAction *action, VnrWindow *window) { + g_assert(window != NULL && VNR_IS_WINDOW(window)); + if(!window->slideshow) return; - g_assert(window != NULL && VNR_IS_WINDOW(window)); - gboolean slideshow; slideshow = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));