Skip to content

Commit

Permalink
Fail on end-group markers with the wrong tag number. (google#307)
Browse files Browse the repository at this point in the history
* Add a test of group end tag mismatch which currently fails.
  • Loading branch information
judah authored and blackgnezdo committed Jan 28, 2019
1 parent 930749d commit 2c9972d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Store repeated fields as `Vector`s, and expose the internal representation
via new `vec'*` lenses. Use `Vector`s for more efficient
encoding/decoding.
- Fix the parser to fail on end-group markers with the wrong tag number (#282).

## v0.4.0.1
- Bump the dependency on `base` and `containers` to support `ghc-8.6.1`.
Expand Down
35 changes: 30 additions & 5 deletions src/Data/ProtoLens/Compiler/Generate/Encoding.hs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ parseTagCases ::
parseTagCases loop x info =
concatMap (parseFieldCase loop x) allFields
-- TODO: currently we ignore unknown fields.
++ [unknownFieldCase loop x]
++ [unknownFieldCase info loop x]
where
allFields = messageFields info
-- Cases of a oneof are decoded like optional oneof fields.
Expand Down Expand Up @@ -358,15 +358,40 @@ parseFieldCase loop x f = case plainFieldKind f of
]

unknownFieldCase ::
(ParseState Exp -> Exp) -> ParseState Exp -> Alt
unknownFieldCase loop x = wire --> do'
MessageInfo Name -> (ParseState Exp -> Exp) -> ParseState Exp -> Alt
{-
wire -> do
!y <- parseTaggedValueFromWire wire
-- Omitted if not a group:
case y of
TaggedValue utag EndGroup
-> fail ("Mismatched group-end tag number " ++ show utag)
_ -> return ()
loop (over unknownFields (\!t -> y:t) x) ...
-}
unknownFieldCase info loop x = wire --> (do' $
[ bangPat y <-- "Data.ProtoLens.Encoding.Wire.parseTaggedValueFromWire" @@ wire
, stmt . loop . updateParseState (over' unknownFields' (cons @@ y))
$ x
]
++
[ stmt $ case' y
[ pApp "Data.ProtoLens.Encoding.Wire.TaggedValue"
[utag, "Data.ProtoLens.Encoding.Wire.EndGroup"]
--> "Prelude.fail" @@
("Prelude.++"
@@ stringExp "Mismatched group-end tag number "
@@ ("Prelude.show" @@ utag))
, pWildCard --> "Prelude.return" @@ unit
]
| Just _ <- [groupFieldNumber info]
]
++
[ stmt . loop . updateParseState (over' unknownFields' (cons @@ y))
$ x
])
where
wire = "wire"
y = "y"
utag = "utag"

-- | An expression of type "b -> a -> a", corresponding to a Lens a b
-- for this field.
Expand Down

0 comments on commit 2c9972d

Please sign in to comment.