[pypy-commit] pypy default: cffi/763505916552: Give an error when subtracting two pointers and the
arigo
pypy.commits at gmail.com
Mon Aug 15 05:17:36 EDT 2016
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r86195:b3b9882a5be7
Date: 2016-08-15 11:07 +0200
http://bitbucket.org/pypy/pypy/changeset/b3b9882a5be7/
Log: cffi/763505916552: Give an error when subtracting two pointers and
the division's result is not exact (in gcc, we get nonsense, so it
means it is undefined behavior for C, which is best handled by
raising in cffi)
diff --git a/pypy/module/_cffi_backend/cdataobj.py b/pypy/module/_cffi_backend/cdataobj.py
--- a/pypy/module/_cffi_backend/cdataobj.py
+++ b/pypy/module/_cffi_backend/cdataobj.py
@@ -310,11 +310,15 @@
self.ctype.name, ct.name)
#
itemsize = ct.ctitem.size
- if itemsize <= 0:
- itemsize = 1
with self as ptr1, w_other as ptr2:
diff = (rffi.cast(lltype.Signed, ptr1) -
- rffi.cast(lltype.Signed, ptr2)) // itemsize
+ rffi.cast(lltype.Signed, ptr2))
+ if itemsize > 1:
+ if diff % itemsize:
+ raise oefmt(space.w_ValueError,
+ "pointer subtraction: the distance between the two "
+ "pointers is not a multiple of the item size")
+ diff //= itemsize
return space.wrap(diff)
#
return self._add_or_sub(w_other, -1)
diff --git a/pypy/module/_cffi_backend/test/_backend_test_c.py b/pypy/module/_cffi_backend/test/_backend_test_c.py
--- a/pypy/module/_cffi_backend/test/_backend_test_c.py
+++ b/pypy/module/_cffi_backend/test/_backend_test_c.py
@@ -576,6 +576,19 @@
e = py.test.raises(TypeError, "q - a")
assert str(e.value) == "cannot subtract cdata 'short *' and cdata 'int *'"
+def test_ptr_sub_unaligned():
+ BInt = new_primitive_type("int")
+ BIntPtr = new_pointer_type(BInt)
+ a = cast(BIntPtr, 1240)
+ for bi in range(1430, 1438):
+ b = cast(BIntPtr, bi)
+ if ((bi - 1240) % size_of_int()) == 0:
+ assert b - a == (bi - 1240) // size_of_int()
+ assert a - b == (1240 - bi) // size_of_int()
+ else:
+ py.test.raises(ValueError, "b - a")
+ py.test.raises(ValueError, "a - b")
+
def test_cast_primitive_from_cdata():
p = new_primitive_type("int")
n = cast(p, cast(p, -42))
More information about the pypy-commit
mailing list