-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-127740: For odd-length input to bytes.fromhex(...) change the er 8000 ror message to ValueError: fromhex() arg must be of even length #127756
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
Conversation
…e error message to ValueError: fromhex() arg must be of even length
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.
Can we avoid the upfront loop you're using to check this eagerly? E.g. check invalid_char vs hexlen in the error clause?
@hauntsaninja Please review again? |
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 hauntsaninja wanted the error check at the level of the error label, not during the parsing. Since the current code jumps to error in case of an odd-length input, is it possible to check that it's indeed odd-length when creating the error?
Yes, I agree with @picnixz and @hauntsaninja, the new if is done for every character pair now, which makes things less efficient. I suggest moving the new check into the condition just below. |
No, that's not what I meant, sorry for being unclear. many invalid byte values are mapped to 37, not just null. So instead, you need to check for your original condition |
@cfbolz what do you think now ? |
@srinivasreddy code looks good now, imo. please add a news entry though. |
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.
Looks good to me
@@ -459,6 +459,12 @@ def test_fromhex(self): | |||
self.assertRaises(ValueError, self.type2test.fromhex, '\x00') | |||
self.assertRaises(ValueError, self.type2test.fromhex, '12 \x00 34') | |||
|
|||
# For odd number of character(s) | |||
for value in ("a", "aaa", "deadbee"): |
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.
nit: would be nice to have a test case where the string is of even length, e.g. " aaa" and test cases where we check the error message for " aa a " / " aa a a "
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.
sorry for going ahead and merging @hauntsaninja. I should have waited for more input from you.
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.
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.
oh all good, was just a nit and commits are cheap :-) thanks for reviewing!
That was fast. Thanks. |
…e error message to ValueError: fromhex() arg must be of even length (python#127756)
bytes.fromhex
is cryptic #127740