]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-106242: Make ntpath.realpath errors consistent with abspath when there...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 22 Aug 2023 14:35:16 +0000 (07:35 -0700)
committerGitHub <noreply@github.com>
Tue, 22 Aug 2023 14:35:16 +0000 (15:35 +0100)
gh-106242: Make ntpath.realpath errors consistent with abspath when there are embedded nulls (GH-108248)

---------

(cherry picked from commit de33b5c662ea8d35d81ed857c6a39e34ab94c510)

Co-authored-by: Steve Dower <steve.dower@python.org>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
Lib/ntpath.py
Lib/test/test_ntpath.py
Misc/NEWS.d/next/Windows/2023-08-22-00-36-57.gh-issue-106242.q24ITw.rst [new file with mode: 0644]

index 0444b0f65d10ba52356fdece1d05e07a8286da0b..0246419485da0d41292b76b6965b58930318623b 100644 (file)
@@ -695,6 +695,14 @@ else:
         try:
             path = _getfinalpathname(path)
             initial_winerror = 0
+        except ValueError as ex:
+            # gh-106242: Raised for embedded null characters
+            # In strict mode, we convert into an OSError.
+            # Non-strict mode returns the path as-is, since we've already
+            # made it absolute.
+            if strict:
+                raise OSError(str(ex)) from None
+            path = normpath(path)
         except OSError as ex:
             if strict:
                 raise
@@ -714,6 +722,10 @@ else:
             try:
                 if _getfinalpathname(spath) == path:
                     path = spath
+            except ValueError as ex:
+                # Unexpected, as an invalid path should not have gained a prefix
+                # at any point, but we ignore this error just in case.
+                pass
             except OSError as ex:
                 # If the path does not exist and originally did not exist, then
                 # strip the prefix anyway.
index 75e50d92ed1eb1b07970c4c655cff8fb891d3cdd..88660fc05a1b10bff3a3fe8effc7e506b7dc222c 100644 (file)
@@ -332,6 +332,10 @@ class TestNtpath(NtpathTestCase):
             raise OSError("No free drive letters available")
         self.assertEqual(ntpath.realpath(d), d)
 
+        # gh-106242: Embedded nulls and non-strict fallback to abspath
+        self.assertEqual(ABSTFN + "\0spam",
+                         ntpath.realpath(os_helper.TESTFN + "\0spam", strict=False))
+
     @os_helper.skip_unless_symlink
     @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
     def test_realpath_strict(self):
@@ -342,6 +346,8 @@ class TestNtpath(NtpathTestCase):
         self.addCleanup(os_helper.unlink, ABSTFN)
         self.assertRaises(FileNotFoundError, ntpath.realpath, ABSTFN, strict=True)
         self.assertRaises(FileNotFoundError, ntpath.realpath, ABSTFN + "2", strict=True)
+        # gh-106242: Embedded nulls should raise OSError (not ValueError)
+        self.assertRaises(OSError, ntpath.realpath, ABSTFN + "\0spam", strict=True)
 
     @os_helper.skip_unless_symlink
     @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
diff --git a/Misc/NEWS.d/next/Windows/2023-08-22-00-36-57.gh-issue-106242.q24ITw.rst b/Misc/NEWS.d/next/Windows/2023-08-22-00-36-57.gh-issue-106242.q24ITw.rst
new file mode 100644 (file)
index 0000000..ffe42ec
--- /dev/null
@@ -0,0 +1,4 @@
+Fixes :func:`~os.path.realpath` to behave consistently when passed a path
+containing an embedded null character on Windows. In strict mode, it now
+raises :exc:`OSError` instead of the unexpected :exc:`ValueError`, and in
+non-strict mode will make the path absolute.