From e14996eb9c0332aeda6390db207380dd0267e7c2 Mon Sep 17 00:00:00 2001
From: Hugues Fruchet <hugues.fruchet@st.com>
Date: Wed, 25 Sep 2019 16:01:49 +0200
Subject: [PATCH 13/14] waylandsink: silently drop erroneous frame

Do not stop playback when erroneous frame is received at show_frame():
- test file descriptors validity, to not let throw erroneous
file descriptor to wayland backend (which may crash on fatal error)
- if file descriptors are not valid, do not fall into general
"GST_ELEMENT_ERROR" nor return FLOW_ERROR.

Signed-off-by: Hugues Fruchet <hugues.fruchet@st.com>
---
 ext/wayland/gstwaylandsink.c |  6 +++---
 ext/wayland/wllinuxdmabuf.c  | 14 ++++++++++++++
 ext/wayland/wlshmallocator.c | 10 ++++++++++
 3 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/ext/wayland/gstwaylandsink.c b/ext/wayland/gstwaylandsink.c
index e4952cb..cf355d3 100644
--- a/ext/wayland/gstwaylandsink.c
+++ b/ext/wayland/gstwaylandsink.c
@@ -1025,9 +1025,9 @@ activate_failed:
   }
 src_map_failed:
   {
-    GST_ELEMENT_ERROR (sink, RESOURCE, READ,
-        ("Video memory can not be read from userspace."), (NULL));
-    ret = GST_FLOW_ERROR;
+    /* Silently drop in case of erroneous buffer */
+    GST_ERROR_OBJECT (sink, "Video memory can not be read from userspace,"
+                      "dropping buffer %p", buffer);
     goto done;
   }
 dst_map_failed:
diff --git a/ext/wayland/wllinuxdmabuf.c b/ext/wayland/wllinuxdmabuf.c
index 96487d1..36815b5 100644
--- a/ext/wayland/wllinuxdmabuf.c
+++ b/ext/wayland/wllinuxdmabuf.c
@@ -28,6 +28,8 @@
 #include "wllinuxdmabuf.h"
 #include "wlvideoformat.h"
 
+#include <unistd.h>
+
 GST_DEBUG_CATEGORY_EXTERN (gstwayland_debug);
 #define GST_CAT_DEFAULT gstwayland_debug
 
@@ -110,6 +112,18 @@ gst_wl_linux_dmabuf_construct_wl_buffer (GstBuffer * buf,
     if (gst_buffer_find_memory (buf, offset, 1, &mem_idx, &length, &skip)) {
       GstMemory *m = gst_buffer_peek_memory (buf, mem_idx);
       gint fd = gst_dmabuf_memory_get_fd (m);
+      gint dup_fd = dup (fd);
+
+      /* Test if file descriptor is valid */
+      if (dup_fd < 0) {
+        GST_ERROR_OBJECT (display, "zwp_linux_dmabuf: dup failed for fd=%d (err=%d) buffer=%p",
+                fd, dup_fd, buf);
+        zwp_linux_buffer_params_v1_destroy (params);
+        data.wbuf = NULL;
+        goto out;
+      }
+      close(dup_fd);
+
       zwp_linux_buffer_params_v1_add (params, fd, i, m->offset + skip,
           stride, 0, 0);
     } else {
diff --git a/ext/wayland/wlshmallocator.c b/ext/wayland/wlshmallocator.c
index 0a82a35..8659c2f 100644
--- a/ext/wayland/wlshmallocator.c
+++ b/ext/wayland/wlshmallocator.c
@@ -212,12 +212,22 @@ gst_wl_shm_memory_construct_wl_buffer (GstMemory * mem, GstWlDisplay * display,
   enum wl_shm_format format;
   struct wl_shm_pool *wl_pool;
   struct wl_buffer *wbuffer;
+  gint fd = gst_fd_memory_get_fd (mem);
+  gint dup_fd = dup(fd);
 
   if (!gst_wl_shm_validate_video_info (info)) {
     GST_DEBUG_OBJECT (display, "Unsupported strides and offsets.");
     return NULL;
   }
 
+  /* Test if file descriptor is valid */
+  if (dup_fd < 0) {
+    GST_ERROR_OBJECT (display, "wl_shm_memory: dup failed for fd=%d (err=%d) mem=%p",
+        fd, dup_fd, mem);
+    return NULL;
+  }
+  close(dup_fd);
+
   width = GST_VIDEO_INFO_WIDTH (info);
   height = GST_VIDEO_INFO_HEIGHT (info);
   stride = GST_VIDEO_INFO_PLANE_STRIDE (info, 0);
-- 
2.25.1

