diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 5c45f9c50fc039..44efe45afc0dff 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -460,7 +460,7 @@ def test_fromhex(self): self.assertRaises(ValueError, self.type2test.fromhex, '12 \x00 34') # For odd number of character(s) - for value in ("a", "a ", " a"," a ", "a a a", "a a a ", " a a a", " a a a "): + for value in ("a", "a ", " a"," a ", "a a a", "aa a ", " aa a", " aaa "): with self.assertRaises(ValueError) as cm: self.type2test.fromhex(value) self.assertIn("fromhex() arg must be of even length", str(cm.exception)) diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index fe0477ffa5ad06..30f232e2dbb231 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -2541,6 +2541,14 @@ _PyBytes_FromHex(PyObject *string, int use_bytearray) } str++; + /* Check if we have a second digit*/ + if (str >= end) { + PyErr_SetString(PyExc_ValueError, + "fromhex() arg must be of even length"); + _PyBytesWriter_Dealloc(&writer); + return NULL; + } + bot = _PyLong_DigitValue[*str]; if (bot >= 16) { invalid_char = str - PyUnicode_1BYTE_DATA(string); @@ -2554,15 +2562,9 @@ _PyBytes_FromHex(PyObject *string, int use_bytearray) return _PyBytesWriter_Finish(&writer, buf); error: - if (str >= end) { - PyErr_SetString(PyExc_ValueError, - "fromhex() arg must be of even length"); - } - else { - PyErr_Format(PyExc_ValueError, - "non-hexadecimal number found in " - "fromhex() arg at position %zd", invalid_char); - } + PyErr_Format(PyExc_ValueError, + "non-hexadecimal number found in " + "fromhex() arg at position %zd", invalid_char); _PyBytesWriter_Dealloc(&writer); return NULL; }