diff --git a/Include/unicodeobject.h b/Include/unicodeobject.h --- a/Include/unicodeobject.h +++ b/Include/unicodeobject.h @@ -1692,6 +1692,10 @@ PyObject *unicode ); +/* Checks for NUL characters in a str object. Useful for path sanity checks. */ +PyAPI_FUNC(int) _PyUnicode_HasNULChars(PyObject*); + + /* --- Methods & Slots ---------------------------------------------------- These are capable of handling Unicode objects and strings on input diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -306,6 +306,11 @@ finally: os.unlink(TESTFN) + def testConstructorHandlesNULChars(self): + fn_with_NUL = 'foo\0bar' + self.assertRaises(TypeError, _FileIO, fn_with_NUL, 'w') + self.assertRaises(TypeError, _FileIO, bytes(fn_with_NUL, 'ascii'), 'w') + def testInvalidFd(self): self.assertRaises(ValueError, _FileIO, -10) self.assertRaises(OSError, _FileIO, make_bad_fd()) diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -363,6 +363,11 @@ self.assertRaises(exc, fp.seek, 1, self.SEEK_CUR) self.assertRaises(exc, fp.seek, -1, self.SEEK_END) + def test_open_handles_NUL_chars(self): + fn_with_NUL = 'foo\0bar' + self.assertRaises(TypeError, open, fn_with_NUL, 'w') + self.assertRaises(TypeError, open, bytes(fn_with_NUL, 'ascii'), 'w') + def test_raw_file_io(self): with self.open(support.TESTFN, "wb", buffering=0) as f: self.assertEqual(f.readable(), False) diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -258,6 +258,12 @@ #ifdef MS_WINDOWS if (PyUnicode_Check(nameobj)) { + int rv = _PyUnicode_HasNULChars(nameobj); + if (rv) { + if (rv != -1) + PyErr_SetString(PyExc_TypeError, "embedded NUL character"); + return -1; + } widename = PyUnicode_AsUnicode(nameobj); if (widename == NULL) return -1; @@ -265,28 +271,10 @@ #endif if (fd < 0) { - if (PyBytes_Check(nameobj) || PyByteArray_Check(nameobj)) { - Py_ssize_t namelen; - if (PyObject_AsCharBuffer(nameobj, &name, &namelen) < 0) - return -1; + if (!PyUnicode_FSConverter(nameobj, &stringobj)) { + return -1; } - else { - PyObject *u = PyUnicode_FromObject(nameobj); - - if (u == NULL) - return -1; - - stringobj = PyUnicode_EncodeFSDefault(u); - Py_DECREF(u); - if (stringobj == NULL) - return -1; - if (!PyBytes_Check(stringobj)) { - PyErr_SetString(PyExc_TypeError, - "encoder failed to return bytes"); - goto error; - } - name = PyBytes_AS_STRING(stringobj); - } + name = PyBytes_AS_STRING(stringobj); } s = mode; diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -3575,6 +3575,21 @@ int +_PyUnicode_HasNULChars(PyObject* s) +{ + static PyObject *nul; + int rv; + + if (nul == NULL) + nul = PyUnicode_FromStringAndSize("\0", 1); + if (nul == NULL) + return -1; + rv = PyUnicode_Contains(s, nul); + return rv; +} + + +int PyUnicode_FSConverter(PyObject* arg, void* addr) { PyObject *output = NULL;