Skip to content

gh-127740: For odd-length input to bytes.fromhex(...) change the error message to ValueError: fromhex() arg must be of even length - #127756

Merged
cfbolz merged 23 commits into
python:mainfrom
srinivasreddy:gh-127740
Dec 11, 2024
Merged

gh-127740: For odd-length input to bytes.fromhex(...) change the error message to ValueError: fromhex() arg must be of even length#127756
cfbolz merged 23 commits into
python:mainfrom
srinivasreddy:gh-127740

Conversation

@srinivasreddy

@srinivasreddysrinivasreddy commented Dec 9, 2024

Copy link
Copy Markdown
Contributor

…e error message to ValueError: fromhex() arg must be of even length

@hauntsaninjahauntsaninja left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
ContributorAuthor

@hauntsaninja Please review again?

@picnixzpicnixz left a comment

Copy link
Copy Markdown
Member

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?

Comment threadObjects/bytesobject.c Outdated
Comment threadLib/test/test_bytes.py Outdated
@cfbolz

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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
Copy Markdown
ContributorAuthor

@cfbolz what do you think now ?

@cfbolz

Copy link
Copy Markdown
Contributor

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

@cfbolzcfbolz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me

self.assertRaises(ValueError, self.type2test.fromhex, '12 \x00 34')

# For odd number of character(s)
for value in ("a", "aaa", "deadbee"):

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
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
Copy Markdown
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 freeto 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

@srinivasreddy@cfbolz@Kodiologist@picnixz@hauntsaninja@Redeyder723