Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix out-of-bounds array access in hFlipBit. #551

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions test/Test/FS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,19 @@ createFile hfs p = withFile hfs p (WriteMode MustBeNew) $ \_ -> pure ()
data WithBitOffset a = WithBitOffset Int a
deriving stock Show

bitLength :: BS.ByteString -> Int
bitLength bs = BS.length bs * 8

instance Arbitrary (WithBitOffset ByteString) where
arbitrary = do
bs <- arbitrary `suchThat` (\bs -> BS.length bs > 0)
bitOffset <- chooseInt (0, BS.length bs - 1)
bitOffset <- chooseInt (0, bitLength bs - 1)
pure $ WithBitOffset bitOffset bs
shrink (WithBitOffset bitOffset bs) =
[ WithBitOffset bitOffset' bs'
| bs' <- shrink bs
, BS.length bs' > 0
, let bitOffset' = max 0 $ min (BS.length bs' - 1) bitOffset
, let bitOffset' = max 0 $ min (bitLength bs' - 1) bitOffset
] ++ [
WithBitOffset bitOffset' bs
| bitOffset' <- max 0 <$> shrink bitOffset
Expand Down
12 changes: 10 additions & 2 deletions test/Test/Util/FS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -360,23 +360,31 @@ hFlipBit ::
-> Int -- ^ Bit offset
-> m ()
hFlipBit hfs h bitOffset = do
-- Check that the bit offset is within the file
fileSize <- hGetSize hfs h
let fileSizeBits = 8 * fileSize
assert (bitOffset >= 0) $ pure ()
assert (bitOffset < fromIntegral fileSizeBits) $ pure ()
-- Create an empty buffer initialised to all 0 bits. The buffer must have at
-- least the size of a machine word.
let n = sizeOf (0 :: Word)
buf <- newPinnedByteArray n
setByteArray buf 0 n (0 :: Word)
setByteArray buf 0 1 (0 :: Word)
-- Read the bit at the given offset
let (byteOffset, i) = bitOffset `quotRem` 8
bufOff = BufferOffset 0
count = 1
off = AbsOffset (fromIntegral byteOffset)
-- Check that the byte offset is within the file
assert (byteOffset >= 0) $ pure ()
assert (byteOffset < fromIntegral fileSize) $ pure ()
assert (i >= 0 && i < 8) $ pure ()
void $ hGetBufExactlyAt hfs h buf bufOff count off
-- Flip the bit in memory, and then write it back
let bvec = BitMVec 0 8 buf
flipBit bvec i
void $ hPutBufExactlyAt hfs h buf bufOff count off


{-------------------------------------------------------------------------------
Errors
-------------------------------------------------------------------------------}
Expand Down
Loading