diff options
author | Bartosz Nitka <niteria@gmail.com> | 2016-10-06 05:40:24 -0700 |
---|---|---|
committer | Bartosz Nitka <niteria@gmail.com> | 2016-10-07 03:07:13 -0700 |
commit | e41b9c614984b63c4660018cecde682453e083e5 (patch) | |
tree | 21237358709a4b236b687dcc5187896695aef245 /libraries/base/GHC/Foreign.hs | |
parent | cbe11d5fefefce518c246b470350a5a3bf8efbd6 (diff) | |
download | haskell-e41b9c614984b63c4660018cecde682453e083e5.tar.gz |
Fix memory leak from #12664
This fixes the leak with `setProgArgv`. The problem was
that `setProgArgv` would not free the objects pointed
to by `prog_argc`, `prog_argv` when the globals were
changed resulting in a leak.
The only strictly necessary change is in `rts/RtsFlags.c`, but
the code in `System.Environment` was a bit confusing and not
exception safe, so I refactored it.
Test Plan: ./validate
Reviewers: simonmar, ezyang, austin, hvr, bgamari, erikd
Subscribers: thomie
Differential Revision: https://phabricator.haskell.org/D2576
GHC Trac Issues: #12664
Diffstat (limited to 'libraries/base/GHC/Foreign.hs')
-rw-r--r-- | libraries/base/GHC/Foreign.hs | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/libraries/base/GHC/Foreign.hs b/libraries/base/GHC/Foreign.hs index e8553d8061..7d2f915920 100644 --- a/libraries/base/GHC/Foreign.hs +++ b/libraries/base/GHC/Foreign.hs @@ -32,6 +32,7 @@ module GHC.Foreign ( -- withCString, withCStringLen, + withCStringsLen, charIsRepresentable, ) where @@ -134,6 +135,23 @@ withCString enc s act = withEncodedCString enc True s $ \(cp, _sz) -> act cp withCStringLen :: TextEncoding -> String -> (CStringLen -> IO a) -> IO a withCStringLen enc = withEncodedCString enc False +-- | Marshal a list of Haskell strings into an array of NUL terminated C strings +-- using temporary storage. +-- +-- * the Haskell strings may /not/ contain any NUL characters +-- +-- * the memory is freed when the subcomputation terminates (either +-- normally or via an exception), so the pointer to the temporary +-- storage must /not/ be used after this. +-- +withCStringsLen :: TextEncoding + -> [String] + -> (Int -> Ptr CString -> IO a) + -> IO a +withCStringsLen enc strs f = go [] strs + where + go cs (s:ss) = withCString enc s $ \c -> go (c:cs) ss + go cs [] = withArrayLen (reverse cs) f -- | Determines whether a character can be accurately encoded in a 'CString'. -- |