changeset: 95733:bdf13c7dcf7f parent: 95730:abb86c6b11b2 parent: 95732:cdadde8396a4 user: Serhiy Storchaka date: Mon Apr 20 10:12:28 2015 +0300 files: Include/unicodeobject.h Lib/test/test_posix.py Misc/NEWS Modules/_io/clinic/fileio.c.h Modules/_io/fileio.c Modules/clinic/posixmodule.c.h Modules/posixmodule.c Objects/unicodeobject.c description: Issue #23908: os functions now reject paths with embedded null character on Windows instead of silently truncate them. Removed no longer used _PyUnicode_HasNULChars(). diff -r abb86c6b11b2 -r bdf13c7dcf7f Include/unicodeobject.h --- a/Include/unicodeobject.h Mon Apr 20 09:31:51 2015 +0300 +++ b/Include/unicodeobject.h Mon Apr 20 10:12:28 2015 +0300 @@ -2060,12 +2060,6 @@ PyObject *element /* Element string */ ); -/* Checks whether the string contains any NUL characters. */ - -#ifndef Py_LIMITED_API -PyAPI_FUNC(int) _PyUnicode_HasNULChars(PyObject *); -#endif - /* Checks whether argument is a valid identifier. */ PyAPI_FUNC(int) PyUnicode_IsIdentifier(PyObject *s); diff -r abb86c6b11b2 -r bdf13c7dcf7f Lib/test/test_posix.py --- a/Lib/test/test_posix.py Mon Apr 20 09:31:51 2015 +0300 +++ b/Lib/test/test_posix.py Mon Apr 20 10:12:28 2015 +0300 @@ -1168,6 +1168,42 @@ else: self.fail("No valid path_error2() test for os." + name) + def test_path_with_null_character(self): + fn = support.TESTFN + fn_with_NUL = fn + '\0' + self.addCleanup(support.unlink, fn) + support.unlink(fn) + fd = None + try: + with self.assertRaises(ValueError): + fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises + finally: + if fd is not None: + os.close(fd) + self.assertFalse(os.path.exists(fn)) + self.assertRaises(ValueError, os.mkdir, fn_with_NUL) + self.assertFalse(os.path.exists(fn)) + open(fn, 'wb').close() + self.assertRaises(ValueError, os.stat, fn_with_NUL) + + def test_path_with_null_byte(self): + fn = os.fsencode(support.TESTFN) + fn_with_NUL = fn + b'\0' + self.addCleanup(support.unlink, fn) + support.unlink(fn) + fd = None + try: + with self.assertRaises(ValueError): + fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises + finally: + if fd is not None: + os.close(fd) + self.assertFalse(os.path.exists(fn)) + self.assertRaises(ValueError, os.mkdir, fn_with_NUL) + self.assertFalse(os.path.exists(fn)) + open(fn, 'wb').close() + self.assertRaises(ValueError, os.stat, fn_with_NUL) + class PosixGroupsTester(unittest.TestCase): def setUp(self): diff -r abb86c6b11b2 -r bdf13c7dcf7f Misc/NEWS --- a/Misc/NEWS Mon Apr 20 09:31:51 2015 +0300 +++ b/Misc/NEWS Mon Apr 20 10:12:28 2015 +0300 @@ -37,6 +37,9 @@ Library ------- +- Issue #23908: os functions now reject paths with embedded null character + on Windows instead of silently truncate them. + - Issue #23728: binascii.crc_hqx() could return an integer outside of the range 0-0xffff for empty data. diff -r abb86c6b11b2 -r bdf13c7dcf7f Modules/_io/fileio.c --- a/Modules/_io/fileio.c Mon Apr 20 09:31:51 2015 +0300 +++ b/Modules/_io/fileio.c Mon Apr 20 10:12:28 2015 +0300 @@ -281,15 +281,14 @@ #ifdef MS_WINDOWS if (PyUnicode_Check(nameobj)) { - int rv = _PyUnicode_HasNULChars(nameobj); - if (rv) { - if (rv != -1) - PyErr_SetString(PyExc_ValueError, "embedded null character"); + Py_ssize_t length; + widename = PyUnicode_AsUnicodeAndSize(nameobj, &length); + if (widename == NULL) + return -1; + if (wcslen(widename) != length) { + PyErr_SetString(PyExc_ValueError, "embedded null character"); return -1; } - widename = PyUnicode_AsUnicode(nameobj); - if (widename == NULL) - return -1; } else #endif if (fd < 0) diff -r abb86c6b11b2 -r bdf13c7dcf7f Modules/posixmodule.c --- a/Modules/posixmodule.c Mon Apr 20 09:31:51 2015 +0300 +++ b/Modules/posixmodule.c Mon Apr 20 10:12:28 2015 +0300 @@ -866,6 +866,11 @@ Py_DECREF(unicode); return 0; } + if (wcslen(wide) != length) { + FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character"); + Py_DECREF(unicode); + return 0; + } path->wide = wide; path->narrow = NULL; diff -r abb86c6b11b2 -r bdf13c7dcf7f Objects/unicodeobject.c --- a/Objects/unicodeobject.c Mon Apr 20 09:31:51 2015 +0300 +++ b/Objects/unicodeobject.c Mon Apr 20 10:12:28 2015 +0300 @@ -3607,21 +3607,6 @@ int -_PyUnicode_HasNULChars(PyObject* str) -{ - Py_ssize_t pos; - - if (PyUnicode_READY(str) == -1) - return -1; - pos = findchar(PyUnicode_DATA(str), PyUnicode_KIND(str), - PyUnicode_GET_LENGTH(str), '\0', 1); - if (pos == -1) - return 0; - else - return 1; -} - -int PyUnicode_FSConverter(PyObject* arg, void* addr) { PyObject *output = NULL;