[libdazzle] shortcuts: add more strict type checks
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libdazzle] shortcuts: add more strict type checks
- Date: Thu, 26 Jul 2018 04:40:25 +0000 (UTC)
commit a7f44855515fcf07b88cf6624fe2e2546ef723c4
Author: Christian Hergert <chergert redhat com>
Date: Wed Jul 25 21:40:04 2018 -0700
shortcuts: add more strict type checks
src/shortcuts/dzl-shortcut-chord.c | 48 ++++++++++++++++++++++++++++----------
1 file changed, 36 insertions(+), 12 deletions(-)
---
diff --git a/src/shortcuts/dzl-shortcut-chord.c b/src/shortcuts/dzl-shortcut-chord.c
index c7d65f7..8e14fe7 100644
--- a/src/shortcuts/dzl-shortcut-chord.c
+++ b/src/shortcuts/dzl-shortcut-chord.c
@@ -28,6 +28,7 @@
#include "util/dzl-macros.h"
#define MAX_CHORD_SIZE 4
+#define CHORD_MAGIC 0x83316672
G_DEFINE_BOXED_TYPE (DzlShortcutChord, dzl_shortcut_chord,
dzl_shortcut_chord_copy, dzl_shortcut_chord_free)
@@ -42,6 +43,7 @@ typedef struct
struct _DzlShortcutChord
{
DzlShortcutKey keys[MAX_CHORD_SIZE];
+ guint magic;
};
typedef struct
@@ -58,6 +60,12 @@ struct _DzlShortcutChordTable
guint size;
};
+static inline gboolean
+IS_SHORTCUT_CHORD (const DzlShortcutChord *chord)
+{
+ return chord != NULL && chord->magic == CHORD_MAGIC;
+}
+
static GdkModifierType
sanitize_modifier_mask (GdkModifierType mods)
{
@@ -71,13 +79,16 @@ static gint
dzl_shortcut_chord_compare (const DzlShortcutChord *a,
const DzlShortcutChord *b)
{
+ g_assert (IS_SHORTCUT_CHORD (a));
+ g_assert (IS_SHORTCUT_CHORD (b));
+
return memcmp (a, b, sizeof *a);
}
static gboolean
dzl_shortcut_chord_is_valid (DzlShortcutChord *self)
{
- g_assert (self != NULL);
+ g_assert (IS_SHORTCUT_CHORD (self));
/* Ensure we got a valid first key at least */
if (self->keys[0].keyval == 0)
@@ -98,6 +109,7 @@ dzl_shortcut_chord_new_from_event (const GdkEventKey *key)
return NULL;
self = g_slice_new0 (DzlShortcutChord);
+ self->magic = CHORD_MAGIC;
self->keys[0].keyval = gdk_keyval_to_lower (key->keyval);
self->keys[0].modifier = sanitize_modifier_mask (key->state);
@@ -128,6 +140,7 @@ dzl_shortcut_chord_new_from_string (const gchar *accelerator)
return NULL;
self = g_slice_new0 (DzlShortcutChord);
+ self->magic = CHORD_MAGIC;
/* Parse each section from the accelerator */
for (guint i = 0; parts[i]; i++)
@@ -146,7 +159,7 @@ dzl_shortcut_chord_append_event (DzlShortcutChord *self,
{
guint i;
- g_return_val_if_fail (self != NULL, FALSE);
+ g_return_val_if_fail (IS_SHORTCUT_CHORD (self), FALSE);
g_return_val_if_fail (key != NULL, FALSE);
for (i = 0; i < G_N_ELEMENTS (self->keys); i++)
@@ -172,6 +185,8 @@ dzl_shortcut_chord_count_keys (const DzlShortcutChord *self)
{
guint count = 0;
+ g_assert (IS_SHORTCUT_CHORD (self));
+
for (guint i = 0; i < G_N_ELEMENTS (self->keys); i++)
{
if (self->keys[i].keyval != 0)
@@ -190,7 +205,7 @@ dzl_shortcut_chord_match (const DzlShortcutChord *self,
guint self_count = 0;
guint other_count = 0;
- g_return_val_if_fail (self != NULL, DZL_SHORTCUT_MATCH_NONE);
+ g_return_val_if_fail (IS_SHORTCUT_CHORD (self), DZL_SHORTCUT_MATCH_NONE);
g_return_val_if_fail (other != NULL, DZL_SHORTCUT_MATCH_NONE);
self_count = dzl_shortcut_chord_count_keys (self);
@@ -210,6 +225,8 @@ dzl_shortcut_chord_to_string (const DzlShortcutChord *self)
{
GString *str;
+ g_assert (IS_SHORTCUT_CHORD (self));
+
if (self == NULL || self->keys[0].keyval == 0)
return NULL;
@@ -242,6 +259,8 @@ dzl_shortcut_chord_get_label (const DzlShortcutChord *self)
if (self == NULL || self->keys[0].keyval == 0)
return NULL;
+ g_return_val_if_fail (IS_SHORTCUT_CHORD (self), NULL);
+
str = g_string_new (NULL);
for (guint i = 0; i < G_N_ELEMENTS (self->keys); i++)
@@ -266,15 +285,10 @@ dzl_shortcut_chord_get_label (const DzlShortcutChord *self)
DzlShortcutChord *
dzl_shortcut_chord_copy (const DzlShortcutChord *self)
{
- DzlShortcutChord *copy;
-
if (self == NULL)
return NULL;
- copy = g_slice_new (DzlShortcutChord);
- memcpy (copy, self, sizeof *copy);
-
- return copy;
+ return g_slice_dup (DzlShortcutChord, self);
}
guint
@@ -283,6 +297,8 @@ dzl_shortcut_chord_hash (gconstpointer data)
const DzlShortcutChord *self = data;
guint hash = 0;
+ g_assert (IS_SHORTCUT_CHORD (self));
+
for (guint i = 0; i < G_N_ELEMENTS (self->keys); i++)
{
const DzlShortcutKey *key = &self->keys[i];
@@ -311,8 +327,13 @@ dzl_shortcut_chord_equal (gconstpointer data1,
void
dzl_shortcut_chord_free (DzlShortcutChord *self)
{
+ g_assert (!self || IS_SHORTCUT_CHORD (self));
+
if (self != NULL)
- g_slice_free (DzlShortcutChord, self);
+ {
+ self->magic = 0xAAAAAAAA;
+ g_slice_free (DzlShortcutChord, self);
+ }
}
GType
@@ -342,6 +363,9 @@ dzl_shortcut_chord_table_sort (gconstpointer a,
const DzlShortcutChordTableEntry *keya = a;
const DzlShortcutChordTableEntry *keyb = b;
+ g_assert (IS_SHORTCUT_CHORD (a));
+ g_assert (IS_SHORTCUT_CHORD (b));
+
return dzl_shortcut_chord_compare (&keya->chord, &keyb->chord);
}
@@ -406,14 +430,14 @@ static void
dzl_shortcut_chord_table_remove_index (DzlShortcutChordTable *self,
guint position)
{
- const DzlShortcutChordTableEntry *entry;
+ DzlShortcutChordTableEntry *entry;
gpointer data;
g_assert (self != NULL);
g_assert (position < self->len);
entry = &self->entries[position];
- data = entry->data;
+ data = g_steal_pointer (&entry->data);
if (position + 1 < self->len)
memmove ((gpointer)entry,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]