[Python-checkins] cpython (3.4): Issue #23908: os functions now reject paths with embedded null character

serhiy.storchaka python-checkins at python.org
Mon Apr 20 09:14:31 CEST 2015


https://hg.python.org/cpython/rev/cdadde8396a4
changeset:   95732:cdadde8396a4
branch:      3.4
parent:      95729:0ead02929df2
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Mon Apr 20 09:53:58 2015 +0300
summary:
  Issue #23908: os functions now reject paths with embedded null character
on Windows instead of silently truncate them.

files:
  Lib/test/test_posix.py |  36 ++++++++++++++++++++++++++++++
  Misc/NEWS              |   3 ++
  Modules/_io/fileio.c   |  13 +++++-----
  Modules/posixmodule.c  |   5 ++++
  4 files changed, 50 insertions(+), 7 deletions(-)


diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py
--- a/Lib/test/test_posix.py
+++ b/Lib/test/test_posix.py
@@ -1169,6 +1169,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(TypeError):
+                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(TypeError, os.mkdir, fn_with_NUL)
+        self.assertFalse(os.path.exists(fn))
+        open(fn, 'wb').close()
+        self.assertRaises(TypeError, 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 --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,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 --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -275,15 +275,14 @@
 
 #ifdef MS_WINDOWS
     if (PyUnicode_Check(nameobj)) {
-        int rv = _PyUnicode_HasNULChars(nameobj);
-        if (rv) {
-            if (rv != -1)
-                PyErr_SetString(PyExc_TypeError, "embedded NUL character");
+        Py_ssize_t length;
+        widename = PyUnicode_AsUnicodeAndSize(nameobj, &length);
+        if (widename == NULL)
+            return -1;
+        if (wcslen(widename) != length) {
+            PyErr_SetString(PyExc_TypeError, "embedded NUL character");
             return -1;
         }
-        widename = PyUnicode_AsUnicode(nameobj);
-        if (widename == NULL)
-            return -1;
     } else
 #endif
     if (fd < 0)
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -858,6 +858,11 @@
             Py_DECREF(unicode);
             return 0;
         }
+        if (wcslen(wide) != length) {
+            FORMAT_EXCEPTION(PyExc_TypeError, "embedded null character");
+            Py_DECREF(unicode);
+            return 0;
+        }
 
         path->wide = wide;
         path->narrow = NULL;

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list