[commit: unix] ghc-7.6: Fix a memory corruption issue in putEnv (fixes #7342) (b95a003)
Ian Lynagh
igloo at earth.li
Sun Dec 2 02:41:46 CET 2012
Repository : ssh://darcs.haskell.org//srv/darcs/packages/unix
On branch : ghc-7.6
http://hackage.haskell.org/trac/ghc/changeset/b95a003b20436863ef2f5fc01e6cd77f5bef94a0
>---------------------------------------------------------------
commit b95a003b20436863ef2f5fc01e6cd77f5bef94a0
Author: Simon Hengel <sol at typeful.net>
Date: Thu Oct 18 00:27:42 2012 +0200
Fix a memory corruption issue in putEnv (fixes #7342)
>---------------------------------------------------------------
System/Posix/Env.hsc | 17 ++++++++++++++++-
1 files changed, 16 insertions(+), 1 deletions(-)
diff --git a/System/Posix/Env.hsc b/System/Posix/Env.hsc
index be4711b..c30a20e 100644
--- a/System/Posix/Env.hsc
+++ b/System/Posix/Env.hsc
@@ -38,6 +38,10 @@ import Foreign.Ptr
import Foreign.Storable
import Control.Monad
import Data.Maybe (fromMaybe)
+#ifdef __GLASGOW_HASKELL__
+import GHC.IO.Encoding (getFileSystemEncoding)
+import qualified GHC.Foreign as GHC
+#endif
#if __GLASGOW_HASKELL__ > 700
import System.Posix.Internals (withFilePath, peekFilePath)
#elif __GLASGOW_HASKELL__ > 611
@@ -53,6 +57,13 @@ peekFilePath :: CString -> IO FilePath
peekFilePath = peekCString
#endif
+newFilePath :: String -> IO CString
+#ifdef __GLASGOW_HASKELL__
+newFilePath s = getFileSystemEncoding >>= (`GHC.newCString` s)
+#else
+newFilePath = newCString
+#endif
+
-- |'getEnv' looks up a variable in the environment.
getEnv :: String -> IO (Maybe String)
@@ -137,7 +148,11 @@ unsetEnv name = putEnv (name ++ "=")
-- and is equivalent to @setEnv(key,value,True{-overwrite-})@.
putEnv :: String -> IO ()
-putEnv keyvalue = withFilePath keyvalue $ \s ->
+putEnv keyvalue = newFilePath keyvalue >>= \s ->
+ -- IMPORTANT: Do not free `s` after calling putenv!
+ --
+ -- According to SUSv2, the string passed to putenv becomes part of the
+ -- enviroment.
throwErrnoIfMinus1_ "putenv" (c_putenv s)
foreign import ccall unsafe "putenv"
More information about the Cvs-libraries
mailing list