Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
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
Revert "[3.15] gh-158583: Fix uninitialized memory read in bytes.from…
…hex() (GH-158584) (#158691)"

This reverts commit 4f7af46.
  • Loading branch information
pablogsal committed Oct 5, 2026
commit 30df3946e19ce6a02e3b80e1662a22538b5212c7
9 changes: 0 additions & 9 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,15 +508,6 @@ def test_fromhex(self):
self.type2test.fromhex(data)
self.assertIn('at position %s' % pos, str(cm.exception))

# gh-158583: Check for out of bounds reads (uninitialized bytes).
# Create an array from a list to not overallocate.
a = array.array('B', list(b'1234 ')) # Py_ISSPACE() loop
self.assertEqual(self.type2test.fromhex(a), b'\x12\x34')

a = array.array('B', list(b'12345')) # Missing second digit
with self.assertRaises(ValueError):
self.type2test.fromhex(a)

def test_hex(self):
self.assertRaises(TypeError, self.type2test.hex)
self.assertRaises(TypeError, self.type2test.hex, 1)
Expand Down

This file was deleted.

18 changes: 8 additions & 10 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2691,35 +2691,33 @@ _PyBytes_FromHex(PyObject *string, int use_bytearray)
if (Py_ISSPACE(*str)) {
do {
str++;
if (str >= end) {
goto done;
}
} while (Py_ISSPACE(*str));
if (str >= end)
break;
}

top = _PyLong_DigitValue[*str];
if (top >= 16) {
invalid_char = str - start;
goto error;
}

str++;
if (str >= end) {
invalid_char = -1;
goto error;
}

bot = _PyLong_DigitValue[*str];
if (bot >= 16) {
invalid_char = str - start;
/* Check if we had a second digit */
if (str >= end){
invalid_char = -1;
} else {
invalid_char = str - start;
}
goto error;
}
str++;

*buf++ = (unsigned char)((top << 4) + bot);
}

done:
if (view.obj != NULL) {
PyBuffer_Release(&view);
}
Expand Down