]> granicus.if.org Git - python/commitdiff
Issue #23908: os functions now reject paths with embedded null character
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 20 Apr 2015 07:12:28 +0000 (10:12 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 20 Apr 2015 07:12:28 +0000 (10:12 +0300)
on Windows instead of silently truncate them.

Removed no longer used _PyUnicode_HasNULChars().

1  2 
Include/unicodeobject.h
Lib/test/test_posix.py
Misc/NEWS
Modules/_io/fileio.c
Modules/posixmodule.c
Objects/unicodeobject.c

index 814d7c0cf63904986da78e26bb90e3537463f5a7,d7b2acebdec418bd8c6710db04df1b7b71e13335..4ba632803c021343c57f920bbc246640f212e16f
@@@ -2060,12 -2060,12 +2060,6 @@@ 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 d49cd92c77d324d23910d1f8872771f057d344bf,aeb89241072f7108488c00c601f2f085061fa7bb..77e5b0c4fdf6fba9059cf6e0d9ce0d0799f3b459
@@@ -1168,6 -1169,42 +1168,42 @@@ class PosixTester(unittest.TestCase)
              else:
                  self.fail("No valid path_error2() test for os." + name)
  
 -            with self.assertRaises(TypeError):
+     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:
 -        self.assertRaises(TypeError, os.mkdir, fn_with_NUL)
++            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(TypeError, os.stat, fn_with_NUL)
++        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 --cc Misc/NEWS
Simple merge
index 145d93adeb156bb51be5e4d5e19e5ebb5f6760a3,74508a7f42869f76c085adfa1c42a59fbcf70ef0..0894ca45c891bbbadb5d1cc4007aad6549933817
@@@ -281,15 -275,14 +281,14 @@@ _io_FileIO___init___impl(fileio *self, 
  
  #ifdef MS_WINDOWS
      if (PyUnicode_Check(nameobj)) {
-         int rv = _PyUnicode_HasNULChars(nameobj);
-         if (rv) {
-             if (rv != -1)
-                 PyErr_SetString(PyExc_ValueError, "embedded null character");
-             return -1;
-         }
-         widename = PyUnicode_AsUnicode(nameobj);
+         Py_ssize_t length;
+         widename = PyUnicode_AsUnicodeAndSize(nameobj, &length);
          if (widename == NULL)
              return -1;
 -            PyErr_SetString(PyExc_TypeError, "embedded NUL character");
+         if (wcslen(widename) != length) {
++            PyErr_SetString(PyExc_ValueError, "embedded null character");
+             return -1;
+         }
      } else
  #endif
      if (fd < 0)
index 89fb08b6743a979d0a16f935a507ce5e78bd26f5,e5384379415433856d95ebd13e6cea5848d18e01..90d820fa8115dcd0307f4a3abf5cfc70e77f0b86
@@@ -866,6 -858,11 +866,11 @@@ path_converter(PyObject *o, void *p) 
              Py_DECREF(unicode);
              return 0;
          }
 -            FORMAT_EXCEPTION(PyExc_TypeError, "embedded null character");
+         if (wcslen(wide) != length) {
++            FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character");
+             Py_DECREF(unicode);
+             return 0;
+         }
  
          path->wide = wide;
          path->narrow = NULL;
index 9611ed41f7fe81a51adf0b795852c224eee9bfe5,b425e43289c2a4f0df052fe38759739a06d6050e..3225fb3be9d41b358c3ee9e7b1792ceb8dd333d6
@@@ -3606,21 -3642,21 +3606,6 @@@ PyUnicode_DecodeFSDefaultAndSize(const 
  }
  
  
--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)
  {