Skip to content

Commit

Permalink
Issue python#21151: Fixed a segfault in the winreg module.
Browse files Browse the repository at this point in the history
When ``None`` was passed as a ``REG_BINARY`` value to SetValueEx,
PyMem_DEL was called on an uninitialized buffer.  Patch by John Ehresman.

(Also an incidental typo fix in a comment in test_winreg)
  • Loading branch information
zware committed Jul 3, 2014
1 parent 0d50af4 commit ad4690f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
15 changes: 14 additions & 1 deletion Lib/test/test_winreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def test_setvalueex_value_range(self):
def test_queryvalueex_return_value(self):
# Test for Issue #16759, return unsigned int from QueryValueEx.
# Reg2Py, which gets called by QueryValueEx, was returning a value
# generated by PyLong_FromLong. The implmentation now uses
# generated by PyLong_FromLong. The implementation now uses
# PyLong_FromUnsignedLong to match DWORD's size.
try:
with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
Expand All @@ -354,6 +354,19 @@ def test_queryvalueex_return_value(self):
finally:
DeleteKey(HKEY_CURRENT_USER, test_key_name)

def test_setvalueex_crash_with_none_arg(self):
# Test for Issue #21151, segfault when None is passed to SetValueEx
try:
with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
self.assertNotEqual(ck.handle, 0)
test_val = None
SetValueEx(ck, "test_name", 0, REG_BINARY, test_val)
ret_val, ret_type = QueryValueEx(ck, "test_name")
self.assertEqual(ret_type, REG_BINARY)
self.assertEqual(ret_val, test_val)
finally:
DeleteKey(HKEY_CURRENT_USER, test_key_name)



@unittest.skipUnless(REMOTE_NAME, "Skipping remote registry tests")
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Core and Builtins
Library
-------

- Issue #21151: Fixed a segfault in the winreg module when ``None`` is passed
as a ``REG_BINARY`` value to SetValueEx. Patch by John Ehresman.

- Issue #21090: io.FileIO.readall() does not ignore I/O errors anymore. Before,
it ignored I/O errors if at least the first C call read() succeed.

Expand Down
4 changes: 3 additions & 1 deletion PC/winreg.c
Original file line number Diff line number Diff line change
Expand Up @@ -871,8 +871,10 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
/* ALSO handle ALL unknown data types here. Even if we can't
support it natively, we should handle the bits. */
default:
if (value == Py_None)
if (value == Py_None) {
*retDataSize = 0;
*retDataBuf = NULL;
}
else {
Py_buffer view;

Expand Down

0 comments on commit ad4690f

Please sign in to comment.