Skip to content

Commit

Permalink
fix(common): Session::deserialize better error for empty payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Sep 28, 2024
1 parent f2a4fe3 commit e031c7a
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pubky-common/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ impl Session {
}

pub fn deserialize(bytes: &[u8]) -> Result<Self> {
if bytes.is_empty() {
return Err(Error::EmptyPayload);
}

if bytes[0] > 0 {
return Err(Error::UnknownVersion);
}
Expand All @@ -82,6 +86,8 @@ pub type Result<T> = core::result::Result<T, Error>;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Empty payload")]
EmptyPayload,
#[error("Unknown version")]
UnknownVersion,
#[error(transparent)]
Expand Down Expand Up @@ -123,4 +129,9 @@ mod tests {

assert_eq!(deseiralized, session)
}

#[test]
fn deserialize() {
let deseiralized = Session::deserialize(&[]).unwrap();
}
}

0 comments on commit e031c7a

Please sign in to comment.