[0.11] gstreamer: memory: add more checks

Wim Taymans wtay at kemper.freedesktop.org
Thu Jan 5 09:15:55 PST 2012


Module: gstreamer
Branch: 0.11
Commit: d483ff2db5c597f7db72e3772bafc0bc2de8debe
URL:    http://cgit.freedesktop.org/gstreamer/gstreamer/commit/?id=d483ff2db5c597f7db72e3772bafc0bc2de8debe

Author: Wim Taymans <wim.taymans at collabora.co.uk>
Date:   Thu Jan  5 18:15:20 2012 +0100

memory: add more checks

Add check for mapping and resizing

---

 gst/gstmemory.c             |   10 ++++++++++
 tests/check/gst/gstmemory.c |   26 ++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/gst/gstmemory.c b/gst/gstmemory.c
index 0791fb8..1cdbf26 100644
--- a/gst/gstmemory.c
+++ b/gst/gstmemory.c
@@ -214,10 +214,20 @@ _default_mem_map (GstMemoryDefault * mem, gsize * size, gsize * maxsize,
 static gboolean
 _default_mem_unmap (GstMemoryDefault * mem, gpointer data, gsize size)
 {
+  g_return_val_if_fail ((guint8 *) data >= mem->data
+      && (guint8 *) data < mem->data + mem->maxsize, FALSE);
+
   if (size != -1) {
+    /* check if resize happened or unmap was called with different data */
+    if (mem->data + mem->offset != data) {
+      /* adjust the size */
+      size = (guint8 *) data - mem->data + size - mem->offset;
+    }
+
     g_return_val_if_fail (mem->offset + size <= mem->maxsize, FALSE);
     mem->size = size;
   }
+
   return TRUE;
 }
 
diff --git a/tests/check/gst/gstmemory.c b/tests/check/gst/gstmemory.c
index b903a51..79e27c2 100644
--- a/tests/check/gst/gstmemory.c
+++ b/tests/check/gst/gstmemory.c
@@ -387,6 +387,7 @@ GST_START_TEST (test_map)
   fail_unless (data != NULL);
   fail_unless (size == 100);
   fail_unless (maxsize == maxalloc);
+  ASSERT_CRITICAL (gst_memory_unmap (mem, (guint8 *) data - 1, maxsize + 1));
   gst_memory_unmap (mem, data, size);
 
   /* make smaller by unmapping less */
@@ -475,6 +476,30 @@ GST_START_TEST (test_map_nested)
 
 GST_END_TEST;
 
+GST_START_TEST (test_map_resize)
+{
+  GstMemory *mem;
+  gsize size, maxsize;
+  gpointer data;
+
+  mem = gst_allocator_alloc (NULL, 100, 0);
+
+  /* do mapping */
+  data = gst_memory_map (mem, &size, &maxsize, GST_MAP_READ);
+  fail_unless (data != NULL);
+  fail_unless (size == 100);
+
+  /* resize the buffer */
+  gst_memory_resize (mem, 1, maxsize - 1);
+
+  /* unmap the buffer with original pointer and size */
+  gst_memory_unmap (mem, data, maxsize);
+
+  gst_memory_unref (mem);
+}
+
+GST_END_TEST;
+
 
 static Suite *
 gst_memory_suite (void)
@@ -492,6 +517,7 @@ gst_memory_suite (void)
   tcase_add_test (tc_chain, test_resize);
   tcase_add_test (tc_chain, test_map);
   tcase_add_test (tc_chain, test_map_nested);
+  tcase_add_test (tc_chain, test_map_resize);
 
   return s;
 }



More information about the gstreamer-commits mailing list