mdns: Fix a crash on context error
authorJan Schmidt <jan@centricular.com>
Fri, 18 Aug 2023 08:21:18 +0000 (18:21 +1000)
committerTim-Philipp Müller <tim@centricular.com>
Sun, 20 Aug 2023 16:30:11 +0000 (17:30 +0100)
Make sure not to free the microdns provider context until the
device provider asks it to stop. Fixes a crash if there is
an error (such as MDNS port being busy) that makes the
mdns listener exit early.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/5207>

subprojects/gst-plugins-bad/ext/mdns/gstmicrodnsdevice.c

index 180cd82cb1711140691e8234bdf6bf9c4ed50bed..ded71d4d581cc71331be15eddf84313634705794 100644 (file)
@@ -44,6 +44,7 @@ struct _GstMDNSDeviceProvider
 struct _ListenerContext
 {
   GMutex lock;
+  GCond stop_cond;
   GstDeviceProvider *provider;
 
   /* The following fields are protected by @lock */
@@ -362,9 +363,18 @@ _listen (ListenerContext * ctx)
 done:
   GST_INFO_OBJECT (ctx->provider, "Done listening");
 
+  /* Wait until we're told to stop, or gst_mdns_device_provider_stop()
+     can access a freed context */
+  g_mutex_lock (&ctx->lock);
+  while (!ctx->stop) {
+    g_cond_wait (&ctx->stop_cond, &ctx->lock);
+  }
+  g_mutex_unlock (&ctx->lock);
+
   g_sequence_free (ctx->last_seen_devices);
   g_hash_table_unref (ctx->devices);
   g_mutex_clear (&ctx->lock);
+  g_cond_clear (&ctx->stop_cond);
   g_free (ctx);
 
   return NULL;
@@ -385,6 +395,7 @@ gst_mdns_device_provider_start (GstDeviceProvider * provider)
   ListenerContext *ctx = g_new0 (ListenerContext, 1);
 
   g_mutex_init (&ctx->lock);
+  g_cond_init (&ctx->stop_cond);
   ctx->provider = provider;
   ctx->devices =
       g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
@@ -405,6 +416,7 @@ gst_mdns_device_provider_stop (GstDeviceProvider * provider)
 
   g_mutex_lock (&self->current_ctx->lock);
   self->current_ctx->stop = true;
+  g_cond_broadcast (&self->current_ctx->stop_cond);
   g_mutex_unlock (&self->current_ctx->lock);
 
   self->current_ctx = NULL;