Use g_memdup2() where available and add fallback for older GLib versions
authorDoug Nazar <nazard@nazar.ca>
Fri, 23 Apr 2021 16:12:58 +0000 (12:12 -0400)
committerGStreamer Marge Bot <gitlab-merge-bot@gstreamer-foundation.org>
Tue, 8 Jun 2021 07:59:05 +0000 (07:59 +0000)
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: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/803>

gst/glib-compat-private.h
gst/gstregistrychunks.c
libs/gst/base/gstbitwriter.c
libs/gst/base/gstbytereader.c
libs/gst/base/gstbytereader.h
libs/gst/base/gstbytewriter.c
libs/gst/base/gstindex.c
tests/check/libs/bitwriter.c
tests/check/libs/bytereader.c
tests/check/libs/bytewriter.c

index 8f37de2..617a50e 100644 (file)
@@ -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
 
index b352c45..80d0d4f 100644 (file)
@@ -40,6 +40,7 @@
 #include <gst/gstinfo.h>
 #include <gst/gstenumtypes.h>
 #include <gst/gstpadtemplate.h>
+#include "glib-compat-private.h"
 
 #include <gst/gstregistrychunks.h>
 
@@ -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
 
index 2f7f56b..0947a68 100644 (file)
@@ -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) {
index e9c2ec0..c0a16ef 100644 (file)
@@ -26,6 +26,7 @@
 #define GST_BYTE_READER_DISABLE_INLINES
 #include "gstbytereader.h"
 
+#include "gst/glib-compat-private.h"
 #include <string.h>
 
 /**
@@ -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; \
 }
index b988739..2130a8a 100644 (file)
@@ -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 */
index 335f22f..aec1093 100644 (file)
@@ -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);
 
index d54e0fe..964508e 100644 (file)
@@ -61,6 +61,7 @@
 #endif
 
 #include <gst/gst.h>
+#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);
index d6f68d3..4628a5b 100644 (file)
@@ -29,6 +29,7 @@
 #include <gst/check/gstcheck.h>
 #include <gst/base/gstbitwriter.h>
 #include <gst/base/gstbitreader.h>
+#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);
index 24a2971..bc4c406 100644 (file)
@@ -27,6 +27,7 @@
 #include <gst/gst.h>
 #include <gst/check/gstcheck.h>
 #include <gst/base/gstbytereader.h>
+#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,
index 89999af..c8017b8 100644 (file)
@@ -27,6 +27,7 @@
 #include <gst/gst.h>
 #include <gst/check/gstcheck.h>
 #include <gst/base/gstbytewriter.h>
+#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));