8000 gh-127740: For odd-length input to bytes.fromhex(...) change the error message to ValueError: fromhex() arg must be of even length by srinivasreddy · Pull Request #127756 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 23 commits into from
Dec 11, 2024

Conversation

srinivasreddy
Copy link
Contributor
@srinivasreddy srinivasreddy commented Dec 9, 2024

…e error message to ValueError: fromhex() arg must be of even length
Copy link
Contributor
@hauntsaninja hauntsaninja left a 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?

@srinivasreddy
Copy link
Contributor Author

@hauntsaninja Please review again?

Copy link
Member
@picnixz picnixz left a 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?

@cfbolz
Copy link
Contributor
cfbolz commented Dec 10, 2024

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. if (bot >= 16) { is true if we are at the end of the string (because the null at the end is mapped to 37), so we can do the check in there.

@cfbolz
Copy link
Contributor
cfbolz commented Dec 10, 2024

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 if (str >= end) {, but in the same place where you now added the if (bot == 37){ check.

@srinivasreddy
Copy link
Contributor Author

@cfbolz what do you think now ?

@cfbolz
Copy link
Contributor
cfbolz commented Dec 10, 2024

@srinivasreddy code looks good now, imo. please add a news entry though.

Copy link
Contributor
@cfbolz cfbolz left a 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

@cfbolz cfbolz merged commit db9bea0 into python:main Dec 11, 2024
45 checks passed
@@ -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"):
Copy link
Contributor

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 "

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

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!

@Kodiologist
Copy link
Contributor

That was fast. Thanks.

srinivasreddy added a commit to srinivasreddy/cpython that referenced this pull request Jan 8, 2025
…e error message to ValueError: fromhex() arg must be of even length (python#127756)
srinivasreddy added a commit to srinivasreddy/cpython that referenced this pull request Jan 8, 2025
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.

6 participants
0