Commits

Armin Rigo committed 0ac3dee

Test and fix: two pointers of different types can compare equal, but
they still had different hashes

Comments (0)

Files changed (2)

c/_cffi_backend.c

 
 static long cdata_hash(CDataObject *cd)
 {
-    long h = _Py_HashPointer(cd->c_type) ^ _Py_HashPointer(cd->c_data);
-    if (h == -1)
-        h = -2;
-    return h;
+    return _Py_HashPointer(cd->c_data);
 }
 
 static Py_ssize_t
     BInt = new_primitive_type("int")
     BFloat = new_primitive_type("float")
     for i in range(1, 20):
-        if (hash(cast(BChar, chr(i))) !=
-            hash(cast(BInt, i))):
+        x1 = cast(BChar, chr(i))
+        x2 = cast(BInt, i)
+        if hash(x1) != hash(x2):
             break
     else:
         raise AssertionError("hashes are equal")
     assert x.__name__ == '<cdata>'
     assert hasattr(x, '__doc__')
 
+def test_different_types_of_ptr_equality():
+    BVoidP = new_pointer_type(new_void_type())
+    BIntP = new_pointer_type(new_primitive_type("int"))
+    x = cast(BVoidP, 12345)
+    assert x == cast(BIntP, 12345)
+    assert x != cast(BIntP, 12344)
+    assert hash(x) == hash(cast(BIntP, 12345))
+
 def test_version():
     # this test is here mostly for PyPy
     assert __version__ == "0.6"