diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-07-29-09-52-22.gh-issue-95380.p8S_K0.rst b/Misc/NEWS.d/next/Core and Builtins/2022-07-29-09-52-22.gh-issue-95380.p8S_K0.rst new file mode 100644 index 00000000000000..3ae365fb43c154 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-07-29-09-52-22.gh-issue-95380.p8S_K0.rst @@ -0,0 +1 @@ +Remove max path size limit of 1024 for :func:`fcntl.fcntl` function. Patch by Grégory Starck. diff --git a/Modules/clinic/fcntlmodule.c.h b/Modules/clinic/fcntlmodule.c.h index c41f088ff1528b..4562b1b505c1f5 100644 --- a/Modules/clinic/fcntlmodule.c.h +++ b/Modules/clinic/fcntlmodule.c.h @@ -13,8 +13,7 @@ PyDoc_STRVAR(fcntl_fcntl__doc__, "the relevant C header files. The argument arg is optional, and\n" "defaults to 0; it may be an int or a string. If arg is given as a string,\n" "the return value of fcntl is a string of that length, containing the\n" -"resulting value put in the arg buffer by the operating system. The length\n" -"of the arg string is not allowed to exceed 1024 bytes. If the arg given\n" +"resulting value put in the arg buffer by the operating system. If the arg given\n" "is an integer or if none is specified, the result value is an integer\n" "corresponding to the return value of the fcntl call in the C code."); @@ -243,4 +242,4 @@ fcntl_lockf(PyObject *module, PyObject *const *args, Py_ssize_t nargs) exit: return return_value; } -/*[clinic end generated code: output=b8cb14ab35de4c6a input=a9049054013a1b77]*/ +/*[clinic end generated code: output=aac49bd0da6cfb17 input=a9049054013a1b77]*/ diff --git a/Modules/fcntlmodule.c b/Modules/fcntlmodule.c index 9a8ec8dc9858d7..088874c53df008 100644 --- a/Modules/fcntlmodule.c +++ b/Modules/fcntlmodule.c @@ -40,21 +40,19 @@ as constants in the fcntl module, using the same names as used in the relevant C header files. The argument arg is optional, and defaults to 0; it may be an int or a string. If arg is given as a string, the return value of fcntl is a string of that length, containing the -resulting value put in the arg buffer by the operating system. The length -of the arg string is not allowed to exceed 1024 bytes. If the arg given +resulting value put in the arg buffer by the operating system. If the arg given is an integer or if none is specified, the result value is an integer corresponding to the return value of the fcntl call in the C code. [clinic start generated code]*/ static PyObject * fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg) -/*[clinic end generated code: output=888fc93b51c295bd input=7955340198e5f334]*/ +/*[clinic end generated code: output=888fc93b51c295bd input=eed7ab9999c13350]*/ { unsigned int int_arg = 0; int ret; char *str; Py_ssize_t len; - char buf[1024]; int async_err = 0; if (PySys_Audit("fcntl.fcntl", "iiO", fd, code, arg ? arg : Py_None) < 0) { @@ -65,11 +63,12 @@ fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg) int parse_result; if (PyArg_Parse(arg, "s#", &str, &len)) { - if ((size_t)len > sizeof buf) { - PyErr_SetString(PyExc_ValueError, - "fcntl string arg too long"); + PyObject* buf_ret = PyBytes_FromStringAndSize(NULL, len); + if (buf_ret == NULL) { + PyErr_NoMemory(); return NULL; } + char *buf = PyBytes_AS_STRING(buf_ret); memcpy(buf, str, len); do { Py_BEGIN_ALLOW_THREADS @@ -77,9 +76,10 @@ fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg) Py_END_ALLOW_THREADS } while (ret == -1 && errno == EINTR && !(async_err = PyErr_CheckSignals())); if (ret < 0) { + Py_DECREF(buf_ret); return !async_err ? PyErr_SetFromErrno(PyExc_OSError) : NULL; } - return PyBytes_FromStringAndSize(buf, len); + return buf_ret; } PyErr_Clear();