Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix corrupted config file header for non-ASCII package names #5804

Merged
merged 2 commits into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cabal/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
supports fully static linking;
[`glibc` has some issues](https://sourceware.org/glibc/wiki/FAQ#Even_statically_linked_programs_need_some_shared_libraries_which_is_not_acceptable_for_me.__What_can_I_do.3F)
with fully static linking.
* Fix corrupted config file header for non-ASCII package names
([2557](https://github.com/haskell/cabal/issues/2557)).
* Extend `Distribution.Simple.Utils.rewriteFileEx` from ASCII to UTF-8 encoding.

----

Expand Down
4 changes: 2 additions & 2 deletions Cabal/Distribution/Simple/Configure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ parseHeader header = case BLC8.words header of
["Saved", "package", "config", "for", pkgId, "written", "by", cabalId,
"using", compId] ->
fromMaybe (throw ConfigStateFileBadHeader) $ do
_ <- simpleParsec (BLC8.unpack pkgId) :: Maybe PackageIdentifier
_ <- simpleParsec (fromUTF8LBS pkgId) :: Maybe PackageIdentifier
cabalId' <- simpleParsec (BLC8.unpack cabalId)
compId' <- simpleParsec (BLC8.unpack compId)
return (cabalId', compId')
Expand All @@ -286,7 +286,7 @@ showHeader :: PackageIdentifier -- ^ The processed package.
-> ByteString
showHeader pkgId = BLC8.unwords
[ "Saved", "package", "config", "for"
, BLC8.pack $ prettyShow pkgId
, toUTF8LBS $ prettyShow pkgId
, "written", "by"
, BLC8.pack $ prettyShow currentCabalId
, "using"
Expand Down
19 changes: 11 additions & 8 deletions Cabal/Distribution/Simple/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ import Control.Concurrent.MVar
( newEmptyMVar, putMVar, takeMVar )
import Data.Typeable
( cast )
import qualified Data.ByteString.Lazy.Char8 as BS.Char8
import qualified Data.ByteString.Lazy as BS

import System.Directory
( Permissions(executable), getDirectoryContents, getPermissions
Expand Down Expand Up @@ -1401,19 +1401,22 @@ rewriteFile = rewriteFileEx normal
-- the same as the existing content then leave the file as is so that we do not
-- update the file's modification time.
--
-- NB: the file is assumed to be ASCII-encoded.
-- NB: Before Cabal-3.0 the file content was assumed to be
-- ASCII-representable. Since Cabal-3.0 the file is assumed to be
-- UTF-8 encoded.
rewriteFileEx :: Verbosity -> FilePath -> String -> IO ()
rewriteFileEx verbosity path newContent =
flip catchIO mightNotExist $ do
existingContent <- annotateIO verbosity $ readFile path
_ <- evaluate (length existingContent)
unless (existingContent == newContent) $
existingContent <- annotateIO verbosity $ BS.readFile path
_ <- evaluate (BS.length existingContent)
unless (existingContent == newContent') $
annotateIO verbosity $
writeFileAtomic path (BS.Char8.pack newContent)
writeFileAtomic path newContent'
where
newContent' = toUTF8LBS newContent

mightNotExist e | isDoesNotExistError e
= annotateIO verbosity $ writeFileAtomic path
(BS.Char8.pack newContent)
= annotateIO verbosity $ writeFileAtomic path newContent'
| otherwise
= ioError e

Expand Down