From f90f3cde5f165daa99b9756eba4b98bc26aa1362 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Olivier=20Cr=C3=AAte?= Date: Mon, 19 Aug 2013 14:55:22 -0400 Subject: [PATCH] buffer: Fix gst_buffer_memcmp() where the buffer is smaller than size Also add unit tests for gst_buffer_memcmp https://bugzilla.gnome.org/show_bug.cgi?id=706162 --- gst/gstbuffer.c | 3 +++ tests/check/gst/gstbuffer.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/gst/gstbuffer.c b/gst/gstbuffer.c index ff43177..406e90b 100644 --- a/gst/gstbuffer.c +++ b/gst/gstbuffer.c @@ -1685,6 +1685,9 @@ gst_buffer_memcmp (GstBuffer * buffer, gsize offset, gconstpointer mem, "buffer %p, offset %" G_GSIZE_FORMAT ", size %" G_GSIZE_FORMAT, buffer, offset, size); + if (G_UNLIKELY (gst_buffer_get_size (buffer) < offset + size)) + return -1; + len = GST_BUFFER_MEM_LEN (buffer); for (i = 0; i < len && size > 0 && res == 0; i++) { diff --git a/tests/check/gst/gstbuffer.c b/tests/check/gst/gstbuffer.c index e3da42c..e52175f 100644 --- a/tests/check/gst/gstbuffer.c +++ b/tests/check/gst/gstbuffer.c @@ -294,6 +294,33 @@ GST_START_TEST (test_metadata_writable) GST_END_TEST; +GST_START_TEST (test_memcmp) +{ + GstBuffer *buffer; + char buf[3] = { 0, 0, 0 }; + + buffer = gst_buffer_new_and_alloc (2); + gst_buffer_memset (buffer, 0, 0, 2); + + fail_unless (gst_buffer_memcmp (buffer, 0, buf, 2) == 0); + fail_unless (gst_buffer_memcmp (buffer, 0, buf, 1) == 0); + fail_unless (gst_buffer_memcmp (buffer, 1, buf, 1) == 0); + fail_unless (gst_buffer_memcmp (buffer, 0, buf, 3) != 0); + fail_unless (gst_buffer_memcmp (buffer, 2, buf, 1) != 0); + fail_unless (gst_buffer_memcmp (buffer, 4, buf, 1) != 0); + + gst_buffer_memset (buffer, 0, 0x20, 2); + fail_unless (gst_buffer_memcmp (buffer, 0, buf, 2) != 0); + fail_unless (gst_buffer_memcmp (buffer, 0, buf, 1) != 0); + fail_unless (gst_buffer_memcmp (buffer, 1, buf, 1) != 0); + fail_unless (gst_buffer_memcmp (buffer, 0, buf, 3) != 0); + fail_unless (gst_buffer_memcmp (buffer, 2, buf, 1) != 0); + + gst_buffer_unref (buffer); +} + +GST_END_TEST; + GST_START_TEST (test_copy) { GstBuffer *buffer, *copy; @@ -842,6 +869,7 @@ gst_buffer_suite (void) tcase_add_test (tc_chain, test_make_writable); tcase_add_test (tc_chain, test_span); tcase_add_test (tc_chain, test_metadata_writable); + tcase_add_test (tc_chain, test_memcmp); tcase_add_test (tc_chain, test_copy); tcase_add_test (tc_chain, test_try_new_and_alloc); tcase_add_test (tc_chain, test_size); -- 2.7.4