Handle AF_UNIX addresses according to PEP 383. diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -977,7 +977,7 @@ makesockaddr(SOCKET_T sockfd, struct soc for (len = 0; len < splen && a->sun_path[len] != 0; len++) ; } - return PyUnicode_FromStringAndSize(a->sun_path, len); + return PyUnicode_DecodeFSDefaultAndSize(a->sun_path, len); } #endif /* AF_UNIX */ @@ -1149,8 +1149,18 @@ getsockaddrarg(PySocketSockObject *s, Py struct sockaddr_un* addr; char *path; int len; - if (!PyArg_Parse(args, "s#", &path, &len)) - return 0; + int retval = 0; + + /* PEP 383. Not using PyUnicode_FSConverter since we need to + allow embedded nulls on Linux. */ + if (PyUnicode_Check(args)) { + if ((args = PyUnicode_EncodeFSDefault(args)) == NULL) + return 0; + } + else + Py_INCREF(args); + if (!PyArg_Parse(args, "y#", &path, &len)) + goto unix_out; addr = (struct sockaddr_un*)addr_ret; #ifdef linux @@ -1159,7 +1169,7 @@ getsockaddrarg(PySocketSockObject *s, Py if (len > sizeof addr->sun_path) { PyErr_SetString(socket_error, "AF_UNIX path too long"); - return 0; + goto unix_out; } } else @@ -1169,7 +1179,7 @@ getsockaddrarg(PySocketSockObject *s, Py if (len >= sizeof addr->sun_path) { PyErr_SetString(socket_error, "AF_UNIX path too long"); - return 0; + goto unix_out; } addr->sun_path[len] = 0; } @@ -1180,7 +1190,10 @@ getsockaddrarg(PySocketSockObject *s, Py #else *len_ret = len + offsetof(struct sockaddr_un, sun_path); #endif - return 1; + retval = 1; + unix_out: + Py_DECREF(args); + return retval; } #endif /* AF_UNIX */