[Git][ghc/ghc][wip/T13660] base: Reject NUL codepoints in Windows FilePaths

Ben Gamari (@bgamari) gitlab at gitlab.haskell.org
Sun Mar 26 03:50:07 UTC 2023



Ben Gamari pushed to branch wip/T13660 at Glasgow Haskell Compiler / GHC


Commits:
346f1039 by Ben Gamari at 2023-03-25T23:50:01-04:00
base: Reject NUL codepoints in Windows FilePaths

Similarly to POSIX, Windows rejects NULs in FilePaths. Unlike POSIX, we
can check the `FilePath` rather than its encoding since all paths are
UTF-16 on Windows.

Fixes #13660 on Windows.

- - - - -


1 changed file:

- libraries/base/System/Posix/Internals.hs


Changes:

=====================================
libraries/base/System/Posix/Internals.hs
=====================================
@@ -164,13 +164,32 @@ fdGetMode fd = do
 
 #if defined(mingw32_HOST_OS)
 withFilePath :: FilePath -> (CWString -> IO a) -> IO a
-withFilePath = withCWString
+withFilePath fp f = do
+    checkForInteriorNuls fp
+    withCWString fp f
 
 newFilePath :: FilePath -> IO CWString
-newFilePath = newCWString
+newFilePath fp = do
+    checkForInteriorNuls fp
+    newCWString fp
 
 peekFilePath :: CWString -> IO FilePath
 peekFilePath = peekCWString
+
+-- | Check a 'FilePath' for internal NUL codepoints as these are
+-- disallowed in Windows filepaths. See #13660.
+checkForInteriorNuls :: FilePath -> IO ()
+checkForInteriorNuls fp = when ('\0' `elem` fp) (ioError err)
+  where
+    err =
+        IOError
+          { ioe_handle = Nothing
+          , ioe_type = InvalidArgument
+          , ioe_location = "checkForInteriorNuls"
+          , ioe_description = "Windows filepaths must not contain internal NUL codepoints."
+          , ioe_errno = Nothing
+          , ioe_filename = Just fp
+          }
 #else
 
 withFilePath :: FilePath -> (CString -> IO a) -> IO a



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/346f103916e49a2c1cb982729ff419dbe86ea9e6

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/346f103916e49a2c1cb982729ff419dbe86ea9e6
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20230325/07864ec0/attachment-0001.html>


More information about the ghc-commits mailing list