diff options
author | Srinivas Reddy Thatiparthy (తాటిపర్తి శ్రీనివాస్ రెడ్డి) <thatiparthysreenivas@gmail.com> | 2024-12-11 07:35:17 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-11 07:35:17 (GMT) |
commit | db9bea0386c1c0b6c9d7c66474cda7e47e4b56f5 (patch) | |
tree | eed741469781461c4c7c74d6022e9a7d2fb94bf2 /Objects | |
parent | 12b4f1a5a175d4dcec27631fce2883038f0917ae (diff) | |
download | cpython-db9bea0386c1c0b6c9d7c66474cda7e47e4b56f5.zip cpython-db9bea0386c1c0b6c9d7c66474cda7e47e4b56f5.tar.gz cpython-db9bea0386c1c0b6c9d7c66474cda7e47e4b56f5.tar.bz2 |
gh-127740: For odd-length input to bytes.fromhex(...) change the error message to ValueError: fromhex() arg must be of even length (#127756)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/bytesobject.c | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 8c7651f..533089d 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -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; } |