From fa9bb7929eb4966b139eef3291aaedd1638eacb1 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Wed, 5 Jul 2006 17:09:18 +0000 Subject: [PATCH] No point in checking if the size of the subbuffer > 0, the code handles it correclty as demonstrated by unit test. Original commit message from CVS: * gst/gstbuffer.c: (_gst_buffer_copy), (gst_buffer_create_sub): * tests/check/gst/gstbuffer.c: (GST_START_TEST), (gst_buffer_suite): No point in checking if the size of the subbuffer > 0, the code handles it correclty as demonstrated by unit test. Also add a unit test for the zero sized _new_and_alloc and _copy. Fixes #346663. --- ChangeLog | 10 ++++++++++ gst/gstbuffer.c | 7 ++++--- tests/check/gst/gstbuffer.c | 48 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8fa55f1..4aaddbf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,15 @@ 2006-07-05 Wim Taymans + * gst/gstbuffer.c: (_gst_buffer_copy), (gst_buffer_create_sub): + * tests/check/gst/gstbuffer.c: (GST_START_TEST), + (gst_buffer_suite): + No point in checking if the size of the subbuffer > 0, the + code handles it correclty as demonstrated by unit test. + Also add a unit test for the zero sized _new_and_alloc and + _copy. Fixes #346663. + +2006-07-05 Wim Taymans + * libs/gst/base/gstbasetransform.c: (gst_base_transform_prepare_output_buffer), (gst_base_transform_buffer_alloc), diff --git a/gst/gstbuffer.c b/gst/gstbuffer.c index 6f01359..5bfe673 100644 --- a/gst/gstbuffer.c +++ b/gst/gstbuffer.c @@ -210,7 +210,7 @@ _gst_buffer_copy (GstBuffer * buffer) /* we simply copy everything from our parent */ copy->data = g_memdup (buffer->data, buffer->size); /* make sure it gets freed (even if the parent is subclassed, we return a - normal buffer */ + normal buffer) */ copy->malloc_data = copy->data; copy->size = buffer->size; @@ -270,6 +270,8 @@ gst_buffer_new (void) * Creates a newly allocated buffer with data of the given size. * The buffer memory is not cleared. * + * Note that when @size == 0, the buffer data pointer will be NULL. + * * MT safe. * Returns: the new #GstBuffer. */ @@ -466,7 +468,7 @@ gst_subbuffer_init (GTypeInstance * instance, gpointer g_class) * @parent: a #GstBuffer. * @offset: the offset into parent #GstBuffer at which the new sub-buffer * begins. - * @size: the size of the new #GstBuffer sub-buffer, in bytes (with size > 0). + * @size: the size of the new #GstBuffer sub-buffer, in bytes. * * Creates a sub-buffer from @parent at @offset and @size. * This sub-buffer uses the actual memory space of the parent buffer. @@ -486,7 +488,6 @@ gst_buffer_create_sub (GstBuffer * buffer, guint offset, guint size) g_return_val_if_fail (buffer != NULL, NULL); g_return_val_if_fail (buffer->mini_object.refcount > 0, NULL); - g_return_val_if_fail (size > 0, NULL); g_return_val_if_fail (buffer->size >= offset + size, NULL); /* find real parent */ diff --git a/tests/check/gst/gstbuffer.c b/tests/check/gst/gstbuffer.c index dcf321e..105d0d7 100644 --- a/tests/check/gst/gstbuffer.c +++ b/tests/check/gst/gstbuffer.c @@ -81,6 +81,17 @@ GST_START_TEST (test_subbuffer) ASSERT_BUFFER_REFCOUNT (buffer, "parent", 2); ASSERT_BUFFER_REFCOUNT (sub, "subbuffer", 1); + gst_buffer_unref (sub); + + /* create a subbuffer of size 0 */ + sub = gst_buffer_create_sub (buffer, 1, 0); + fail_if (sub == NULL, "create_sub of buffer returned NULL"); + fail_unless (GST_BUFFER_SIZE (sub) == 0, "subbuffer has wrong size"); + fail_unless (memcmp (GST_BUFFER_DATA (buffer) + 1, GST_BUFFER_DATA (sub), + 0) == 0, "subbuffer contains the wrong data"); + ASSERT_BUFFER_REFCOUNT (buffer, "parent", 2); + ASSERT_BUFFER_REFCOUNT (sub, "subbuffer", 1); + /* clean up */ gst_buffer_unref (sub); gst_buffer_unref (buffer); @@ -186,7 +197,7 @@ GST_START_TEST (test_span) gst_buffer_unref (span); ASSERT_BUFFER_REFCOUNT (buffer, "parent", 3); -/* clean up */ + /* clean up */ gst_buffer_unref (sub1); gst_buffer_unref (sub2); gst_buffer_unref (buffer); @@ -321,6 +332,39 @@ GST_START_TEST (test_metadata_writable) GST_END_TEST; +GST_START_TEST (test_copy) +{ + GstBuffer *buffer, *copy; + + buffer = gst_buffer_new_and_alloc (4); + ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1); + + copy = gst_buffer_copy (buffer); + ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1); + ASSERT_BUFFER_REFCOUNT (copy, "copy", 1); + /* data must be copied and thus point to different memory */ + fail_if (GST_BUFFER_DATA (buffer) == GST_BUFFER_DATA (copy)); + + gst_buffer_unref (copy); + gst_buffer_unref (buffer); + + /* a 0-sized buffer has NULL data as per docs */ + buffer = gst_buffer_new_and_alloc (0); + fail_unless (GST_BUFFER_DATA (buffer) == NULL); + fail_unless (GST_BUFFER_SIZE (buffer) == 0); + + /* copying a 0-sized buffer should not crash and also set + * the data member NULL. */ + copy = gst_buffer_copy (buffer); + fail_unless (GST_BUFFER_DATA (copy) == NULL); + fail_unless (GST_BUFFER_SIZE (copy) == 0); + + gst_buffer_unref (copy); + gst_buffer_unref (buffer); +} + +GST_END_TEST; + Suite * gst_buffer_suite (void) { @@ -335,6 +379,8 @@ gst_buffer_suite (void) tcase_add_test (tc_chain, test_is_span_fast); tcase_add_test (tc_chain, test_span); tcase_add_test (tc_chain, test_metadata_writable); + tcase_add_test (tc_chain, test_copy); + return s; } -- 2.7.4