Blame SOURCES/0001-mxfdemux-Store-GstMXFDemuxEssenceTrack-in-their-own-.patch

4c3a81
From 057131f23df3c40d8ee81fe5b160a6056408ff52 Mon Sep 17 00:00:00 2001
4c3a81
From: Wim Taymans <wtaymans@redhat.com>
4c3a81
Date: Tue, 12 Dec 2023 18:00:58 +0100
4c3a81
Subject: [PATCH] mxfdemux: Store GstMXFDemuxEssenceTrack in their own fixed
4c3a81
 allocation
4c3a81
4c3a81
Previously they were stored inline inside a GArray, but as references to
4c3a81
the tracks were stored in various other places although the array could
4c3a81
still be updated (and reallocated!), this could lead to dangling
4c3a81
references in various places.
4c3a81
4c3a81
Instead now store them in a GPtrArray in their own allocation so each
4c3a81
track's memory position stays fixed.
4c3a81
4c3a81
Fixes ZDI-CAN-22299
4c3a81
4c3a81
Fixes https://gitlab.freedesktop.org/gstreamer/gstreamer/-/issues/3055
4c3a81
4c3a81
Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/5638>
4c3a81
---
4c3a81
 gst/mxf/mxfdemux.c | 114 +++++++++++++++++++++------------------------
4c3a81
 gst/mxf/mxfdemux.h |   2 +-
4c3a81
 2 files changed, 53 insertions(+), 63 deletions(-)
4c3a81
4c3a81
diff --git a/gst/mxf/mxfdemux.c b/gst/mxf/mxfdemux.c
4c3a81
index 54ceec1df..6d76da9c1 100644
4c3a81
--- a/gst/mxf/mxfdemux.c
4c3a81
+++ b/gst/mxf/mxfdemux.c
4c3a81
@@ -153,10 +153,25 @@ gst_mxf_demux_partition_free (GstMXFDemuxPartition * partition)
4c3a81
 }
4c3a81
 
4c3a81
 static void
4c3a81
-gst_mxf_demux_reset_mxf_state (GstMXFDemux * demux)
4c3a81
+gst_mxf_demux_essence_track_free (GstMXFDemuxEssenceTrack * t)
4c3a81
 {
4c3a81
-  guint i;
4c3a81
+  if (t->offsets)
4c3a81
+    g_array_free (t->offsets, TRUE);
4c3a81
+
4c3a81
+  g_free (t->mapping_data);
4c3a81
+
4c3a81
+  if (t->tags)
4c3a81
+    gst_tag_list_unref (t->tags);
4c3a81
+
4c3a81
+  if (t->caps)
4c3a81
+    gst_caps_unref (t->caps);
4c3a81
+
4c3a81
+  g_free (t);
4c3a81
+}
4c3a81
 
4c3a81
+static void
4c3a81
+gst_mxf_demux_reset_mxf_state (GstMXFDemux * demux)
4c3a81
+{
4c3a81
   GST_DEBUG_OBJECT (demux, "Resetting MXF state");
4c3a81
 
4c3a81
   g_list_foreach (demux->partitions, (GFunc) gst_mxf_demux_partition_free,
4c3a81
@@ -166,22 +181,7 @@ gst_mxf_demux_reset_mxf_state (GstMXFDemux * demux)
4c3a81
 
4c3a81
   demux->current_partition = NULL;
4c3a81
 
4c3a81
-  for (i = 0; i < demux->essence_tracks->len; i++) {
4c3a81
-    GstMXFDemuxEssenceTrack *t =
4c3a81
-        &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack, i);
4c3a81
-
4c3a81
-    if (t->offsets)
4c3a81
-      g_array_free (t->offsets, TRUE);
4c3a81
-
4c3a81
-    g_free (t->mapping_data);
4c3a81
-
4c3a81
-    if (t->tags)
4c3a81
-      gst_tag_list_unref (t->tags);
4c3a81
-
4c3a81
-    if (t->caps)
4c3a81
-      gst_caps_unref (t->caps);
4c3a81
-  }
4c3a81
-  g_array_set_size (demux->essence_tracks, 0);
4c3a81
+  g_ptr_array_set_size (demux->essence_tracks, 0);
4c3a81
 }
4c3a81
 
4c3a81
 static void
4c3a81
@@ -199,7 +199,7 @@ gst_mxf_demux_reset_linked_metadata (GstMXFDemux * demux)
4c3a81
 
4c3a81
   for (i = 0; i < demux->essence_tracks->len; i++) {
4c3a81
     GstMXFDemuxEssenceTrack *track =
4c3a81
-        &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack, i);
4c3a81
+        g_ptr_array_index (demux->essence_tracks, i);
4c3a81
 
