Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 470b8e0

Browse files
committed
[CSL-585] Fix for attributes, WIP
1 parent 4251a75 commit 470b8e0

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/Pos/Data/Attributes.hs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,33 @@ getAttributes :: (Word8 -> h -> Maybe (Get h))
7171
-> Word32
7272
-> h
7373
-> Get (Attributes h)
74-
getAttributes keyGetMapper maxLen initData = do
75-
totalLen <- getWord32be
74+
getAttributes keyGetMapper (fromIntegral -> maxLen) initData = do
75+
-- CSL-594 TODO use varint
76+
totalLen <- fromIntegral <$> getWord32be
7677
when (maxLen > 0 && totalLen > maxLen) $
7778
fail $ "Attributes: wrong totalLen " ++ show totalLen
7879
++ " (maxLen=" ++ show maxLen ++ ")"
7980
read1 <- G.bytesRead
80-
let readWhileKnown dat = ifM G.isEmpty (return dat) $ do
81+
let cond = orM [ G.isEmpty
82+
, (\r -> r - read1 >= totalLen) <$> G.bytesRead ]
83+
readWhileKnown dat = ifM cond (return dat) $ do
8184
key <- G.lookAhead getWord8
8285
case keyGetMapper key dat of
8386
Nothing -> return dat
8487
Just gh -> getWord8 >> gh >>= readWhileKnown
8588
attrData <- readWhileKnown initData
8689
read <- flip (-) read1 <$> G.bytesRead
87-
when (read > fromIntegral totalLen) $
90+
when (read > totalLen) $
8891
fail $ "Attributes: wrong total length field value, deserialized "
8992
++ show read ++ " bytes, totalLen=" ++ show totalLen
90-
attrRemain <- getByteString $ fromIntegral totalLen - fromIntegral read
93+
attrRemain <- getByteString $ fromIntegral $ totalLen - read
9194
return $ Attributes {..}
9295

9396
-- | Generate 'Put' given the way to serialize inner attribute value
9497
-- into set of keys and values.
9598
putAttributes :: (h -> [(Word8, ByteString)]) -> Attributes h -> Put
9699
putAttributes putMapper Attributes {..} = do
100+
-- CSL-594 TODO use varint
97101
putWord32be totalLen
98102
mapM_ putAttr kvs
99103
putByteString attrRemain

0 commit comments

Comments
 (0)