pytalloc: Fix comparison of disparate types
authorPetr Viktorin <pviktori@redhat.com>
Fri, 6 Mar 2015 17:57:00 +0000 (18:57 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Thu, 23 Apr 2015 23:50:11 +0000 (01:50 +0200)
When fed Python objects of different types, pytalloc_default_cmp
compared pointers to PyType objects that weren't part of an array,
resulting in undefined behavior.

This makes things a bit better (though it still casts ptrdiff_t to int).

Signed-off-by: Petr Viktorin <pviktori@redhat.com>
Reviewed-By: Jelmer Vernooij <jelmer@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
lib/talloc/pytalloc.c

index 80196c6c77b58c395f5dde78e3d19f6d9de4c4a3..ac4fe0f41ec81199cb3a57defd38551e26d2e10d 100644 (file)
@@ -102,7 +102,7 @@ static int pytalloc_default_cmp(PyObject *_obj1, PyObject *_obj2)
        pytalloc_Object *obj1 = (pytalloc_Object *)_obj1,
                                         *obj2 = (pytalloc_Object *)_obj2;
        if (obj1->ob_type != obj2->ob_type)
-               return (obj1->ob_type - obj2->ob_type);
+               return ((char *)obj1->ob_type - (char *)obj2->ob_type);
 
        return ((char *)pytalloc_get_ptr(obj1) - (char *)pytalloc_get_ptr(obj2));
 }