Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Replace unwrap() in UntrustedRlp Display impl #7548

Closed
wants to merge 1 commit into from

Conversation

lght
Copy link

@lght lght commented Jan 13, 2018

Use match { ... } in place of unwrap().

Use `match { ... }` in place of `unwrap()`.
@parity-cla-bot
Copy link

It looks like @lght signed our Contributor License Agreement. 👍

Many thanks,

Parity Technologies CLA Bot

@lght lght added A0-pleasereview 🤓 Pull request needs code review. M4-core ⛓ Core client code / Rust. labels Jan 13, 2018
Ok(Prototype::List(len)) => {
write!(f, "[")?;
for i in 0..len-1 {
let _ = match self.at(i) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disregarding errors while writing is bad. why not simply use expect instead of rewriting? Or even rewrite using for (i, item) in self.iter().enumerate() in the list case?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not simply use expect instead of rewriting?

The point of these fixes was to remove panics from UntrustedRlp handling, using expect would just reintroduce the same problem.

I was following the pattern from other error conditions in this section of code. Definitely open to suggestions for how to handle the errors without panicking.

Copy link
Contributor

@rphmeier rphmeier Jan 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, then I would recommend using an iterator as it will be cleaner:

write!(f, "[")?;
for item in self.iter() {
    write!(f, "{}", item)?;
}
write!(f, "]")?;

both approaches are only as broken as the implementation of fn at which would be pretty good to fuzz.

Copy link
Author

@lght lght Jan 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like the conciseness of your solution. It does have the drawback of making the source of the error more obscure (now the .at() source is hidden behind the .iter() implementation).

Should there be a more specific error raised by untrusted_rlp.at() to make the error's origin more clear? Maybe an error type like "RlpItemAccessError" or "RlpItemDecodeErrorAtIndex"?

Currently the error resolves to RlpExpectedToBeData, which doesn't point to the .at() implementation at all. I was only able to track down the origin after looking through the code for a couple hours. Had the .at().unwrap() not been in the Display impl, it may have taken much longer.

fn at which would be pretty good to fuzz

I'm open to getting started on that, adding to the work started by @onicslabs.

@rphmeier rphmeier added A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. and removed A0-pleasereview 🤓 Pull request needs code review. labels Jan 14, 2018
@5chdn 5chdn added this to the 1.10 milestone Jan 16, 2018
@5chdn 5chdn modified the milestones: 1.10, 1.11 Jan 23, 2018
@debris debris closed this Feb 6, 2018
@5chdn 5chdn deleted the fix-rlp-display-error branch November 1, 2018 10:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. M4-core ⛓ Core client code / Rust.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants