Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A (Try)FromBytes #[length] field attribute #1328

Open
jswrenn opened this issue May 20, 2024 · 2 comments
Open

A (Try)FromBytes #[length] field attribute #1328

jswrenn opened this issue May 20, 2024 · 2 comments
Labels
customer-request Documents customer requests.

Comments

@jswrenn
Copy link
Collaborator

jswrenn commented May 20, 2024

See also: #5 (comment)

Addresses #1289. Discussion of alternative solutions should occur there, or in their own issues.

Many formats include a length field in their header that describes the length of a variable-sized body; e.g., UDP:

#[derive(FromBytes, KnownLayout, Immutable)]
#[repr(C)]
struct Packet {
    src_port: [u8; 2],
    dst_port: [u8; 2],
    length: [u8; 2],
    checksum: [u8; 2],
    body: [u8],
}

Such types are inconvenient to correctly parse in zerocopy; the length must first be parsed, and only then FromBytes::try_ref_from_prefix_with_elems can be invoked with that length. Alternatively, the entire buffer can be parsed with try_ref_from_prefix, and then truncated thereafter. These approaches are inconvenient.

We could potentially simplify this with a #[length] field attribute; e.g.:

#[derive(FromBytes, KnownLayout, Immutable)]
#[repr(C)]
struct Packet {
    src_port: [u8; 2],
    dst_port: [u8; 2],
    #[zerocopy::length]
    length: [u8; 2],
    checksum: [u8; 2],
    body: [u8],
}

...such that FromBytes::ref_from_prefix would respect the length attribute.

Some considerations:

  • This attribute should accept a function for cases in which the length in bytes must be computed
  • This approach should also work on TryFromBytes, where excess data might be invalid
  • Should we attempt to support the case where the length is factored into a different, fixed-sized header type?
@jswrenn jswrenn changed the title #[length] attribute A (Try)FromBytes #[length] field attribute May 20, 2024
@jswrenn jswrenn added the customer-request Documents customer requests. label May 20, 2024
@kupiakos
Copy link
Contributor

If the design in #1289 (comment) were to be implemented, this attribute could have the validator always return ValidIfTruncatedTo(length_specified) assuming the other fields are validated.

@kupiakos
Copy link
Contributor

kupiakos commented May 20, 2024

This should also consider:

  • Whether the length is in bytes or number of elements, and
  • Whether this length includes the size of the header itself or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customer-request Documents customer requests.
Projects
None yet
Development

No branches or pull requests

2 participants