]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #23908: os functions now reject paths with embedded null character
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 20 Apr 2015 06:53:58 +0000 (09:53 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 20 Apr 2015 06:53:58 +0000 (09:53 +0300)
on Windows instead of silently truncate them.

Lib/test/test_posix.py
Misc/NEWS
Modules/_io/fileio.c
Modules/posixmodule.c

index ce8f0b35cb586f69f5cb795dab21ae94fd1fc2e3..aeb89241072f7108488c00c601f2f085061fa7bb 100644 (file)
@@ -1169,6 +1169,42 @@ class PosixTester(unittest.TestCase):
             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):
index 84bd2ccd560eb302f96f0322c1f8a4d89e8eb2f7..e384e5841d9887d8e36966e7fad00cd00864aeca 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,9 @@ Core and Builtins
 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.
 
index 95bcb77c3cc61e84a3373c2a622d9f02382fd36c..74508a7f42869f76c085adfa1c42a59fbcf70ef0 100644 (file)
@@ -275,15 +275,14 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
 
 #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);
+        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;
+        }
     } else
 #endif
     if (fd < 0)
index d45f59e5f1b6763eb71d14df31b5c488ca61a119..e5384379415433856d95ebd13e6cea5848d18e01 100644 (file)
@@ -858,6 +858,11 @@ path_converter(PyObject *o, void *p) {
             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;