diff options
author | Joey Sacchini <joey@sacchini.net> | 2020-10-08 13:08:49 -0400 |
---|---|---|
committer | Joey Sacchini <joey@sacchini.net> | 2020-10-08 13:08:49 -0400 |
commit | 2b345b92b0925cd138c171ee5971a6e977467483 (patch) | |
tree | 0fbefde570f78bee1f0cebb05efbb7ee4de9103e | |
parent | 83d0a8a0d6690cafba319852e0c27981bb861109 (diff) | |
download | mcproto-rs-2b345b92b0925cd138c171ee5971a6e977467483.tar.gz mcproto-rs-2b345b92b0925cd138c171ee5971a6e977467483.tar.bz2 mcproto-rs-2b345b92b0925cd138c171ee5971a6e977467483.zip |
error when data is not consumed fully
-rw-r--r-- | src/protocol.rs | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/src/protocol.rs b/src/protocol.rs index 98b9740..816d20b 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -36,6 +36,7 @@ pub trait Packet<I: PacketIdentifier>: Serialize { pub enum PacketErr { UnknownId(i32), DeserializeFailed(DeserializeErr), + ExtraData(Vec<u8>), } impl fmt::Display for PacketErr { @@ -43,7 +44,8 @@ impl fmt::Display for PacketErr { use PacketErr::*; match self { UnknownId(id) => f.write_fmt(format_args!("unknown packet id {:?}", id)), - DeserializeFailed(err) => f.write_fmt(format_args!("failed to deserialize packet: {:?}", err)) + DeserializeFailed(err) => f.write_fmt(format_args!("failed to deserialize packet: {:?}", err)), + ExtraData(data) => f.write_fmt(format_args!("extra data unparsed at end of packet: {:?}", data)) } } } @@ -203,7 +205,15 @@ macro_rules! define_protocol { let data = raw.data; match (id.id, id.state, id.direction) { - $(($id, $state, $direction) => Ok($nam($body::mc_deserialize(data).map_err(DeserializeFailed)?.value))),*, + $(($id, $state, $direction) => Ok($nam({ + let deserialized = $body::mc_deserialize(data).map_err(DeserializeFailed)?; + let rest = deserialized.data; + if !rest.is_empty() { + return Err(ExtraData(rest.to_vec())) + } + + deserialized.value + }))),*, other => Err(UnknownId(other.0)), } } |