[commit: ghc] wip/rwbarton-biniface: Boundary condition fixes for Binary (36fdaf6)
git at git.haskell.org
git at git.haskell.org
Mon Feb 20 01:06:43 UTC 2017
Repository : ssh://git@git.haskell.org/ghc
On branch : wip/rwbarton-biniface
Link : http://ghc.haskell.org/trac/ghc/changeset/36fdaf672d7dcafd56ae3710635720310feeb0a0/ghc
>---------------------------------------------------------------
commit 36fdaf672d7dcafd56ae3710635720310feeb0a0
Author: Reid Barton <rwbarton at gmail.com>
Date: Sun Feb 19 20:06:13 2017 -0500
Boundary condition fixes for Binary
>---------------------------------------------------------------
36fdaf672d7dcafd56ae3710635720310feeb0a0
compiler/utils/Binary.hs | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/compiler/utils/Binary.hs b/compiler/utils/Binary.hs
index 4345654..7ed45ed 100644
--- a/compiler/utils/Binary.hs
+++ b/compiler/utils/Binary.hs
@@ -91,6 +91,7 @@ data BinHandle
= BinMem { -- binary data stored in an unboxed array
bh_usr :: UserData, -- sigh, need parameterized modules :-)
_off_r :: !FastMutInt, -- the current offset
+ -- Invariant: _off_r <= _sz_r
_sz_r :: !FastMutInt, -- size of the array (cached)
_arr_r :: !(IORef BinArray) -- the array (bounds: (0,size-1))
}
@@ -164,7 +165,7 @@ tellBin (BinMem _ r _ _) = do ix <- readFastMutInt r; return (BinPtr ix)
seekBin :: BinHandle -> Bin a -> IO ()
seekBin h@(BinMem _ ix_r sz_r _) (BinPtr p) = do
sz <- readFastMutInt sz_r
- if (p >= sz)
+ if (p > sz)
then do expandBin h p; writeFastMutInt ix_r p
else writeFastMutInt ix_r p
@@ -173,7 +174,7 @@ seekBy h@(BinMem _ ix_r sz_r _) off = do
sz <- readFastMutInt sz_r
ix <- readFastMutInt ix_r
let ix' = ix + off
- if (ix' >= sz)
+ if (ix' > sz)
then do expandBin h ix'; writeFastMutInt ix_r ix'
else writeFastMutInt ix_r ix'
@@ -209,7 +210,8 @@ readBinMem filename = do
writeFastMutInt sz_r filesize
return (BinMem noUserData ix_r sz_r arr_r)
--- expand the size of the array to include a specified offset
+-- expand the size of the array to strictly include a specified offset
+-- (i.e., not at EOF, so we can write at least one byte there)
expandBin :: BinHandle -> Int -> IO ()
expandBin (BinMem _ _ sz_r arr_r) off = do
sz <- readFastMutInt sz_r
More information about the ghc-commits
mailing list