tests/qtest/fuzz-sdcard-test: Add reproducer for OSS-Fuzz (Issue 29225)
[qemu/rayw.git] / include / glib-compat.h
blob3113a7d2af84185ccc28ce7186008c91fa46f603
1 /*
2 * GLIB Compatibility Functions
4 * Copyright IBM, Corp. 2013
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 * Michael Tokarev <mjt@tls.msk.ru>
9 * Paolo Bonzini <pbonzini@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
16 #ifndef QEMU_GLIB_COMPAT_H
17 #define QEMU_GLIB_COMPAT_H
19 /* Ask for warnings for anything that was marked deprecated in
20 * the defined version, or before. It is a candidate for rewrite.
22 #define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_56
24 /* Ask for warnings if code tries to use function that did not
25 * exist in the defined version. These risk breaking builds
27 #define GLIB_VERSION_MAX_ALLOWED GLIB_VERSION_2_56
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
32 #include <glib.h>
33 #if defined(G_OS_UNIX)
34 #include <glib-unix.h>
35 #include <sys/types.h>
36 #include <pwd.h>
37 #endif
40 * Note that because of the GLIB_VERSION_MAX_ALLOWED constant above, allowing
41 * use of functions from newer GLib via this compat header needs a little
42 * trickery to prevent warnings being emitted.
44 * Consider a function from newer glib-X.Y that we want to use
46 * int g_foo(const char *wibble)
48 * We must define a static inline function with the same signature that does
49 * what we need, but with a "_compat" suffix e.g.
51 * static inline void g_foo_compat(const char *wibble)
52 * {
53 * #if GLIB_CHECK_VERSION(X, Y, 0)
54 * g_foo(wibble)
55 * #else
56 * g_something_equivalent_in_older_glib(wibble);
57 * #endif
58 * }
60 * The #pragma at the top of this file turns off -Wdeprecated-declarations,
61 * ensuring this wrapper function impl doesn't trigger the compiler warning
62 * about using too new glib APIs. Finally we can do
64 * #define g_foo(a) g_foo_compat(a)
66 * So now the code elsewhere in QEMU, which *does* have the
67 * -Wdeprecated-declarations warning active, can call g_foo(...) as normal,
68 * without generating warnings.
72 * g_memdup2_qemu:
73 * @mem: (nullable): the memory to copy.
74 * @byte_size: the number of bytes to copy.
76 * Allocates @byte_size bytes of memory, and copies @byte_size bytes into it
77 * from @mem. If @mem is %NULL it returns %NULL.
79 * This replaces g_memdup(), which was prone to integer overflows when
80 * converting the argument from a #gsize to a #guint.
82 * This static inline version is a backport of the new public API from
83 * GLib 2.68, kept internal to GLib for backport to older stable releases.
84 * See https://gitlab.gnome.org/GNOME/glib/-/issues/2319.
86 * Returns: (nullable): a pointer to the newly-allocated copy of the memory,
87 * or %NULL if @mem is %NULL.
89 static inline gpointer g_memdup2_qemu(gconstpointer mem, gsize byte_size)
91 #if GLIB_CHECK_VERSION(2, 68, 0)
92 return g_memdup2(mem, byte_size);
93 #else
94 gpointer new_mem;
96 if (mem && byte_size != 0) {
97 new_mem = g_malloc(byte_size);
98 memcpy(new_mem, mem, byte_size);
99 } else {
100 new_mem = NULL;
103 return new_mem;
104 #endif
106 #define g_memdup2(m, s) g_memdup2_qemu(m, s)
108 #if defined(G_OS_UNIX)
110 * Note: The fallback implementation is not MT-safe, and it returns a copy of
111 * the libc passwd (must be g_free() after use) but not the content. Because of
112 * these important differences the caller must be aware of, it's not #define for
113 * GLib API substitution.
115 static inline struct passwd *
116 g_unix_get_passwd_entry_qemu(const gchar *user_name, GError **error)
118 #if GLIB_CHECK_VERSION(2, 64, 0)
119 return g_unix_get_passwd_entry(user_name, error);
120 #else
121 struct passwd *p = getpwnam(user_name);
122 if (!p) {
123 g_set_error_literal(error, G_UNIX_ERROR, 0, g_strerror(errno));
124 return NULL;
126 return (struct passwd *)g_memdup(p, sizeof(*p));
127 #endif
129 #endif /* G_OS_UNIX */
131 static inline bool
132 qemu_g_test_slow(void)
134 static int cached = -1;
135 if (cached == -1) {
136 cached = g_test_slow() || getenv("G_TEST_SLOW") != NULL;
138 return cached;
141 #undef g_test_slow
142 #undef g_test_thorough
143 #undef g_test_quick
144 #define g_test_slow() qemu_g_test_slow()
145 #define g_test_thorough() qemu_g_test_slow()
146 #define g_test_quick() (!qemu_g_test_slow())
148 #pragma GCC diagnostic pop
150 #endif