summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon McVittie <smcv@collabora.com>2022-10-24 11:58:23 +0100
committerSimon McVittie <smcv@collabora.com>2022-10-28 12:07:45 +0100
commit8a29325374fcc2dc847d65374d63b4401fc270a0 (patch)
tree41bdb391f2028209b5416dfb9bf8ee9506cb25d8
parente701578c6f4e621c7c3c688b5a04bcbf0a67d930 (diff)
downloadlibglnx-8a29325374fcc2dc847d65374d63b4401fc270a0.tar.gz
backports: Add a backport of g_memdup2()
g_memdup2() replaces g_memdup(), which is prone to integer overflow on 64-bit systems if copying a very large object with an attacker-controlled size. The original version in GLib is extern, but it seems simple enough to inline a backport. Related: https://gitlab.gnome.org/GNOME/glib/-/issues/2319 Signed-off-by: Simon McVittie <smcv@collabora.com>
-rw-r--r--glnx-backports.h34
1 files changed, 29 insertions, 5 deletions
diff --git a/glnx-backports.h b/glnx-backports.h
index afab392..f99f08f 100644
--- a/glnx-backports.h
+++ b/glnx-backports.h
@@ -1,8 +1,10 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
+ * Copyright 1998 Manish Singh
+ * Copyright 1998 Tim Janik
* Copyright (C) 2015 Colin Walters <walters@verbum.org>
* Copyright 2017 Emmanuele Bassi
- * SPDX-License-Identifier: LGPL-2.0-or-later
+ * SPDX-License-Identifier: LGPL-2.1-or-later
*
* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
@@ -10,7 +12,7 @@
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -18,13 +20,13 @@
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
+#include <string.h>
+
#include <gio/gio.h>
G_BEGIN_DECLS
@@ -85,6 +87,28 @@ gboolean glnx_set_object (GObject **object_ptr,
#define G_DBUS_METHOD_INVOCATION_UNHANDLED FALSE
#endif
+#if !GLIB_CHECK_VERSION(2, 68, 0)
+static inline gpointer _glnx_memdup2 (gconstpointer mem,
+ gsize byte_size) G_GNUC_ALLOC_SIZE(2);
+static inline gpointer
+_glnx_memdup2 (gconstpointer mem,
+ gsize byte_size)
+{
+ gpointer new_mem;
+
+ if (mem && byte_size != 0)
+ {
+ new_mem = g_malloc (byte_size);
+ memcpy (new_mem, mem, byte_size);
+ }
+ else
+ new_mem = NULL;
+
+ return new_mem;
+}
+#define g_memdup2 _glnx_memdup2
+#endif
+
#ifndef G_OPTION_ENTRY_NULL /* added in 2.70 */
#define G_OPTION_ENTRY_NULL { NULL, 0, 0, 0, NULL, NULL, NULL }
#endif