File tree Expand file tree Collapse file tree 1 file changed +9
-1
lines changed Expand file tree Collapse file tree 1 file changed +9
-1
lines changed Original file line number Diff line number Diff line change @@ -360,6 +360,11 @@ 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 )
@@ -370,13 +375,16 @@ hFlipBit hfs h bitOffset = do
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
-------------------------------------------------------------------------------}
You can’t perform that action at this time.
0 commit comments