diff options
author | Antonio Gutierrez <chibby0ne@gmail.com> | 2019-10-09 02:19:48 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2019-10-09 02:19:48 (GMT) |
commit | 594e2edfb5e0d24e03469d035d8f39ff29a64d99 (patch) | |
tree | e40d0cc1fd6a74704b53922f7ec86637e8ebf245 /Modules/posixmodule.c | |
parent | e8bedbddadaa86be6bd86dc32dbdbd53933a4988 (diff) | |
download | cpython-594e2edfb5e0d24e03469d035d8f39ff29a64d99.zip cpython-594e2edfb5e0d24e03469d035d8f39ff29a64d99.tar.gz cpython-594e2edfb5e0d24e03469d035d8f39ff29a64d99.tar.bz2 |
closes bpo-36161: Use thread-safe ttyname_r instead of ttyname. (GH-14868)
Signed-off-by: Antonio Gutierrez <chibby0ne@gmail.com>
Diffstat (limited to 'Modules/posixmodule.c')
-rw-r--r-- | Modules/posixmodule.c | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 5664027..aeb0e9d 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -2774,13 +2774,24 @@ static PyObject * os_ttyname_impl(PyObject *module, int fd) /*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/ { - char *ret; - ret = ttyname(fd); - if (ret == NULL) { + long size = sysconf(_SC_TTY_NAME_MAX); + if (size == -1) { + return posix_error(); + } + char *buffer = (char *)PyMem_RawMalloc(size); + if (buffer == NULL) { + return PyErr_NoMemory(); + } + int ret = ttyname_r(fd, buffer, size); + if (ret != 0) { + PyMem_RawFree(buffer); + errno = ret; return posix_error(); } - return PyUnicode_DecodeFSDefault(ret); + PyObject *res = PyUnicode_DecodeFSDefault(buffer); + PyMem_RawFree(buffer); + return res; } #endif |