From 0261df973b8af0c7201e63145b8a0283c6153f36 Mon Sep 17 00:00:00 2001 From: Eduardo Habkost Date: Sun, 11 Mar 2018 14:29:19 -0400 Subject: [PATCH] machine: Ensure all TYPE_MACHINE subclasses have the right suffix Now that all non-abstract TYPE_MACHINE subclasses have the -machine suffix, add an assert to ensure this will be always true. Backports commit dcb3d601115eed77aef543fe3a920adc17544e06 from qemu --- qemu/glib_compat.c | 60 ++++++++++++++++++++++++++++++++++++++ qemu/hw/core/machine.c | 10 ++++++- qemu/include/glib_compat.h | 4 ++- 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/qemu/glib_compat.c b/qemu/glib_compat.c index c5300d84..999ef6f8 100644 --- a/qemu/glib_compat.c +++ b/qemu/glib_compat.c @@ -94,6 +94,66 @@ gboolean g_str_equal(gconstpointer v1, gconstpointer v2) return strcmp((const char*)v1, (const char*)v2) == 0; } +/** + * g_str_has_suffix: + * @str: a nul-terminated string. + * @suffix: the nul-terminated suffix to look for. + * + * Looks whether the string @str ends with @suffix. + * + * Return value: %TRUE if @str end with @suffix, %FALSE otherwise. + * + * Since: 2.2 + **/ +gboolean +g_str_has_suffix(const gchar *str, const gchar *suffix) +{ + int str_len; + int suffix_len; + + if (str == NULL || suffix == NULL) { + return FALSE; + } + + str_len = strlen (str); + suffix_len = strlen (suffix); + + if (str_len < suffix_len) + return FALSE; + + return strcmp (str + str_len - suffix_len, suffix) == 0; +} + +/** + * g_str_has_prefix: + * @str: a nul-terminated string. + * @prefix: the nul-terminated prefix to look for. + * + * Looks whether the string @str begins with @prefix. + * + * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise. + * + * Since: 2.2 + **/ +gboolean +g_str_has_prefix(const gchar *str, const gchar *prefix) +{ + int str_len; + int prefix_len; + + if (str == NULL || prefix == NULL) { + return FALSE; + } + + str_len = strlen (str); + prefix_len = strlen (prefix); + + if (str_len < prefix_len) + return FALSE; + + return strncmp (str, prefix, prefix_len) == 0; +} + // g_int_hash() is lifted from glib-2.28.0/glib/gutils.c /** * g_int_hash: diff --git a/qemu/hw/core/machine.c b/qemu/hw/core/machine.c index d8154725..996b931f 100644 --- a/qemu/hw/core/machine.c +++ b/qemu/hw/core/machine.c @@ -15,6 +15,14 @@ #include "qapi/error.h" #include "qemu/cutils.h" +static void machine_class_base_init(ObjectClass *oc, void *data) +{ + if (!object_class_is_abstract(oc)) { + const char *cname = object_class_get_name(oc); + assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX)); + } +} + static void machine_initfn(struct uc_struct *uc, Object *obj, void *opaque) { } @@ -38,7 +46,7 @@ static const TypeInfo machine_info = { NULL, NULL, - NULL, + machine_class_base_init, NULL, true, diff --git a/qemu/include/glib_compat.h b/qemu/include/glib_compat.h index f82229b3..72b4ddf9 100644 --- a/qemu/include/glib_compat.h +++ b/qemu/include/glib_compat.h @@ -59,8 +59,10 @@ typedef void (*GDestroyNotify)(gpointer data); guint g_str_hash(gconstpointer v); gboolean g_str_equal(gconstpointer v1, gconstpointer v2); -guint g_int_hash(gconstpointer v); +gboolean g_str_has_suffix(const gchar *str, const gchar *prefix); +gboolean g_str_has_prefix(const gchar *str, const gchar *prefix); +guint g_int_hash(gconstpointer v); gboolean g_int_equal(gconstpointer v1, gconstpointer v2); typedef struct _GList {