4c3a81
     track->source_package = NULL;
4c3a81
     track->source_track = NULL;
4c3a81
@@ -707,8 +707,7 @@ gst_mxf_demux_update_essence_tracks (GstMXFDemux * demux)
4c3a81
 
4c3a81
       for (k = 0; k < demux->essence_tracks->len; k++) {
4c3a81
         GstMXFDemuxEssenceTrack *tmp =
4c3a81
-            &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack,
4c3a81
-            k);
4c3a81
+            g_ptr_array_index (demux->essence_tracks, k);
4c3a81
 
4c3a81
         if (tmp->track_number == track->parent.track_number &&
4c3a81
             tmp->body_sid == edata->body_sid) {
4c3a81
@@ -726,24 +725,23 @@ gst_mxf_demux_update_essence_tracks (GstMXFDemux * demux)
4c3a81
       }
4c3a81
 
4c3a81
       if (!etrack) {
4c3a81
-        GstMXFDemuxEssenceTrack tmp;
4c3a81
+        GstMXFDemuxEssenceTrack *tmp = g_new0 (GstMXFDemuxEssenceTrack, 1);
4c3a81
 
4c3a81
-        memset (&tmp, 0, sizeof (tmp));
4c3a81
-        tmp.body_sid = edata->body_sid;
4c3a81
-        tmp.index_sid = edata->index_sid;
4c3a81
-        tmp.track_number = track->parent.track_number;
4c3a81
-        tmp.track_id = track->parent.track_id;
4c3a81
-        memcpy (&tmp.source_package_uid, &package->parent.package_uid, 32);
4c3a81
+        tmp->body_sid = edata->body_sid;
4c3a81
+        tmp->index_sid = edata->index_sid;
4c3a81
+        tmp->track_number = track->parent.track_number;
4c3a81
+        tmp->track_id = track->parent.track_id;
4c3a81
+        memcpy (&tmp->source_package_uid, &package->parent.package_uid, 32);
4c3a81
 
4c3a81
         if (demux->current_partition->partition.body_sid == edata->body_sid &&
4c3a81
             demux->current_partition->partition.body_offset == 0)
4c3a81
-          tmp.position = 0;
4c3a81
+          tmp->position = 0;
4c3a81
         else
4c3a81
-          tmp.position = -1;
4c3a81
+          tmp->position = -1;
4c3a81
 
4c3a81
-        g_array_append_val (demux->essence_tracks, tmp);
4c3a81
+        g_ptr_array_add (demux->essence_tracks, tmp);
4c3a81
         etrack =
4c3a81
-            &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack,
4c3a81
+            g_ptr_array_index (demux->essence_tracks,
4c3a81
             demux->essence_tracks->len - 1);
4c3a81
         new = TRUE;
4c3a81
       }
4c3a81
@@ -869,13 +867,7 @@ gst_mxf_demux_update_essence_tracks (GstMXFDemux * demux)
4c3a81
 
4c3a81
     next:
4c3a81
       if (new) {
4c3a81
-        g_free (etrack->mapping_data);
4c3a81
-        if (etrack->tags)
4c3a81
-          gst_tag_list_unref (etrack->tags);
4c3a81
-        if (etrack->caps)
4c3a81
-          gst_caps_unref (etrack->caps);
4c3a81
-
4c3a81
-        g_array_remove_index (demux->essence_tracks,
4c3a81
+        g_ptr_array_remove_index (demux->essence_tracks,
4c3a81
             demux->essence_tracks->len - 1);
4c3a81
       }
4c3a81
     }
4c3a81
@@ -888,7 +880,7 @@ gst_mxf_demux_update_essence_tracks (GstMXFDemux * demux)
4c3a81
 
4c3a81
   for (i = 0; i < demux->essence_tracks->len; i++) {
4c3a81
     GstMXFDemuxEssenceTrack *etrack =
4c3a81
-        &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack, i);
4c3a81
+        g_ptr_array_index (demux->essence_tracks, i);
4c3a81
 
