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

Builder: avoid unsound buffer reuse #691

Merged
merged 1 commit into from
Sep 18, 2024
Merged
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
8 changes: 4 additions & 4 deletions Data/ByteString/Builder/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ buildStepToCIOS (AllocationStrategy nextBuffer bufSize trim) =
-- Checking for empty case avoids allocating 'n-1' empty
-- buffers for 'n' insertChunkH right after each other.
if isEmpty
then fill nextStep (Buffer fpbuf (BufferRange pbuf pe))
then fill nextStep buf
else do buf' <- nextBuffer (Just (buf, bufSize))
fill nextStep buf'

Expand All @@ -1208,9 +1208,9 @@ buildStepToCIOS (AllocationStrategy nextBuffer bufSize trim) =
| trim chunkSize size = do
bs <- S.createFp chunkSize $ \fpbuf' ->
S.memcpyFp fpbuf' fpbuf chunkSize
-- Instead of allocating a new buffer after trimming,
-- we re-use the old buffer and consider it empty.
return $ Yield1 bs (mkCIOS True)
-- It is not safe to re-use the old buffer (see #690),
-- so we allocate a new buffer after trimming.
return $ Yield1 bs (mkCIOS False)
| otherwise =
return $ Yield1 (S.BS fpbuf chunkSize) (mkCIOS False)
where
Expand Down
Loading