Issue #23908: os functions, open() and the io.FileIO constructor now reject
unicode paths with embedded null character on Windows instead of silently
truncate them.
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index 74b2305..0678ef8 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -224,8 +224,13 @@
}
#ifdef MS_WINDOWS
- if (PyUnicode_Check(nameobj))
+ if (PyUnicode_Check(nameobj)) {
widename = PyUnicode_AS_UNICODE(nameobj);
+ if (wcslen(widename) != (size_t)PyUnicode_GET_SIZE(nameobj)) {
+ PyErr_SetString(PyExc_TypeError, "embedded NUL character");
+ return -1;
+ }
+ }
if (widename == NULL)
#endif
if (fd < 0)
@@ -234,6 +239,10 @@
Py_ssize_t namelen;
if (PyObject_AsCharBuffer(nameobj, &name, &namelen) < 0)
return -1;
+ if (strlen(name) != (size_t)namelen) {
+ PyErr_SetString(PyExc_TypeError, "embedded NUL character");
+ return -1;
+ }
}
else {
PyObject *u = PyUnicode_FromObject(nameobj);
@@ -252,6 +261,10 @@
goto error;
}
name = PyBytes_AS_STRING(stringobj);
+ if (strlen(name) != (size_t)PyBytes_GET_SIZE(stringobj)) {
+ PyErr_SetString(PyExc_TypeError, "embedded NUL character");
+ goto error;
+ }
}
}