4c3a81
     if (!etrack->source_package || !etrack->source_track || !etrack->caps) {
4c3a81
       GST_ERROR_OBJECT (demux, "Failed to update essence track %u", i);
4c3a81
@@ -1055,7 +1047,7 @@ gst_mxf_demux_update_tracks (GstMXFDemux * demux)
4c3a81
 
4c3a81
     for (k = 0; k < demux->essence_tracks->len; k++) {
4c3a81
       GstMXFDemuxEssenceTrack *tmp =
4c3a81
-          &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack, k);
4c3a81
+          g_ptr_array_index (demux->essence_tracks, k);
4c3a81
 
4c3a81
       if (tmp->source_package == source_package &&
4c3a81
           tmp->source_track == source_track) {
4c3a81
@@ -1533,8 +1525,7 @@ gst_mxf_demux_pad_set_component (GstMXFDemux * demux, GstMXFDemuxPad * pad,
4c3a81
   pad->current_essence_track = NULL;
4c3a81
 
4c3a81
   for (k = 0; k < demux->essence_tracks->len; k++) {
4c3a81
-    GstMXFDemuxEssenceTrack *tmp =
4c3a81
-        &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack, k);
4c3a81
+    GstMXFDemuxEssenceTrack *tmp = g_ptr_array_index (demux->essence_tracks, k);
4c3a81
 
4c3a81
     if (tmp->source_package == source_package &&
4c3a81
         tmp->source_track == source_track) {
4c3a81
@@ -1657,7 +1648,7 @@ gst_mxf_demux_handle_generic_container_essence_element (GstMXFDemux * demux,
4c3a81
 
4c3a81
   for (i = 0; i < demux->essence_tracks->len; i++) {
4c3a81
     GstMXFDemuxEssenceTrack *tmp =
4c3a81
-        &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack, i);
4c3a81
+          g_ptr_array_index (demux->essence_tracks, i);
4c3a81
 
4c3a81
     if (tmp->body_sid == demux->current_partition->partition.body_sid &&
4c3a81
         (tmp->track_number == track_number || tmp->track_number == 0)) {
4c3a81
@@ -2505,7 +2496,7 @@ gst_mxf_demux_handle_klv_packet (GstMXFDemux * demux, const MXFUL * key,
4c3a81
 
4c3a81
       for (i = 0; i < demux->essence_tracks->len; i++) {
4c3a81
         GstMXFDemuxEssenceTrack *etrack =
4c3a81
-            &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack, i);
4c3a81
+                g_ptr_array_index (demux->essence_tracks, i);
4c3a81
 
4c3a81
         if (etrack->body_sid != demux->current_partition->partition.body_sid)
4c3a81
           continue;
4c3a81
@@ -2568,7 +2559,7 @@ gst_mxf_demux_handle_klv_packet (GstMXFDemux * demux, const MXFUL * key,
4c3a81
       guint i;
4c3a81
       for (i = 0; i < demux->essence_tracks->len; i++) {
4c3a81
         GstMXFDemuxEssenceTrack *etrack =
4c3a81
-            &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack, i);
4c3a81
+            g_ptr_array_index (demux->essence_tracks, i);
4c3a81
 
4c3a81
         if (etrack->body_sid != demux->current_partition->partition.body_sid)
4c3a81
           continue;
4c3a81
@@ -2763,7 +2754,7 @@ from_index:
4c3a81
 
4c3a81
     for (i = 0; i < demux->essence_tracks->len; i++) {
4c3a81
       GstMXFDemuxEssenceTrack *t =
4c3a81
-          &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack, i);
4c3a81
+          g_ptr_array_index (demux->essence_tracks, i);
4c3a81
 
4c3a81
       if (index_start_position != -1 && t == etrack)
4c3a81
         t->position = index_start_position;
4c3a81
@@ -2786,8 +2777,7 @@ from_index:
4c3a81
       if (ret == GST_FLOW_EOS) {
4c3a81
         for (i = 0; i < demux->essence_tracks->len; i++) {
4c3a81
           GstMXFDemuxEssenceTrack *t =
4c3a81
-              &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack,
4c3a81
-              i);
4c3a81
+              g_ptr_array_index (demux->essence_tracks, i);
4c3a81
 
4c3a81
           if (t->position > 0)
4c3a81
             t->duration = t->position;
4c3a81
@@ -2869,7 +2859,7 @@ gst_mxf_demux_pull_and_handle_klv_packet (GstMXFDemux * demux)
4c3a81
 
4c3a81
     for (i = 0; i < demux->essence_tracks->len; i++) {
4c3a81
       GstMXFDemuxEssenceTrack *t =
4c3a81
-          &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack, i);
4c3a81
+          g_ptr_array_index (demux->essence_tracks, i);
4c3a81
 
4c3a81
       if (t->position > 0)
4c3a81
         t->duration = t->position;
4c3a81
@@ -3472,8 +3462,8 @@ gst_mxf_demux_seek_push (GstMXFDemux * demux, GstEvent * event)
4c3a81
   }
4c3a81
 
4c3a81
   for (i = 0; i < demux->essence_tracks->len; i++) {
4c3a81
-    GstMXFDemuxEssenceTrack *t =
4c3a81
-        &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack, i);
4c3a81
+    GstMXFDemuxEssenceTrack *t = g_ptr_array_index (demux->essence_tracks, i);
4c3a81
+
4c3a81
     t->position = -1;
4c3a81
   }
4c3a81
 
4c3a81
@@ -3808,8 +3798,8 @@ gst_mxf_demux_seek_pull (GstMXFDemux * demux, GstEvent * event)
4c3a81
   }
4c3a81
 
4c3a81
   for (i = 0; i < demux->essence_tracks->len; i++) {
4c3a81
-    GstMXFDemuxEssenceTrack *t =
4c3a81
-        &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack, i);
4c3a81
+    GstMXFDemuxEssenceTrack *t = g_ptr_array_index (demux->essence_tracks, i);
4c3a81
+
4c3a81
     t->position = -1;
4c3a81
   }
4c3a81
 
4c3a81
@@ -4097,7 +4087,7 @@ gst_mxf_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
4c3a81
 
4c3a81
       for (i = 0; i < demux->essence_tracks->len; i++) {
4c3a81
         GstMXFDemuxEssenceTrack *t =
4c3a81
-            &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack, i);
4c3a81
+            g_ptr_array_index (demux->essence_tracks, i);
4c3a81
 
4c3a81
         if (t->position > 0)
4c3a81
           t->duration = t->position;
4c3a81
@@ -4138,8 +4128,8 @@ gst_mxf_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
4c3a81
 
4c3a81
           for (i = 0; i < demux->essence_tracks->len; i++) {
4c3a81
             GstMXFDemuxEssenceTrack *etrack =
4c3a81
-                &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack,
4c3a81
-                i);
4c3a81
+                g_ptr_array_index (demux->essence_tracks, i);
4c3a81
+
4c3a81
             etrack->position = -1;
4c3a81
           }
4c3a81
           ret = TRUE;
4c3a81
@@ -4163,8 +4153,8 @@ gst_mxf_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
4c3a81
 
4c3a81
       for (i = 0; i < demux->essence_tracks->len; i++) {
4c3a81
         GstMXFDemuxEssenceTrack *t =
4c3a81
-            &g_array_index (demux->essence_tracks, GstMXFDemuxEssenceTrack,
4c3a81
-            i);
4c3a81
+            g_ptr_array_index (demux->essence_tracks, i);
4c3a81
+
4c3a81
         t->position = -1;
4c3a81
       }
4c3a81
       demux->current_partition = NULL;
4c3a81
@@ -4435,7 +4425,7 @@ gst_mxf_demux_finalize (GObject * object)
4c3a81
 
4c3a81
   g_ptr_array_free (demux->src, TRUE);
4c3a81
   demux->src = NULL;
4c3a81
-  g_array_free (demux->essence_tracks, TRUE);
4c3a81
+  g_ptr_array_free (demux->essence_tracks, TRUE);
4c3a81
   demux->essence_tracks = NULL;
4c3a81
 
4c3a81
   g_hash_table_destroy (demux->metadata);
4c3a81
@@ -4512,8 +4502,8 @@ gst_mxf_demux_init (GstMXFDemux * demux)
4c3a81
   g_rw_lock_init (&demux->metadata_lock);
4c3a81
 
4c3a81
   demux->src = g_ptr_array_new ();
4c3a81
-  demux->essence_tracks =
4c3a81
-      g_array_new (FALSE, FALSE, sizeof (GstMXFDemuxEssenceTrack));
4c3a81
+  demux->essence_tracks = g_ptr_array_new_with_free_func ((GDestroyNotify)
4c3a81
+      gst_mxf_demux_essence_track_free);
4c3a81
 
4c3a81
   gst_segment_init (&demux->segment, GST_FORMAT_TIME);
4c3a81
 
4c3a81
diff --git a/gst/mxf/mxfdemux.h b/gst/mxf/mxfdemux.h
4c3a81
index b4d7242a8..9b13488b0 100644
4c3a81
--- a/gst/mxf/mxfdemux.h
4c3a81
+++ b/gst/mxf/mxfdemux.h
4c3a81
@@ -160,7 +160,7 @@ struct _GstMXFDemux
4c3a81
   GList *partitions;
4c3a81
   GstMXFDemuxPartition *current_partition;
4c3a81
 
4c3a81
-  GArray *essence_tracks;
4c3a81
+  GPtrArray *essence_tracks;
4c3a81
 
4c3a81
   GList *pending_index_table_segments;
4c3a81
   GList *index_tables; /* one per BodySID / IndexSID */
4c3a81
-- 
4c3a81
2.43.0
4c3a81