[Python-checkins] bpo-38020: Fixes crash in os.readlink() on Windows (GH-15663)

Steve Dower webhook-mailer at python.org
Tue Sep 3 15:50:57 EDT 2019


https://github.com/python/cpython/commit/993ac92418839427d4068d6ae8e618b06b5d9294
commit: 993ac92418839427d4068d6ae8e618b06b5d9294
branch: master
author: Steve Dower <steve.dower at python.org>
committer: GitHub <noreply at github.com>
date: 2019-09-03T12:50:51-07:00
summary:

bpo-38020: Fixes crash in os.readlink() on Windows (GH-15663)

files:
A Misc/NEWS.d/next/Windows/2019-09-03-11-47-37.bpo-38020.xFZ2j0.rst
M Modules/posixmodule.c

diff --git a/Misc/NEWS.d/next/Windows/2019-09-03-11-47-37.bpo-38020.xFZ2j0.rst b/Misc/NEWS.d/next/Windows/2019-09-03-11-47-37.bpo-38020.xFZ2j0.rst
new file mode 100644
index 000000000000..e6ddaba2f67b
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2019-09-03-11-47-37.bpo-38020.xFZ2j0.rst
@@ -0,0 +1,2 @@
+Fixes potential crash when calling :func:`os.readlink` (or indirectly
+through :func:`~os.path.realpath`) on a file that is not a supported link.
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 2302678ccc14..c412d07a0e11 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -7818,7 +7818,7 @@ os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
     HANDLE reparse_point_handle;
     char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
     _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
-    PyObject *result;
+    PyObject *result = NULL;
 
     /* First get a handle to the reparse point */
     Py_BEGIN_ALLOW_THREADS
@@ -7872,7 +7872,7 @@ os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
             name[1] = L'\\';
         }
         result = PyUnicode_FromWideChar(name, nameLen);
-        if (path->narrow) {
+        if (result && path->narrow) {
             Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
         }
     }



More information about the Python-checkins mailing list