mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
gh-127740: For odd-length input to bytes.fromhex(...) change the error message to ValueError: fromhex() arg must be of even length (#127756)
This commit is contained in:
parent
12b4f1a5a1
commit
db9bea0386
3 changed files with 23 additions and 4 deletions
|
@ -2543,7 +2543,12 @@ _PyBytes_FromHex(PyObject *string, int use_bytearray)
|
|||
|
||||
bot = _PyLong_DigitValue[*str];
|
||||
if (bot >= 16) {
|
||||
invalid_char = str - PyUnicode_1BYTE_DATA(string);
|
||||
/* Check if we had a second digit */
|
||||
if (str >= end){
|
||||
invalid_char = -1;
|
||||
} else {
|
||||
invalid_char = str - PyUnicode_1BYTE_DATA(string);
|
||||
}
|
||||
goto error;
|
||||
}
|
||||
str++;
|
||||
|
@ -2554,9 +2559,14 @@ _PyBytes_FromHex(PyObject *string, int use_bytearray)
|
|||
return _PyBytesWriter_Finish(&writer, buf);
|
||||
|
||||
error:
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"non-hexadecimal number found in "
|
||||
"fromhex() arg at position %zd", invalid_char);
|
||||
if (invalid_char == -1) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"fromhex() arg must contain an even number of hexadecimal digits");
|
||||
} else {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"non-hexadecimal number found in "
|
||||
"fromhex() arg at position %zd", invalid_char);
|
||||
}
|
||||
_PyBytesWriter_Dealloc(&writer);
|
||||
return NULL;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue