[pypy-commit] pypy default: Test and fix

arigo noreply at buildbot.pypy.org
Fri Oct 9 19:41:05 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r80095:5c2b0a8678a4
Date: 2015-10-09 19:40 +0200
http://bitbucket.org/pypy/pypy/changeset/5c2b0a8678a4/

Log:	Test and fix

diff --git a/pypy/module/_cffi_backend/ctypearray.py b/pypy/module/_cffi_backend/ctypearray.py
--- a/pypy/module/_cffi_backend/ctypearray.py
+++ b/pypy/module/_cffi_backend/ctypearray.py
@@ -18,6 +18,7 @@
     _attrs_            = ['ctptr']
     _immutable_fields_ = ['ctptr']
     kind = "array"
+    is_nonfunc_pointer_or_array = True
 
     def __init__(self, space, ctptr, length, arraysize, extra):
         W_CTypePtrOrArray.__init__(self, space, arraysize, extra, 0,
diff --git a/pypy/module/_cffi_backend/ctypeobj.py b/pypy/module/_cffi_backend/ctypeobj.py
--- a/pypy/module/_cffi_backend/ctypeobj.py
+++ b/pypy/module/_cffi_backend/ctypeobj.py
@@ -21,6 +21,7 @@
 
     cast_anything = False
     is_primitive_integer = False
+    is_nonfunc_pointer_or_array = False
     kind = "?"
 
     def __init__(self, space, size, name, name_position):
diff --git a/pypy/module/_cffi_backend/ctypeptr.py b/pypy/module/_cffi_backend/ctypeptr.py
--- a/pypy/module/_cffi_backend/ctypeptr.py
+++ b/pypy/module/_cffi_backend/ctypeptr.py
@@ -172,6 +172,7 @@
     _immutable_fields_ = ['is_file', 'cache_array_type?', 'is_void_ptr']
     kind = "pointer"
     cache_array_type = None
+    is_nonfunc_pointer_or_array = True
 
     def __init__(self, space, ctitem):
         from pypy.module._cffi_backend import ctypearray
diff --git a/pypy/module/_cffi_backend/func.py b/pypy/module/_cffi_backend/func.py
--- a/pypy/module/_cffi_backend/func.py
+++ b/pypy/module/_cffi_backend/func.py
@@ -124,6 +124,14 @@
     #
     return cdataobj.W_CDataFromBuffer(space, _cdata, w_ctype, buf, w_x)
 
+
+def unsafe_escaping_ptr_for_ptr_or_array(w_cdata):
+    if not w_cdata.ctype.is_nonfunc_pointer_or_array:
+        raise oefmt(w_cdata.space.w_TypeError,
+                    "expected a pointer or array ctype, got '%s'",
+                    w_cdata.ctype.name)
+    return w_cdata.unsafe_escaping_ptr()
+
 c_memmove = rffi.llexternal('memmove', [rffi.CCHARP, rffi.CCHARP,
                                         rffi.SIZE_T], lltype.Void,
                                 _nowrapper=True)
@@ -137,7 +145,7 @@
     src_buf = None
     src_data = lltype.nullptr(rffi.CCHARP.TO)
     if isinstance(w_src, cdataobj.W_CData):
-        src_data = w_src.unsafe_escaping_ptr()
+        src_data = unsafe_escaping_ptr_for_ptr_or_array(w_src)
         src_is_ptr = True
     else:
         src_buf = _fetch_as_read_buffer(space, w_src)
@@ -158,7 +166,7 @@
     dest_buf = None
     dest_data = lltype.nullptr(rffi.CCHARP.TO)
     if isinstance(w_dest, cdataobj.W_CData):
-        dest_data = w_dest.unsafe_escaping_ptr()
+        dest_data = unsafe_escaping_ptr_for_ptr_or_array(w_dest)
         dest_is_ptr = True
     else:
         dest_buf = _fetch_as_write_buffer(space, w_dest)
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
@@ -3459,6 +3459,12 @@
     p = newp(SignedCharA, 5)
     py.test.raises(ValueError, memmove, p, p + 1, -1)   # not segfault
 
+def test_memmove_bad_cdata():
+    BInt = new_primitive_type("int")
+    p = cast(BInt, 42)
+    py.test.raises(TypeError, memmove, p, bytearray(b'a'), 1)
+    py.test.raises(TypeError, memmove, bytearray(b'a'), p, 1)
+
 def test_dereference_null_ptr():
     BInt = new_primitive_type("int")
     BIntPtr = new_pointer_type(BInt)


More information about the pypy-commit mailing list