Eo: Added debug-mode check for eo_data_get.
authortasn <tasn>
Wed, 2 May 2012 13:59:18 +0000 (13:59 +0000)
committertasn <tasn@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Wed, 2 May 2012 13:59:18 +0000 (13:59 +0000)
git-svn-id: http://svn.enlightenment.org/svn/e/trunk/PROTO/eobj@70625 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

lib/eo.c
tests/eo_test_general.c

index 177bfa9..dcf7cf0 100644 (file)
--- a/lib/eo.c
+++ b/lib/eo.c
@@ -486,6 +486,21 @@ _eo_class_base_op_init(Eo_Class *klass)
    *(desc->ops.base_op_id) = klass->class_id << OP_CLASS_OFFSET;
 }
 
+static Eina_Bool
+_eo_class_mro_has(const Eo_Class *klass, const Eo_Class *find)
+{
+   const Eo_Class **itr;
+   for (itr = klass->mro ; *itr ; itr++)
+     {
+        if (*itr == find)
+          {
+             return EINA_TRUE;
+          }
+     }
+
+   return EINA_FALSE;
+}
+
 static Eina_List *
 _eo_class_mro_add(Eina_List *mro, const Eo_Class *klass)
 {
@@ -1189,8 +1204,13 @@ eo_data_get(const Eo *obj, const Eo_Class *klass)
 {
    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
 
-   /* FIXME: Add a check that this is of the right klass and we don't seg.
-    * Probably just return NULL. */
+#ifndef NDEBUG
+   if (!_eo_class_mro_has(obj->klass, klass))
+     {
+        ERR("Tried getting data of class '%s' from object of class '%s', but the former is not a direct inheritance of the latter.", klass->desc->name, obj->klass->desc->name);
+        return NULL;
+     }
+#endif
    if (EINA_LIKELY(klass->desc->data_size > 0))
      {
         if (EINA_UNLIKELY(klass->desc->type == EO_CLASS_TYPE_MIXIN))
index 7e61562..7b989a2 100644 (file)
@@ -19,6 +19,33 @@ START_TEST(eo_simple)
 }
 END_TEST
 
+START_TEST(eo_data_fetch)
+{
+   eo_init();
+
+   static const Eo_Class_Description class_desc = {
+        "Simple2",
+        EO_CLASS_TYPE_REGULAR,
+        EO_CLASS_DESCRIPTION_OPS(NULL, NULL, 0),
+        NULL,
+        10,
+        NULL,
+        NULL,
+        NULL,
+        NULL
+   };
+
+   const Eo_Class *klass = eo_class_new(&class_desc, EO_BASE_CLASS, NULL);
+   fail_if(!klass);
+
+   Eo *obj = eo_add(klass, NULL);
+   fail_if(!obj);
+   fail_if(eo_data_get(obj, SIMPLE_CLASS));
+
+   eo_shutdown();
+}
+END_TEST
+
 START_TEST(eo_refs)
 {
    eo_init();
@@ -353,4 +380,5 @@ void eo_test_general(TCase *tc)
    tcase_add_test(tc, eo_weak_reference);
    tcase_add_test(tc, eo_refs);
    tcase_add_test(tc, eo_magic_checks);
+   tcase_add_test(tc, eo_data_fetch);
 }