diff options
author | Reid Barton <rwbarton@gmail.com> | 2017-02-19 20:06:13 -0500 |
---|---|---|
committer | Reid Barton <rwbarton@gmail.com> | 2017-02-20 00:30:04 -0500 |
commit | 179eafedda7c2063c07858b79d32b7d90e32cb0b (patch) | |
tree | 86d8725aa1d0ae13d5a06822b123ca4cc314366d | |
parent | 7527870438ad664cbb353a8dc4c5ee2b30472f89 (diff) | |
download | haskell-179eafedda7c2063c07858b79d32b7d90e32cb0b.tar.gz |
Boundary condition fixes for Binarywip/rwbarton-biniface
-rw-r--r-- | compiler/utils/Binary.hs | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/compiler/utils/Binary.hs b/compiler/utils/Binary.hs index a1ccee3ae7..4398f1292f 100644 --- a/compiler/utils/Binary.hs +++ b/compiler/utils/Binary.hs @@ -101,6 +101,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)) } @@ -174,7 +175,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 @@ -183,7 +184,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' @@ -219,7 +220,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 |