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

encode_vec() isn't infallible #1064

Closed
hu55a1n1 opened this issue Dec 20, 2021 · 3 comments · Fixed by #1093
Closed

encode_vec() isn't infallible #1064

hu55a1n1 opened this issue Dec 20, 2021 · 3 comments · Fixed by #1093
Assignees
Labels
bug Something isn't working

Comments

@hu55a1n1
Copy link
Member

hu55a1n1 commented Dec 20, 2021

What went wrong?

tendermint_proto::Protobuf::encode_vec() cannot fail since it uses a vector with required capacity and calls prost::Message::encode() which only fails if the input buffer doesn't have sufficient capacity.

fn encode_vec(&self) -> Result<Vec<u8>, Error> {
    let mut wire = Vec::with_capacity(self.encoded_len());
    self.encode(&mut wire).map(|_| wire)
}

Definition of "done"

tendermint_proto::Protobuf::encode_vec() could simply return a Vec<u8> and unwrap() internally.

fn encode_vec(&self) -> Vec<u8> {
    let mut wire = Vec::with_capacity(self.encoded_len());
    self.encode(&mut wire).map(|_| wire).unwrap()
}
@tony-iqlusion
Copy link
Collaborator

Nit:

self.encode(&mut wire).map(|_| wire).unwrap()

It would be better to use expect:

self.encode(&mut wire).map(|_| wire).expect("buffer size too small")

This clearly states the expected invariant from prost::Message::encode, and using expect over unwrap is generally considered a Rust best practice.

@hu55a1n1 hu55a1n1 self-assigned this Dec 21, 2021
@thanethomson
Copy link
Contributor

@hu55a1n1 are you still going to handle this? I can take over if you don't have time for it.

@hu55a1n1
Copy link
Member Author

Sorry for the delay in getting to this @thanethomson. I started working on this before but got busy with other things. Just opened PR #1093 to resolve this. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants