Skip to content

Commit 02e7aa7

Browse files
committed
Add assertions to hFlipBit
1 parent ed4462c commit 02e7aa7

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

test/Test/Util/FS.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,11 @@ hFlipBit ::
360360
-> Int -- ^ Bit offset
361361
-> m ()
362362
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 ()
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)
@@ -370,13 +375,16 @@ hFlipBit hfs h bitOffset = do
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

Comments
 (0)