summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCheng Shao <astrohavoc@gmail.com>2021-12-15 12:21:27 +0000
committerMarge Bot <ben+marge-bot@smart-cactus.org>2021-12-17 21:25:09 -0500
commit94c3ff66a498df778cf32f68198775511b435704 (patch)
tree6fab43fefaec7a71b14597dda8a9b3eb883df1ed
parentabef93f3596b048fbd71afafbd5fddffa877004c (diff)
downloadhaskell-94c3ff66a498df778cf32f68198775511b435704.tar.gz
Binary: make withBinBuffer safe
With this patch, withBinBuffer will construct a ByteString that properly captures the reference to the BinHandle internal MutableByteArray#, making it safe to convert a BinHandle to ByteString and use that ByteString outside the continuation.
-rw-r--r--compiler/GHC/Utils/Binary.hs6
1 files changed, 1 insertions, 5 deletions
diff --git a/compiler/GHC/Utils/Binary.hs b/compiler/GHC/Utils/Binary.hs
index 08e54acbd5..36931b7b1f 100644
--- a/compiler/GHC/Utils/Binary.hs
+++ b/compiler/GHC/Utils/Binary.hs
@@ -169,15 +169,11 @@ setUserData :: BinHandle -> UserData -> BinHandle
setUserData bh us = bh { bh_usr = us }
-- | Get access to the underlying buffer.
---
--- It is quite important that no references to the 'ByteString' leak out of the
--- continuation lest terrible things happen.
withBinBuffer :: BinHandle -> (ByteString -> IO a) -> IO a
withBinBuffer (BinMem _ ix_r _ arr_r) action = do
arr <- readIORef arr_r
ix <- readFastMutInt ix_r
- withForeignPtr arr $ \ptr ->
- BS.unsafePackCStringLen (castPtr ptr, ix) >>= action
+ action $ BS.fromForeignPtr arr 0 ix
---------------------------------------------------------------