From b16e96dd878e0c5e7baeb8fad62ca43de1f66982 Mon Sep 17 00:00:00 2001 From: Doug Nazar Date: Fri, 23 Apr 2021 12:12:58 -0400 Subject: [PATCH] Use g_memdup2() where available and add fallback for older GLib versions glib 2.68 deprecates g_memdup(). Replace with g_memdup2() and add fallback if compiling against older versions, since we want to avoid deprecation warnings. Part-of: --- gst/glib-compat-private.h | 3 +++ gst/gstregistrychunks.c | 3 ++- libs/gst/base/gstbitwriter.c | 6 ++++-- libs/gst/base/gstbytereader.c | 3 ++- libs/gst/base/gstbytereader.h | 5 ++++- libs/gst/base/gstbytewriter.c | 4 +++- libs/gst/base/gstindex.c | 3 ++- tests/check/libs/bitwriter.c | 3 ++- tests/check/libs/bytereader.c | 3 ++- tests/check/libs/bytewriter.c | 5 +++-- 10 files changed, 27 insertions(+), 11 deletions(-) diff --git a/gst/glib-compat-private.h b/gst/glib-compat-private.h index 8f37de2..617a50e 100644 --- a/gst/glib-compat-private.h +++ b/gst/glib-compat-private.h @@ -30,6 +30,9 @@ G_BEGIN_DECLS /* copies */ /* adaptations */ +#if !GLIB_CHECK_VERSION(2, 67, 4) +#define g_memdup2(ptr,sz) ((G_LIKELY(((guint64)(sz)) < G_MAXUINT)) ? g_memdup(ptr,sz) : (g_abort(),NULL)) +#endif G_END_DECLS diff --git a/gst/gstregistrychunks.c b/gst/gstregistrychunks.c index b352c45..80d0d4f 100644 --- a/gst/gstregistrychunks.c +++ b/gst/gstregistrychunks.c @@ -40,6 +40,7 @@ #include #include #include +#include "glib-compat-private.h" #include @@ -95,7 +96,7 @@ _strnlen (const gchar * str, gint maxlen) gint _len = _strnlen (inptr, (endptr-inptr)); \ if (_len == -1) \ goto error_label; \ - outptr = g_memdup ((gconstpointer)inptr, _len + 1); \ + outptr = g_memdup2 ((gconstpointer)inptr, _len + 1); \ inptr += _len + 1; \ }G_STMT_END diff --git a/libs/gst/base/gstbitwriter.c b/libs/gst/base/gstbitwriter.c index 2f7f56b..0947a68 100644 --- a/libs/gst/base/gstbitwriter.c +++ b/libs/gst/base/gstbitwriter.c @@ -27,6 +27,8 @@ #define GST_BIT_WRITER_DISABLE_INLINES #include "gstbitwriter.h" +#include "gst/glib-compat-private.h" + /** * SECTION:gstbitwriter * @title: GstBitWriter @@ -200,7 +202,7 @@ gst_bit_writer_reset_and_get_data (GstBitWriter * bitwriter) data = bitwriter->data; if (bitwriter->owned) - data = g_memdup (data, bitwriter->bit_size >> 3); + data = g_memdup2 (data, bitwriter->bit_size >> 3); gst_bit_writer_reset (bitwriter); return data; @@ -232,7 +234,7 @@ gst_bit_writer_reset_and_get_buffer (GstBitWriter * bitwriter) /* we cannot rely on buffers allocated externally, thus let's dup * the data */ if (data && !bitwriter->owned) - data = g_memdup (data, size); + data = g_memdup2 (data, size); buffer = gst_buffer_new (); if (data != NULL) { diff --git a/libs/gst/base/gstbytereader.c b/libs/gst/base/gstbytereader.c index e9c2ec0..c0a16ef 100644 --- a/libs/gst/base/gstbytereader.c +++ b/libs/gst/base/gstbytereader.c @@ -26,6 +26,7 @@ #define GST_BYTE_READER_DISABLE_INLINES #include "gstbytereader.h" +#include "gst/glib-compat-private.h" #include /** @@ -1222,7 +1223,7 @@ gst_byte_reader_dup_string_utf##bits (GstByteReader * reader, type ** str) \ *str = NULL; \ return FALSE; \ } \ - *str = g_memdup (reader->data + reader->byte, size); \ + *str = g_memdup2 (reader->data + reader->byte, size); \ reader->byte += size; \ return TRUE; \ } diff --git a/libs/gst/base/gstbytereader.h b/libs/gst/base/gstbytereader.h index b988739..2130a8a 100644 --- a/libs/gst/base/gstbytereader.h +++ b/libs/gst/base/gstbytereader.h @@ -362,7 +362,10 @@ static inline guint8 * gst_byte_reader_dup_data_unchecked (GstByteReader * reader, guint size) { gconstpointer data = gst_byte_reader_get_data_unchecked (reader, size); - return (guint8 *) g_memdup (data, size); + guint8 *dup_data = (guint8 *) g_malloc (size); + + memcpy (dup_data, data, size); + return dup_data; } /* Unchecked variants that should not be used */ diff --git a/libs/gst/base/gstbytewriter.c b/libs/gst/base/gstbytewriter.c index 335f22f..aec1093 100644 --- a/libs/gst/base/gstbytewriter.c +++ b/libs/gst/base/gstbytewriter.c @@ -25,6 +25,8 @@ #define GST_BYTE_WRITER_DISABLE_INLINES #include "gstbytewriter.h" +#include "gst/glib-compat-private.h" + /** * SECTION:gstbytewriter * @title: GstByteWriter @@ -236,7 +238,7 @@ gst_byte_writer_reset_and_get_data (GstByteWriter * writer) data = (guint8 *) writer->parent.data; if (!writer->owned) - data = g_memdup (data, writer->parent.size); + data = g_memdup2 (data, writer->parent.size); writer->parent.data = NULL; gst_byte_writer_reset (writer); diff --git a/libs/gst/base/gstindex.c b/libs/gst/base/gstindex.c index d54e0fe..964508e 100644 --- a/libs/gst/base/gstindex.c +++ b/libs/gst/base/gstindex.c @@ -61,6 +61,7 @@ #endif #include +#include "gst/glib-compat-private.h" /* Index signals and args */ enum @@ -798,7 +799,7 @@ gst_index_add_associationv (GstIndex * index, gint id, entry->type = GST_INDEX_ENTRY_ASSOCIATION; entry->id = id; entry->data.assoc.flags = flags; - entry->data.assoc.assocs = g_memdup (list, sizeof (GstIndexAssociation) * n); + entry->data.assoc.assocs = g_memdup2 (list, sizeof (GstIndexAssociation) * n); entry->data.assoc.nassocs = n; gst_index_add_entry (index, entry); diff --git a/tests/check/libs/bitwriter.c b/tests/check/libs/bitwriter.c index d6f68d3..4628a5b 100644 --- a/tests/check/libs/bitwriter.c +++ b/tests/check/libs/bitwriter.c @@ -29,6 +29,7 @@ #include #include #include +#include "gst/glib-compat-private.h" GST_START_TEST (test_initialization) { @@ -76,7 +77,7 @@ GST_START_TEST (test_data) fail_unless (gst_bit_writer_put_bits_uint64 (&writer, 0x45, 48)); fail_unless_equals_int (gst_bit_writer_get_remaining (&writer), 2048 - 71); fail_unless (gst_bit_writer_align_bytes (&writer, 0)); - data = g_memdup (sdata, sizeof (sdata)); + data = g_memdup2 (sdata, sizeof (sdata)); fail_unless (gst_bit_writer_put_bytes (&writer, data, sizeof (sdata))); gst_bit_reader_init (&reader, gst_bit_writer_get_data (&writer), 256); diff --git a/tests/check/libs/bytereader.c b/tests/check/libs/bytereader.c index 24a2971..bc4c406 100644 --- a/tests/check/libs/bytereader.c +++ b/tests/check/libs/bytereader.c @@ -27,6 +27,7 @@ #include #include #include +#include "gst/glib-compat-private.h" #ifndef fail_unless_equals_int64 #define fail_unless_equals_int64(a, b) \ @@ -574,7 +575,7 @@ GST_START_TEST (test_scan) gint found; /* dup so valgrind can detect out of bounds access more easily */ - m = g_memdup (sync_data, sizeof (sync_data)); + m = g_memdup2 (sync_data, sizeof (sync_data)); gst_byte_reader_init (&reader, m, sizeof (sync_data)); found = gst_byte_reader_masked_scan_uint32_peek (&reader, 0xffffff00, diff --git a/tests/check/libs/bytewriter.c b/tests/check/libs/bytewriter.c index 89999af..c8017b8 100644 --- a/tests/check/libs/bytewriter.c +++ b/tests/check/libs/bytewriter.c @@ -27,6 +27,7 @@ #include #include #include +#include "gst/glib-compat-private.h" GST_START_TEST (test_initialization) { @@ -42,7 +43,7 @@ GST_START_TEST (test_initialization) (&writer)), 0); gst_byte_writer_reset (&writer); - data = g_memdup (sdata, sizeof (sdata)); + data = g_memdup2 (sdata, sizeof (sdata)); gst_byte_writer_init_with_data (&writer, data, sizeof (sdata), FALSE); fail_unless_equals_int (gst_byte_writer_get_pos (&writer), 0); fail_unless_equals_int (gst_byte_writer_get_size (&writer), 0); @@ -56,7 +57,7 @@ GST_START_TEST (test_initialization) g_free (data); data = tmp = NULL; - data = g_memdup (sdata, sizeof (sdata)); + data = g_memdup2 (sdata, sizeof (sdata)); gst_byte_writer_init_with_data (&writer, data, sizeof (sdata), TRUE); fail_unless_equals_int (gst_byte_writer_get_pos (&writer), 0); fail_unless_equals_int (gst_byte_writer_get_size (&writer), sizeof (sdata)); -- 2.7.4