From d483ff2db5c597f7db72e3772bafc0bc2de8debe Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 5 Jan 2012 18:15:20 +0100 Subject: [PATCH] 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(+) 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; } -- 2.7.4