@@ -360,23 +360,31 @@ hFlipBit ::
360360 -> Int -- ^ Bit offset
361361 -> m ()
362362hFlipBit hfs h bitOffset = do
363+ -- Check that the bit offset is within the file
364+ fileSize <- hGetSize hfs h
365+ let fileSizeBits = 8 * fileSize
366+ assert (bitOffset >= 0 ) $ pure ()
367+ assert (bitOffset < fromIntegral fileSizeBits) $ pure ()
363368 -- Create an empty buffer initialised to all 0 bits. The buffer must have at
364369 -- least the size of a machine word.
365370 let n = sizeOf (0 :: Word )
366371 buf <- newPinnedByteArray n
367- setByteArray buf 0 n (0 :: Word )
372+ setByteArray buf 0 1 (0 :: Word )
368373 -- Read the bit at the given offset
369374 let (byteOffset, i) = bitOffset `quotRem` 8
370375 bufOff = BufferOffset 0
371376 count = 1
372377 off = AbsOffset (fromIntegral byteOffset)
378+ -- Check that the byte offset is within the file
379+ assert (byteOffset >= 0 ) $ pure ()
380+ assert (byteOffset < fromIntegral fileSize) $ pure ()
381+ assert (i >= 0 && i < 8 ) $ pure ()
373382 void $ hGetBufExactlyAt hfs h buf bufOff count off
374383 -- Flip the bit in memory, and then write it back
375384 let bvec = BitMVec 0 8 buf
376385 flipBit bvec i
377386 void $ hPutBufExactlyAt hfs h buf bufOff count off
378387
379-
380388{- ------------------------------------------------------------------------------
381389 Errors
382390-------------------------------------------------------------------------------}
0 commit comments