]> granicus.if.org Git - python/commitdiff
Issue #13848: open() and the FileIO constructor now check for NUL characters in the...
authorAntoine Pitrou <solipsis@pitrou.net>
Sun, 29 Jan 2012 17:36:34 +0000 (18:36 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Sun, 29 Jan 2012 17:36:34 +0000 (18:36 +0100)
Patch by Hynek Schlawack.

Include/unicodeobject.h
Lib/test/test_fileio.py
Lib/test/test_io.py
Misc/ACKS
Misc/NEWS
Modules/_io/fileio.c
Objects/unicodeobject.c

index 477f526637bb74ae71450e93c4a5bf7b41d9a67b..379a90c6c27a3684ecc774582ca39110374c248e 100644 (file)
@@ -1501,6 +1501,12 @@ PyAPI_FUNC(int) PyUnicode_Contains(
     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);
index 103365d8f2432d37bc09d460f3be39b5d1ccc243..3588fb449b401da83e59d2d94a87a73b11c6ec01 100644 (file)
@@ -305,6 +305,11 @@ class OtherFileTests(unittest.TestCase):
         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())
index df7ca15f58fe06a0070f4434f8807b864654d9eb..ea82cea17ae688570c299ad9b62539c419430d59 100644 (file)
@@ -363,6 +363,11 @@ class IOTest(unittest.TestCase):
             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, self.open, fn_with_NUL, 'w')
+        self.assertRaises(TypeError, self.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)
index 8117d80b13703d1b37b4749e2365434ecd8a0e85..48df3fb1d5e562897daabe3ba13459e52e851060 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -811,6 +811,7 @@ Michael Scharf
 Andreas Schawo
 Neil Schemenauer
 David Scherer
+Hynek Schlawack
 Bob Schmertz
 Gregor Schmid
 Ralf Schmitt
index e02a7c923921ca7baa9a56e8c5cc1ecd064e0842..1287d2d8a54c48af48102b400456d1563af09cb9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -111,6 +111,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #13848: open() and the FileIO constructor now check for NUL
+  characters in the file name.  Patch by Hynek Schlawack.
+
 - Issue #13806: The size check in audioop decompression functions was too
   strict and could reject valid compressed data.  Patch by Oleg Plakhotnyuk.
 
index f39f8b0d879558752717874ea79b06e5f9bc3c82..d5b03eec26aa0d676c9301401c76d60ed1ee6a5c 100644 (file)
@@ -253,34 +253,23 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
     }
 
 #ifdef MS_WINDOWS
-    if (PyUnicode_Check(nameobj))
+    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_AS_UNICODE(nameobj);
+    }
     if (widename == NULL)
 #endif
     if (fd < 0)
     {
-        if (PyBytes_Check(nameobj) || PyByteArray_Check(nameobj)) {
-            Py_ssize_t namelen;
-            if (PyObject_AsCharBuffer(nameobj, &name, &namelen) < 0)
-                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);
+        if (!PyUnicode_FSConverter(nameobj, &stringobj)) {
+            return -1;
         }
+        name = PyBytes_AS_STRING(stringobj);
     }
 
     s = mode;
index 20528b931d02afc802fbabfb2a3c7c48df692372..f51d4d04beb5c4a004a078414474151a4c7dff41 100644 (file)
@@ -1866,6 +1866,19 @@ PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
 }
 
 
+int
+_PyUnicode_HasNULChars(PyObject* s)
+{
+    static PyObject *nul = NULL;
+
+    if (nul == NULL)
+        nul = PyUnicode_FromStringAndSize("\0", 1);
+    if (nul == NULL)
+        return -1;
+    return PyUnicode_Contains(s, nul);
+}
+
+
 int
 PyUnicode_FSConverter(PyObject* arg, void* addr)
 {