Skip to content

Commit

Permalink
Validate struct #[canonical(prefix)] when decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Dentosal committed Aug 29, 2023
1 parent f96ef89 commit 4af7c63
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion fuel-derive/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn parse_attrs(s: &synstructure::Structure) -> HashMap<String, TokenStream> {

/// Pop-level `canonical` attributes for a struct
pub struct StructAttrs {
/// The struct is prefixed with the given byte.
/// The struct is prefixed with the given word.
/// Useful with`#[canonical(inner_discriminant)]`.
pub prefix: Option<TokenStream>,
}
Expand Down
11 changes: 7 additions & 4 deletions fuel-derive/src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ fn deserialize_struct(s: &mut synstructure::Structure) -> TokenStream2 {
}
});

let remove_prefix = if StructAttrs::parse(s).prefix.is_some() {
quote! {
<u64 as ::fuel_types::canonical::Deserialize>::decode_static(buffer)?;
}
let remove_prefix = if let Some(expected_prefix) = StructAttrs::parse(s).prefix {
quote! {{
let prefix = <u64 as ::fuel_types::canonical::Deserialize>::decode_static(buffer)?;
if prefix.try_into() != Ok(#expected_prefix) {
return ::core::result::Result::Err(::fuel_types::canonical::Error::InvalidPrefix)
}
}}
} else {
quote! {}
};
Expand Down
2 changes: 2 additions & 0 deletions fuel-types/src/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub enum Error {
BufferIsTooShort,
/// Got unknown enum's discriminant.
UnknownDiscriminant,
/// Struct prefix was invalid.
InvalidPrefix,
/// Wrong align.
WrongAlign,
/// Allocation too large to be correct.
Expand Down

0 comments on commit 4af7c63

Please sign in to comment.