-
Notifications
You must be signed in to change notification settings - Fork 256
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
feat: add deserde check for JsonStorageKey #1915
base: main
Are you sure you want to change the base?
feat: add deserde check for JsonStorageKey #1915
Conversation
crates/serde/src/storage.rs
Outdated
let hex_str = s.strip_prefix("0x").unwrap_or(s); | ||
let is_prefixed = s.starts_with("0x"); | ||
|
||
if (is_prefixed && hex_str.len() > 64) || (!is_prefixed && hex_str.len() > 66) { | ||
return Err(ParseError::BaseConvertError(BaseConvertError::Overflow)); | ||
} |
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.
this we can change a bit by changing the order in which we check
if it's > 66 -> return err
else if doesn't start with 0x -> return err
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.
I think this needs to be updated because the second assert fails with the else if !is_prefixed
#[test]
fn test_from_str_parsing() {
let hex_str = "0x0101010101010101010101010101010101010101010101010101010101010101";
let key = JsonStorageKey::from_str(hex_str).unwrap();
assert_eq!(key, JsonStorageKey::Hash(B256::from_str(hex_str).unwrap()));
let num_str = "42";
let key = JsonStorageKey::from_str(num_str).unwrap();
assert_eq!(key, JsonStorageKey::Number(U256::from(42)));
}
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.
yeah looks like this test is indeed invalid
crates/serde/src/storage.rs
Outdated
@@ -70,6 +73,14 @@ impl FromStr for JsonStorageKey { | |||
type Err = ParseError; | |||
|
|||
fn from_str(s: &str) -> Result<Self, Self::Err> { | |||
let is_prefixed = s.starts_with("0x"); |
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.
this check is redundant here and should be moved inside the len >= 66 check
crates/serde/src/storage.rs
Outdated
if s.len() > 66 { | ||
return Err(ParseError::BaseConvertError(BaseConvertError::Overflow)); | ||
} else if !is_prefixed { | ||
return Err(ParseError::InvalidDigit(s.chars().next().unwrap())); |
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.
this can panic for empty input
Motivation
should close #1902
Solution
PR Checklist