Skip to content

Commit 90f5739

Browse files
committed
Do not generate duplicate keys in CBOR maps.
See https://www.rfc-editor.org/rfc/rfc7049#section-3.7
1 parent 8719db7 commit 90f5739

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

cuddle.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 3.4
22
name: cuddle
3-
version: 0.2.0.0
3+
version: 0.2.0.1
44
synopsis: CDDL Generator and test utilities
55

66
-- description:

src/Codec/CBOR/Cuddle/CBOR/Gen.hs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import Codec.CBOR.Write qualified as CBOR
3333
import Control.Monad (join, replicateM, (<=<))
3434
import Control.Monad.Reader (Reader, runReader)
3535
import Control.Monad.State.Strict (StateT, runStateT)
36+
import Data.Bifunctor (second)
3637
import Data.ByteString (ByteString)
3738
import Data.ByteString.Base16 qualified as Base16
3839
import Data.Functor ((<&>))
@@ -55,7 +56,6 @@ import System.Random.Stateful
5556
randomM,
5657
uniformByteStringM,
5758
)
58-
import Data.Bifunctor (second)
5959

6060
--------------------------------------------------------------------------------
6161
-- Generator infrastructure
@@ -268,7 +268,13 @@ genForCTree (CTree.Postlude pt) = S <$> genPostlude pt
268268
genForCTree (CTree.Map nodes) = do
269269
items <- pairTermList . flattenWrappedList <$> traverse genForNode nodes
270270
case items of
271-
Just ts -> pure . S $ TMap ts
271+
Just ts ->
272+
let -- De-duplicate keys in the map.
273+
-- Per RFC7049:
274+
-- >> A map that has duplicate keys may be well-formed, but it is not
275+
-- >> valid, and thus it causes indeterminate decoding
276+
tsNodup = Map.toList $ Map.fromList ts
277+
in pure . S $ TMap tsNodup
272278
Nothing -> error "Single terms in map context"
273279
genForCTree (CTree.Array nodes) = do
274280
items <- singleTermList . flattenWrappedList <$> traverse genForNode nodes
@@ -308,12 +314,12 @@ genForCTree (CTree.Control op target controller) = do
308314
tt <- resolveIfRef target
309315
ct <- resolveIfRef controller
310316
case (op, ct) of
311-
(CtlOp.Le, CTree.Literal (VUInt n)) -> case tt of
312-
CTree.Postlude PTUInt -> S. TInteger <$> genUniformRM (0, fromIntegral n)
317+
(CtlOp.Le, CTree.Literal (VUInt n)) -> case tt of
318+
CTree.Postlude PTUInt -> S . TInteger <$> genUniformRM (0, fromIntegral n)
313319
_ -> error "Cannot apply le operator to target"
314320
(CtlOp.Le, _) -> error $ "Invalid controller for .le operator: " <> show controller
315-
(CtlOp.Lt, CTree.Literal (VUInt n)) -> case tt of
316-
CTree.Postlude PTUInt -> S. TInteger <$> genUniformRM (0, fromIntegral n - 1)
321+
(CtlOp.Lt, CTree.Literal (VUInt n)) -> case tt of
322+
CTree.Postlude PTUInt -> S . TInteger <$> genUniformRM (0, fromIntegral n - 1)
317323
_ -> error "Cannot apply lt operator to target"
318324
(CtlOp.Lt, _) -> error $ "Invalid controller for .lt operator: " <> show controller
319325
(CtlOp.Size, CTree.Literal (VUInt n)) -> case tt of

0 commit comments

Comments
 (0)