@@ -360,23 +360,31 @@ hFlipBit ::
360
360
-> Int -- ^ Bit offset
361
361
-> m ()
362
362
hFlipBit 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 ()
363
368
-- Create an empty buffer initialised to all 0 bits. The buffer must have at
364
369
-- least the size of a machine word.
365
370
let n = sizeOf (0 :: Word )
366
371
buf <- newPinnedByteArray n
367
- setByteArray buf 0 n (0 :: Word )
372
+ setByteArray buf 0 1 (0 :: Word )
368
373
-- Read the bit at the given offset
369
374
let (byteOffset, i) = bitOffset `quotRem` 8
370
375
bufOff = BufferOffset 0
371
376
count = 1
372
377
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 ()
373
382
void $ hGetBufExactlyAt hfs h buf bufOff count off
374
383
-- Flip the bit in memory, and then write it back
375
384
let bvec = BitMVec 0 8 buf
376
385
flipBit bvec i
377
386
void $ hPutBufExactlyAt hfs h buf bufOff count off
378
387
379
-
380
388
{- ------------------------------------------------------------------------------
381
389
Errors
382
390
-------------------------------------------------------------------------------}
0 commit comments