Author: Matti Picus <[email protected]>
Branch: ioctl-arg-size
Changeset: r94217:3e2c7c20e319
Date: 2018-04-02 20:23 +0300
http://bitbucket.org/pypy/pypy/changeset/3e2c7c20e319/

Log:    copy cpython approach to sizeof(arg) in ioctl - allocate at least
        IOCTL_BUFSZ

diff --git a/pypy/module/fcntl/interp_fcntl.py 
b/pypy/module/fcntl/interp_fcntl.py
--- a/pypy/module/fcntl/interp_fcntl.py
+++ b/pypy/module/fcntl/interp_fcntl.py
@@ -204,6 +204,7 @@
 
     # XXX this function's interface is a mess.
     # We try to emulate the behavior of Python >= 2.5 w.r.t. mutate_flag
+    IOCTL_BUFSZ = 1024 # like cpython
 
     fd = space.c_filedescriptor_w(w_fd)
     op = rffi.cast(rffi.INT, op)        # C long => C int
@@ -216,15 +217,19 @@
     else:
         arg = rwbuffer.as_str()
         ll_arg = rffi.str2charp(arg)
+        to_alloc = max(IOCTL_BUFSZ, len(arg))
         try:
-            rv = ioctl_str(fd, op, ll_arg)
-            if rv < 0:
-                raise _get_error(space, "ioctl")
-            arg = rffi.charpsize2str(ll_arg, len(arg))
-            if mutate_flag != 0:
-                rwbuffer.setslice(0, arg)
-                return space.newint(rv)
-            return space.newbytes(arg)
+            with rffi.scoped_alloc_buffer(to_alloc) as buf:
+                rffi.c_memcpy(rffi.cast(rffi.VOIDP, buf.raw),
+                              rffi.cast(rffi.VOIDP, ll_arg), len(arg))
+                rv = ioctl_str(fd, op, buf.raw)
+                if rv < 0:
+                    raise _get_error(space, "ioctl")
+                arg = rffi.charpsize2str(buf.raw, len(arg))
+                if mutate_flag != 0:
+                    rwbuffer.setslice(0, arg)
+                    return space.newint(rv)
+                return space.newbytes(arg)
         finally:
             lltype.free(ll_arg, flavor='raw')
 
@@ -240,11 +245,15 @@
             raise
     else:
         ll_arg = rffi.str2charp(arg)
+        to_alloc = max(IOCTL_BUFSZ, len(arg))
         try:
-            rv = ioctl_str(fd, op, ll_arg)
-            if rv < 0:
-                raise _get_error(space, "ioctl")
-            arg = rffi.charpsize2str(ll_arg, len(arg))
+            with rffi.scoped_alloc_buffer(to_alloc) as buf:
+                rffi.c_memcpy(rffi.cast(rffi.VOIDP, buf.raw),
+                              rffi.cast(rffi.VOIDP, ll_arg), len(arg))
+                rv = ioctl_str(fd, op, buf.raw)
+                if rv < 0:
+                    raise _get_error(space, "ioctl")
+                arg = rffi.charpsize2str(buf.raw, len(arg))
             return space.newbytes(arg)
         finally:
             lltype.free(ll_arg, flavor='raw')
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to