-
Notifications
You must be signed in to change notification settings - Fork 120
feat(relay): Add a bounded deserializer for (prost) protobuffers #6295
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
Open
klochek
wants to merge
3
commits into
master
Choose a base branch
from
christopherklochek/relay_serializer_proto
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,7 @@ | |
|
|
||
| #![warn(missing_docs)] | ||
|
|
||
| mod meter; | ||
|
|
||
| pub mod prost; | ||
| pub mod serde; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| //! The operation budget shared by every bounded deserializer in this crate. | ||
|
|
||
| use std::fmt; | ||
|
|
||
| /// A budget for the ops a single deserialization is allowed to spend. | ||
| pub(crate) struct Meter { | ||
| limit: usize, | ||
| remaining: usize, | ||
| exceeded: bool, | ||
| } | ||
|
|
||
| impl Meter { | ||
| /// Creates a meter which allows spending at most `limit` operations. | ||
| pub fn new(limit: usize) -> Self { | ||
| Self { | ||
| limit, | ||
| remaining: limit, | ||
| exceeded: false, | ||
| } | ||
| } | ||
|
|
||
| /// Returns the number of ops spent. | ||
| pub fn spent(&self) -> usize { | ||
| self.limit - self.remaining | ||
| } | ||
|
|
||
| /// Returns true if we've exceeded our budget. | ||
| pub fn exceeded(&self) -> bool { | ||
| self.exceeded | ||
| } | ||
|
|
||
| /// Tries to charge `amount` operations to the budget. If we exceed, we return an error, | ||
| /// set the remaining budget to 0, and mark the budget as exceeded. | ||
| pub fn spend(&mut self, amount: usize) -> Result<(), LimitExceeded> { | ||
| match self.remaining.checked_sub(amount) { | ||
| Some(remaining) => { | ||
| self.remaining = remaining; | ||
| Ok(()) | ||
| } | ||
| None => { | ||
| self.remaining = 0; | ||
| self.exceeded = true; | ||
| Err(LimitExceeded) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// The error produced when a [`Meter`] runs out of budget. | ||
| pub(crate) struct LimitExceeded; | ||
|
|
||
| impl fmt::Display for LimitExceeded { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| write!(f, "deserialization exceeds the operation budget") | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| //! Deserialization routines for prost. | ||
| //! This implements a scanner to record the number of "operations" needed to decode a proto, | ||
| //! allowing a caller to enforce a hard limit on how much work to be done. | ||
| mod scan; | ||
|
|
||
| pub use scan::Error; | ||
| pub use scan::MessageDesc; | ||
| pub use scan::decode; | ||
| pub use scan::scan; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We avoid
pub(...), this can be public as long as the module isn't (which it isn't).