From b1b1fe52d611670c7718533d67d164e4ffa3c975 Mon Sep 17 00:00:00 2001 From: Thomas Vander Stichele Date: Tue, 1 Nov 2005 15:11:16 +0000 Subject: [PATCH] Fix leaks and invalid memory access as reported by valgrind Original commit message from CVS: * check/elements/matroskamux.c: (setup_src_pad), (setup_sink_pad), (setup_matroskamux), (check_buffer_data), (GST_START_TEST): * gst/matroska/matroska-mux.c: (gst_matroska_mux_finalize), (gst_matroska_mux_reset), (gst_matroska_mux_audio_pad_setcaps), (gst_matroska_mux_start), (gst_matroska_mux_write_data), (gst_matroska_mux_collected): Fix leaks and invalid memory access as reported by valgrind --- ChangeLog | 10 ++++++++++ check/elements/matroskamux.c | 17 ++++++++++------- gst/matroska/matroska-mux.c | 29 ++++++++++++++++++----------- tests/check/elements/matroskamux.c | 17 ++++++++++------- 4 files changed, 48 insertions(+), 25 deletions(-) diff --git a/ChangeLog b/ChangeLog index b42970dd3e..160149447b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2005-11-01 Thomas Vander Stichele + + * check/elements/matroskamux.c: (setup_src_pad), (setup_sink_pad), + (setup_matroskamux), (check_buffer_data), (GST_START_TEST): + * gst/matroska/matroska-mux.c: (gst_matroska_mux_finalize), + (gst_matroska_mux_reset), (gst_matroska_mux_audio_pad_setcaps), + (gst_matroska_mux_start), (gst_matroska_mux_write_data), + (gst_matroska_mux_collected): + Fix leaks and invalid memory access as reported by valgrind + 2005-11-01 Thomas Vander Stichele Patch by: Michal Benes diff --git a/check/elements/matroskamux.c b/check/elements/matroskamux.c index 8b31014e72..30bf09f18d 100644 --- a/check/elements/matroskamux.c +++ b/check/elements/matroskamux.c @@ -56,15 +56,16 @@ static GstStaticPadTemplate srcac3template = GST_STATIC_PAD_TEMPLATE ("src", GstPad * setup_src_pad (GstElement * element, - GstStaticPadTemplate * srctemplate, GstCaps * caps) + GstStaticPadTemplate * template, GstCaps * caps) { GstPad *srcpad, *sinkpad; + GstPadTemplate *templ; GST_DEBUG_OBJECT (element, "setting up sending pad"); /* sending pad */ - srcpad = - gst_pad_new_from_template (gst_static_pad_template_get (srctemplate), - "src"); + templ = gst_static_pad_template_get (template); + srcpad = gst_pad_new_from_template (templ, "src"); + gst_object_unref (templ); fail_if (srcpad == NULL, "Could not create a srcpad"); ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1); @@ -110,12 +111,14 @@ setup_sink_pad (GstElement * element, GstStaticPadTemplate * template, GstCaps * caps) { GstPad *srcpad, *sinkpad; + GstPadTemplate *templ; GST_DEBUG_OBJECT (element, "setting up receiving pad"); /* receiving pad */ - sinkpad = - gst_pad_new_from_template (gst_static_pad_template_get (template), - "sink"); + templ = gst_static_pad_template_get (template); + sinkpad = gst_pad_new_from_template (templ, "sink"); + gst_object_unref (templ); + fail_if (sinkpad == NULL, "Could not create a sinkpad"); srcpad = gst_element_get_pad (element, "src"); diff --git a/gst/matroska/matroska-mux.c b/gst/matroska/matroska-mux.c index d922cbdafe..78dcf960cf 100644 --- a/gst/matroska/matroska-mux.c +++ b/gst/matroska/matroska-mux.c @@ -258,6 +258,8 @@ gst_matroska_mux_finalize (GObject * object) gst_object_unref (mux->collect); gst_object_unref (mux->ebml_write); + if (mux->writing_app) + g_free (mux->writing_app); G_OBJECT_CLASS (parent_class)->finalize (object); } @@ -346,7 +348,7 @@ gst_matroska_mux_reset (GstElement * element) /* reset writing_app */ if (mux->writing_app) { - free (mux->writing_app); + g_free (mux->writing_app); } mux->writing_app = g_strdup ("GStreamer Matroska muxer"); @@ -752,11 +754,15 @@ gst_matroska_mux_audio_pad_setcaps (GstPad * pad, GstCaps * caps) offset += GST_BUFFER_SIZE (buf[i]); } - if (memcmp (GST_BUFFER_DATA (buf[0]) + 1, "vorbis", 6) == 0) { - guint8 *hdr = GST_BUFFER_DATA (buf[0]) + 1 + 6 + 4; + if (GST_BUFFER_SIZE (buf[0]) < 1 + 6 + 4) { + GST_WARNING ("First vorbis header too small, ignoring"); + } else { + if (memcmp (GST_BUFFER_DATA (buf[0]) + 1, "vorbis", 6) == 0) { + guint8 *hdr = GST_BUFFER_DATA (buf[0]) + 1 + 6 + 4; - audiocontext->channels = GST_READ_UINT8 (hdr); - audiocontext->samplerate = GST_READ_UINT32_LE (hdr + 1); + audiocontext->channels = GST_READ_UINT8 (hdr); + audiocontext->samplerate = GST_READ_UINT32_LE (hdr + 1); + } } } else { GST_WARNING ("Vorbis header does not contain " @@ -1031,6 +1037,7 @@ gst_matroska_mux_start (GstMatroskaMux * mux) g_free (rand); gst_ebml_write_binary (ebml, GST_MATROSKA_ID_SEGMENTUID, (guint8 *) segment_uid, 16); + g_free (segment_uid); gst_ebml_write_uint (ebml, GST_MATROSKA_ID_TIMECODESCALE, mux->time_scale); mux->duration_pos = ebml->pos; /* get duration */ @@ -1046,10 +1053,10 @@ gst_matroska_mux_start (GstMatroskaMux * mux) /* Query the total length of the track. */ peerpad = gst_pad_get_peer (thepad); - GST_DEBUG ("Querying duration on pad %s:%s", GST_DEBUG_PAD_NAME (thepad)); + GST_DEBUG_OBJECT (thepad, "querying duration"); if (gst_pad_query_duration (peerpad, &format, &trackduration)) { - GST_DEBUG ("%s:%s - duration: %" GST_TIME_FORMAT, - GST_DEBUG_PAD_NAME (thepad), GST_TIME_ARGS (trackduration)); + GST_DEBUG_OBJECT (thepad, "%duration: %" GST_TIME_FORMAT, + GST_TIME_ARGS (trackduration)); if ((gdouble) trackduration > duration) { duration = (gdouble) trackduration; } @@ -1323,12 +1330,12 @@ gst_matroska_mux_write_data (GstMatroskaMux * mux) /* if there is no best pad, we have reached EOS */ if (best == NULL) { - GST_DEBUG ("No best pad finishing..."); + GST_DEBUG_OBJECT (mux, "No best pad finishing..."); gst_matroska_mux_finish (mux); gst_pad_push_event (mux->srcpad, gst_event_new_eos ()); return GST_FLOW_WRONG_STATE; } - GST_DEBUG ("Best pad %s.", gst_pad_get_name (best->collect.pad)); + GST_DEBUG_OBJECT (best->collect.pad, "best pad"); /* write data */ buf = best->buffer; @@ -1478,7 +1485,7 @@ gst_matroska_mux_collected (GstCollectPads * pads, gpointer user_data) { GstMatroskaMux *mux = GST_MATROSKA_MUX (user_data); - GST_DEBUG ("Collected pads"); + GST_DEBUG_OBJECT (mux, "Collected pads"); /* start with a header */ if (mux->state == GST_MATROSKA_MUX_STATE_START) { diff --git a/tests/check/elements/matroskamux.c b/tests/check/elements/matroskamux.c index 8b31014e72..30bf09f18d 100644 --- a/tests/check/elements/matroskamux.c +++ b/tests/check/elements/matroskamux.c @@ -56,15 +56,16 @@ static GstStaticPadTemplate srcac3template = GST_STATIC_PAD_TEMPLATE ("src", GstPad * setup_src_pad (GstElement * element, - GstStaticPadTemplate * srctemplate, GstCaps * caps) + GstStaticPadTemplate * template, GstCaps * caps) { GstPad *srcpad, *sinkpad; + GstPadTemplate *templ; GST_DEBUG_OBJECT (element, "setting up sending pad"); /* sending pad */ - srcpad = - gst_pad_new_from_template (gst_static_pad_template_get (srctemplate), - "src"); + templ = gst_static_pad_template_get (template); + srcpad = gst_pad_new_from_template (templ, "src"); + gst_object_unref (templ); fail_if (srcpad == NULL, "Could not create a srcpad"); ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1); @@ -110,12 +111,14 @@ setup_sink_pad (GstElement * element, GstStaticPadTemplate * template, GstCaps * caps) { GstPad *srcpad, *sinkpad; + GstPadTemplate *templ; GST_DEBUG_OBJECT (element, "setting up receiving pad"); /* receiving pad */ - sinkpad = - gst_pad_new_from_template (gst_static_pad_template_get (template), - "sink"); + templ = gst_static_pad_template_get (template); + sinkpad = gst_pad_new_from_template (templ, "sink"); + gst_object_unref (templ); + fail_if (sinkpad == NULL, "Could not create a sinkpad"); srcpad = gst_element_get_pad (element, "src");