From 215cfcf993389c7950d58d7a37d9bfb0058b7ea6 Mon Sep 17 00:00:00 2001 From: Vineeth TM Date: Thu, 20 Aug 2015 16:21:59 +0900 Subject: [PATCH] gstreamer: Fix memory leaks when context parse fails When g_option_context_parse fails, context and error variables are not getting free'd which results in memory leaks. Free'ing the same. And replacing g_error_free with g_clear_error, which checks if the error being passed is not NULL and sets the variable to NULL on free'ing. https://bugzilla.gnome.org/show_bug.cgi?id=753851 --- docs/manual/advanced-dataaccess.xml | 6 ++++-- docs/manual/appendix-integration.xml | 3 +++ docs/manual/basics-init.xml | 4 +++- libs/gst/helpers/gst-completion-helper.c | 2 ++ libs/gst/helpers/gst-ptp-helper.c | 16 +++++++++++++++- tests/benchmarks/capsnego.c | 6 ++++-- tests/examples/ptp/ptp-print-times.c | 1 + tools/gst-inspect.c | 4 +++- tools/gst-launch.c | 12 +++++++----- tools/gst-typefind.c | 4 +++- 10 files changed, 45 insertions(+), 13 deletions(-) diff --git a/docs/manual/advanced-dataaccess.xml b/docs/manual/advanced-dataaccess.xml index 555d0ba..fa56b13 100644 --- a/docs/manual/advanced-dataaccess.xml +++ b/docs/manual/advanced-dataaccess.xml @@ -915,7 +915,7 @@ main (int argc, char *argv[]) if (error != NULL) { g_print ("could not construct pipeline: %s\n", error->message); - g_error_free (error); + g_clear_error (&error); exit (-1); } @@ -1475,7 +1475,7 @@ bus_cb (GstBus * bus, GstMessage * msg, gpointer user_data) gst_message_parse_error (msg, &err, &dbg); gst_object_default_error (msg->src, err, dbg); - g_error_free (err); + g_clear_error (&err); g_free (dbg); g_main_loop_quit (loop); break; @@ -1505,6 +1505,8 @@ main (int argc, char **argv) g_option_context_add_group (ctx, gst_init_get_option_group ()); if (!g_option_context_parse (ctx, &argc, &argv, &err)) { g_print ("Error initializing: %s\n", err->message); + g_clear_error (&err); + g_option_context_free (ctx); return 1; } g_option_context_free (ctx); diff --git a/docs/manual/appendix-integration.xml b/docs/manual/appendix-integration.xml index b558788..b288a6b 100644 --- a/docs/manual/appendix-integration.xml +++ b/docs/manual/appendix-integration.xml @@ -127,8 +127,11 @@ main (gint argc, gchar **argv) * calls gtk_init() and gst_init() */ if (!g_option_context_parse (ctx, &argc, &argv, &err)) { g_print ("Error initializing: %s\n", err->message); + g_clear_error (&err); + g_option_context_free (ctx); exit (1); } + g_option_context_free (ctx); /* any filenames we got passed on the command line? parse them! */ if (cmd_filenames != NULL) { diff --git a/docs/manual/basics-init.xml b/docs/manual/basics-init.xml index 7a8467b..3a71be6 100644 --- a/docs/manual/basics-init.xml +++ b/docs/manual/basics-init.xml @@ -102,9 +102,11 @@ main (int argc, g_option_context_add_group (ctx, gst_init_get_option_group ()); if (!g_option_context_parse (ctx, &argc, &argv, &err)) { g_print ("Failed to initialize: %s\n", err->message); - g_error_free (err); + g_clear_error (&err); + g_option_context_free (ctx); return 1; } + g_option_context_free (ctx); printf ("Run me with --help to see the Application options appended.\n"); diff --git a/libs/gst/helpers/gst-completion-helper.c b/libs/gst/helpers/gst-completion-helper.c index e99b73b..95821c8 100644 --- a/libs/gst/helpers/gst-completion-helper.c +++ b/libs/gst/helpers/gst-completion-helper.c @@ -235,6 +235,8 @@ main (int argc, char *argv[]) g_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message)); else g_printerr ("Error initializing: Unknown error!\n"); + g_clear_error (&err); + g_option_context_free (ctx); exit (1); } g_option_context_free (ctx); diff --git a/libs/gst/helpers/gst-ptp-helper.c b/libs/gst/helpers/gst-ptp-helper.c index 6c753df..12a9097 100644 --- a/libs/gst/helpers/gst-ptp-helper.c +++ b/libs/gst/helpers/gst-ptp-helper.c @@ -98,6 +98,7 @@ have_socket_data_cb (GSocket * socket, GIOCondition condition, read = g_socket_receive (socket, buffer, sizeof (buffer), NULL, &err); if (read == -1) g_error ("Failed to read from socket: %s", err->message); + g_clear_error (&err); if (verbose) g_message ("Received %" G_GSSIZE_FORMAT " bytes from %s socket", read, @@ -111,6 +112,7 @@ have_socket_data_cb (GSocket * socket, GIOCondition condition, sizeof (header), &written, &err); if (status == G_IO_STATUS_ERROR) { g_error ("Failed to write to stdout: %s", err->message); + g_clear_error (&err); } else if (status == G_IO_STATUS_EOF) { g_message ("EOF on stdout"); exit (0); @@ -124,6 +126,7 @@ have_socket_data_cb (GSocket * socket, GIOCondition condition, g_io_channel_write_chars (stdout_channel, buffer, read, &written, &err); if (status == G_IO_STATUS_ERROR) { g_error ("Failed to write to stdout: %s", err->message); + g_clear_error (&err); } else if (status == G_IO_STATUS_EOF) { g_message ("EOF on stdout"); exit (0); @@ -157,6 +160,7 @@ have_stdin_data_cb (GIOChannel * channel, GIOCondition condition, &read, &err); if (status == G_IO_STATUS_ERROR) { g_error ("Failed to read from stdin: %s", err->message); + g_clear_error (&err); } else if (status == G_IO_STATUS_EOF) { g_message ("EOF on stdin"); exit (0); @@ -171,6 +175,7 @@ have_stdin_data_cb (GIOChannel * channel, GIOCondition condition, status = g_io_channel_read_chars (channel, buffer, header.size, &read, &err); if (status == G_IO_STATUS_ERROR) { g_error ("Failed to read from stdin: %s", err->message); + g_clear_error (&err); } else if (status == G_IO_STATUS_EOF) { g_message ("EOF on stdin"); exit (0); @@ -192,7 +197,7 @@ have_stdin_data_cb (GIOChannel * channel, GIOCondition condition, g_error ("Failed to write to socket: %s", err->message); else if (written != header.size) g_error ("Unexpected write size: %" G_GSSIZE_FORMAT, written); - + g_clear_error (&err); if (verbose) g_message ("Sent %" G_GSSIZE_FORMAT " bytes to %s socket", read, (header.type == TYPE_EVENT ? "event" : "general")); @@ -219,12 +224,14 @@ setup_sockets (void) G_SOCKET_PROTOCOL_UDP, &err); if (!socket_event) g_error ("Couldn't create event socket: %s", err->message); + g_clear_error (&err); socket_general = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &err); if (!socket_general) g_error ("Couldn't create general socket: %s", err->message); + g_clear_error (&err); /* Bind sockets */ bind_addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4); @@ -458,18 +465,22 @@ setup_sockets (void) if (!g_socket_join_multicast_group (socket_event, mcast_addr, FALSE, NULL, &err)) g_error ("Couldn't join multicast group: %s", err->message); + g_clear_error (&err); if (!g_socket_join_multicast_group (socket_general, mcast_addr, FALSE, NULL, &err)) g_error ("Couldn't join multicast group: %s", err->message); + g_clear_error (&err); } } else { /* Join multicast group without any interface */ if (!g_socket_join_multicast_group (socket_event, mcast_addr, FALSE, NULL, &err)) g_error ("Couldn't join multicast group: %s", err->message); + g_clear_error (&err); if (!g_socket_join_multicast_group (socket_general, mcast_addr, FALSE, NULL, &err)) g_error ("Couldn't join multicast group: %s", err->message); + g_clear_error (&err); } event_saddr = g_inet_socket_address_new (mcast_addr, PTP_EVENT_PORT); @@ -592,6 +603,7 @@ write_clock_id (void) sizeof (header), &written, &err); if (status == G_IO_STATUS_ERROR) { g_error ("Failed to write to stdout: %s", err->message); + g_clear_error (&err); } else if (status == G_IO_STATUS_EOF) { g_message ("EOF on stdout"); exit (0); @@ -606,6 +618,7 @@ write_clock_id (void) (const gchar *) clock_id_array, sizeof (clock_id_array), &written, &err); if (status == G_IO_STATUS_ERROR) { g_error ("Failed to write to stdout: %s", err->message); + g_clear_error (&err); } else if (status == G_IO_STATUS_EOF) { g_message ("EOF on stdout"); exit (0); @@ -654,6 +667,7 @@ main (gint argc, gchar ** argv) g_option_context_add_main_entries (opt_ctx, opt_entries, NULL); if (!g_option_context_parse (opt_ctx, &argc, &argv, &err)) g_error ("Error parsing options: %s", err->message); + g_clear_error (&err); g_option_context_free (opt_ctx); setup_sockets (); diff --git a/tests/benchmarks/capsnego.c b/tests/benchmarks/capsnego.c index e035ddc..dc60194 100644 --- a/tests/benchmarks/capsnego.c +++ b/tests/benchmarks/capsnego.c @@ -157,7 +157,7 @@ event_loop (GstElement * bin) gst_message_parse_warning (msg, &err, &dbg); GST_WARNING_OBJECT (GST_MESSAGE_SRC (msg), "%s (%s)", err->message, (dbg ? dbg : "no details")); - g_error_free (err); + g_clear_error (&err); g_free (dbg); break; } @@ -168,7 +168,7 @@ event_loop (GstElement * bin) gst_message_parse_error (msg, &err, &dbg); GST_ERROR_OBJECT (GST_MESSAGE_SRC (msg), "%s (%s)", err->message, (dbg ? dbg : "no details")); - g_error_free (err); + g_clear_error (&err); g_free (dbg); running = FALSE; break; @@ -218,6 +218,8 @@ main (gint argc, gchar * argv[]) g_option_context_add_group (ctx, gst_init_get_option_group ()); if (!g_option_context_parse (ctx, &argc, &argv, &err)) { g_print ("Error initializing: %s\n", GST_STR_NULL (err->message)); + g_clear_error (&err); + g_option_context_free (ctx); return 1; } g_option_context_free (ctx); diff --git a/tests/examples/ptp/ptp-print-times.c b/tests/examples/ptp/ptp-print-times.c index e6c666f..b7de82a 100644 --- a/tests/examples/ptp/ptp-print-times.c +++ b/tests/examples/ptp/ptp-print-times.c @@ -70,6 +70,7 @@ main (gint argc, gchar ** argv) g_option_context_add_group (opt_ctx, gst_init_get_option_group ()); if (!g_option_context_parse (opt_ctx, &argc, &argv, &err)) g_error ("Error parsing options: %s", err->message); + g_clear_error (&err); g_option_context_free (opt_ctx); if (!gst_ptp_init (GST_PTP_CLOCK_ID_NONE, NULL)) diff --git a/tools/gst-inspect.c b/tools/gst-inspect.c index 141c7bd..bb0129a 100644 --- a/tools/gst-inspect.c +++ b/tools/gst-inspect.c @@ -1499,6 +1499,8 @@ main (int argc, char *argv[]) g_option_context_add_group (ctx, gst_init_get_option_group ()); if (!g_option_context_parse (ctx, &argc, &argv, &err)) { g_printerr ("Error initializing: %s\n", err->message); + g_clear_error (&err); + g_option_context_free (ctx); return -1; } g_option_context_free (ctx); @@ -1615,7 +1617,7 @@ main (int argc, char *argv[]) } } else { g_printerr (_("Could not load plugin file: %s\n"), error->message); - g_error_free (error); + g_clear_error (&error); return -1; } } else { diff --git a/tools/gst-launch.c b/tools/gst-launch.c index afbb333..839bd64 100644 --- a/tools/gst-launch.c +++ b/tools/gst-launch.c @@ -325,7 +325,7 @@ print_error_message (GstMessage * msg) if (debug != NULL) g_printerr (_("Additional debug info:\n%s\n"), debug); - g_error_free (err); + g_clear_error (&err); g_free (debug); g_free (name); } @@ -649,7 +649,7 @@ event_loop (GstElement * pipeline, gboolean blocking, gboolean do_progress, if (debug) { PRINT (_("INFO:\n%s\n"), debug); } - g_error_free (gerror); + g_clear_error (&gerror); g_free (debug); g_free (name); break; @@ -668,7 +668,7 @@ event_loop (GstElement * pipeline, gboolean blocking, gboolean do_progress, if (debug) { PRINT (_("Additional debug info:\n%s\n"), debug); } - g_error_free (gerror); + g_clear_error (&gerror); g_free (debug); g_free (name); break; @@ -973,6 +973,8 @@ main (int argc, char *argv[]) g_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message)); else g_printerr ("Error initializing: Unknown error!\n"); + g_clear_error (&error); + g_option_context_free (ctx); exit (1); } g_option_context_free (ctx); @@ -1000,7 +1002,7 @@ main (int argc, char *argv[]) if (error) { g_printerr (_("ERROR: pipeline could not be constructed: %s.\n"), GST_STR_NULL (error->message)); - g_error_free (error); + g_clear_error (&error); } else { g_printerr (_("ERROR: pipeline could not be constructed.\n")); } @@ -1008,7 +1010,7 @@ main (int argc, char *argv[]) } else if (error) { g_printerr (_("WARNING: erroneous pipeline: %s\n"), GST_STR_NULL (error->message)); - g_error_free (error); + g_clear_error (&error); return 1; } diff --git a/tools/gst-typefind.c b/tools/gst-typefind.c index 830d7e6..490aca8 100644 --- a/tools/gst-typefind.c +++ b/tools/gst-typefind.c @@ -106,7 +106,7 @@ typefind_file (const gchar * filename) if (msg) { gst_message_parse_error (msg, &err, NULL); g_printerr ("%s - FAILED: %s\n", filename, err->message); - g_error_free (err); + g_clear_error (&err); gst_message_unref (msg); } else { g_printerr ("%s - FAILED: unknown error\n", filename); @@ -162,6 +162,8 @@ main (int argc, char *argv[]) g_option_context_add_group (ctx, gst_init_get_option_group ()); if (!g_option_context_parse (ctx, &argc, &argv, &err)) { g_print ("Error initializing: %s\n", GST_STR_NULL (err->message)); + g_clear_error (&err); + g_option_context_free (ctx); exit (1); } g_option_context_free (ctx); -- 2.7.4