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

cleanup util::from_hex() #3265

Merged
merged 3 commits into from
Mar 10, 2020
Merged

Conversation

antiochp
Copy link
Member

@antiochp antiochp commented Mar 9, 2020

Our original implementation panic'd if the hex string was an odd number of characters.
This is bad as we use from_hex() when parsing various external ids and hashes.

Edit: We prevented the panic via the janky QQQ parse code (see below). So this PR just cleans it up and adds some better test coverage.

  • Simplified the error handling -
    • just return the (invalid) hex string itself as the error
    • got rid of the janky let err = ("QQQ").parse::<u64>();
  • from_hex() now takes a string slice rather than a String

This was previously happening if the hex string had an odd number of characters -

panicked at 'called `Result::unwrap()` on an `Err` value: ParseIntError { kind: InvalidDigit }'

Edit: There was an edge case where it panic'd. If the hex string was empty -

panicked at 'byte index 2 is out of bounds of ``'

Copy link
Member

@yeastplume yeastplume left a comment

Choose a reason for hiding this comment

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

I remember that particular piece of Jank. Changes look much better.

@jaspervdm
Copy link
Contributor

The fact that there is some malleability in deserializing shouldnt matter anywhere right? e.g. 0xAAAA and 0x0xAAAA both serialize to the same bytes

@antiochp
Copy link
Member Author

antiochp commented Mar 10, 2020

The 0x0xAAAA example is an interesting one - did not think about that. This should be invalid but we permissively accept it.

It may be worth revisiting this and making the prefix logic more strict. But either way this is an improvement over current situation.

I'll merge and we can revisit later.

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

Successfully merging this pull request may close these issues.

3 participants