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

feat: add deserde check for JsonStorageKey #1915

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

stevencartavia
Copy link
Contributor

Motivation

should close #1902

Solution

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

Comment on lines 76 to 81
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));
}
Copy link
Member

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

Copy link
Contributor Author

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)));
    }

Copy link
Member

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

@@ -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");
Copy link
Member

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

if s.len() > 66 {
return Err(ParseError::BaseConvertError(BaseConvertError::Overflow));
} else if !is_prefixed {
return Err(ParseError::InvalidDigit(s.chars().next().unwrap()));
Copy link
Member

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

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

Successfully merging this pull request may close these issues.

[Feature] Add deserde check for JsonStorageKey
2 participants