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
8000

gh-127740: For odd-length input to bytes.fromhex(...) change the error 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
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b5b0da7
gh-127740: For odd-length input to bytes.fromhex(...) change the erro…
srinivasreddy Dec 9, 2024
14b7d5e
Introduce space between variables
srinivasreddy Dec 9, 2024
f3bfe85
gh-127740: Improve code, consolidate two variables into one
srinivasreddy Dec 9, 2024
5a55e24
Check the logic inside of the loop, instead of checking eagerly
srinivasreddy Dec 9, 2024
a3ba0e7
Remove dead variable
srinivasreddy Dec 9, 2024
ee7947a
Fix a bug in logic
srinivasreddy Dec 9, 2024
879994e
Add some more tests
srinivasreddy Dec 9, 2024
ad96b89
Improve tests
srinivasreddy Dec 9, 2024
95744bd
Revert the change that is not necessary
srinivasreddy Dec 9, 2024
6929a4d
Move everthing to error labels
srinivasreddy Dec 9, 2024
aa6684b
Remove comments
srinivasreddy Dec 9, 2024
bf3ace9
Add tests and update logic
srinivasreddy Dec 9, 2024
3588d18
Update tests
srinivasreddy Dec 9, 2024
504f4ed
Add tests and update logic
srinivasreddy Dec 9, 2024
7bab683
Change the invalid_char value to -1
srinivasreddy Dec 9, 2024
7c819c1
Update test case
srinivasreddy Dec 9, 2024
862bfd5
gh-127740: Improve error messages as suggested by @hauntsaninja
srinivasreddy Dec 10, 2024
6cc9305
Update test cases
srinivasreddy Dec 10, 2024
c34696e
gh-127740: NULL at the end is mapped to 37
srinivasreddy Dec 10, 2024
82762db
gh-127740: Reintroduce the space back
srinivasreddy Dec 10, 2024
6628ea7
gh-127740: Check if we had a second digit
srinivasreddy Dec 10, 2024
b20e248
Add News entry
srinivasreddy Dec 10, 2024
cbf50b4
Fix lint
srinivasreddy Dec 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Check the logic inside of the loop, instead of checking eagerly
  • Loading branch information
srinivasreddy committed Dec 9, 2024
commit 5a55e24dbe9d63f1b23a2cfe910add02f42802cc
31 changes: 14 additions & 17 deletions Objects/bytesobject.c
4C19
Original file line number Diff line number Diff line change
Expand Up @@ -2490,7 +2490,7 @@
_PyBytes_FromHex(PyObject *string, int use_bytearray)
{
char *buf;
Py_ssize_t hexlen, invalid_char, real_len=0;

Check warning on line 2493 in Objects/bytesobject.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

unused variable ‘real_len’ [-Wunused-variable]
unsigned int top, bot;
const Py_UCS1 *str, *end;
_PyBytesWriter writer;
Expand All @@ -2517,17 +2517,6 @@

assert(PyUnicode_KIND(string) == PyUnicode_1BYTE_KIND);
str = PyUnicode_1BYTE_DATA(string);
for (Py_ssize_t i = 0; i < hexlen; i++) {
if (!Py_ISSPACE(str[i])) {
real_len++;
}
}
if (real_len % 2 != 0) {
PyErr_SetString(PyExc_ValueError,
"fromhex() arg must be of even length");
_PyBytesWriter_Dealloc(&writer);
return NULL;
}

/* This overestimates if there are spaces */
buf = _PyBytesWriter_Alloc(&writer, hexlen / 2);
Expand All @@ -2537,21 +2526,29 @@
end = str + hexlen;
while (str < end) {
/* skip over spaces in the input */
if (Py_ISSPACE(*str)) {
do {
str++;
} while (Py_ISSPACE(*str));
if (str >= end)
break;
while (str < end && Py_ISSPACE(*str)) {
str++;
}
if (str >= end)
break;

/* Check first hex digit */
top = _PyLong_DigitValue[*str];
if (top >= 16) {
invalid_char = str - PyUnicode_1BYTE_DATA(string);
goto error;
}
str++;

/* Check if we have a second digit*/
if (str >= end || Py_ISSPACE(*str)) {
PyErr_SetString(PyExc_ValueError,
"fromhex() arg must be of even length");
_PyBytesWriter_Dealloc(&writer);
return NULL;
}

/* Check second hex digit */
bot = _PyLong_DigitValue[*str];
if (bot >= 16) {
invalid_char = str - PyUnicode_1BYTE_DATA(string);
Expand Down
Loading
0