summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Dower <steve.dower@microsoft.com>2016-12-17 13:31:58 -0800
committerSteve Dower <steve.dower@microsoft.com>2016-12-17 13:31:58 -0800
commitada27ff7010090e4d2a53a666ece366e86c0f570 (patch)
tree9446851b9c0fc728c86247d045f8a50e910c6613
parent2387e4c60588d93ccd00fb200f526b8178e8b4ef (diff)
parent8c537f03dd3a32b887541168252d7cd8e13154a1 (diff)
downloadcpython-ada27ff7010090e4d2a53a666ece366e86c0f570.tar.gz
Issue #25778: winreg does not truncase string correctly (Patch by Eryk Sun)
-rw-r--r--Lib/test/test_winreg.py14
-rw-r--r--Misc/NEWS2
-rw-r--r--PC/winreg.c13
3 files changed, 21 insertions, 8 deletions
diff --git a/Lib/test/test_winreg.py b/Lib/test/test_winreg.py
index d642b13f68..2be61ae15d 100644
--- a/Lib/test/test_winreg.py
+++ b/Lib/test/test_winreg.py
@@ -57,7 +57,7 @@ class BaseWinregTests(unittest.TestCase):
def delete_tree(self, root, subkey):
try:
- hkey = OpenKey(root, subkey, KEY_ALL_ACCESS)
+ hkey = OpenKey(root, subkey, 0, KEY_ALL_ACCESS)
except OSError:
# subkey does not exist
return
@@ -368,6 +368,18 @@ class LocalWinregTests(BaseWinregTests):
finally:
DeleteKey(HKEY_CURRENT_USER, test_key_name)
+ def test_read_string_containing_null(self):
+ # Test for issue 25778: REG_SZ should not contain null characters
+ try:
+ with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
+ self.assertNotEqual(ck.handle, 0)
+ test_val = "A string\x00 with a null"
+ SetValueEx(ck, "test_name", 0, REG_SZ, test_val)
+ ret_val, ret_type = QueryValueEx(ck, "test_name")
+ self.assertEqual(ret_type, REG_SZ)
+ self.assertEqual(ret_val, "A string")
+ finally:
+ DeleteKey(HKEY_CURRENT_USER, test_key_name)
@unittest.skipUnless(REMOTE_NAME, "Skipping remote registry tests")
diff --git a/Misc/NEWS b/Misc/NEWS
index 718a5d5dad..cf5acfe1dc 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -497,6 +497,8 @@ Library
Windows
-------
+- Issue #25778: winreg does not truncase string correctly (Patch by Eryk Sun)
+
- Issue #28896: Deprecate WindowsRegistryFinder and disable it by default
- Issue #28522: Fixes mishandled buffer reallocation in getpathp.c
diff --git a/PC/winreg.c b/PC/winreg.c
index 9524838c08..5efdc5e0ef 100644
--- a/PC/winreg.c
+++ b/PC/winreg.c
@@ -719,14 +719,13 @@ Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
case REG_SZ:
case REG_EXPAND_SZ:
{
- /* the buffer may or may not have a trailing NULL */
+ /* REG_SZ should be a NUL terminated string, but only by
+ * convention. The buffer may have been saved without a NUL
+ * or with embedded NULs. To be consistent with reg.exe and
+ * regedit.exe, consume only up to the first NUL. */
wchar_t *data = (wchar_t *)retDataBuf;
- int len = retDataSize / 2;
- if (retDataSize && data[len-1] == '\0')
- retDataSize -= 2;
- if (retDataSize <= 0)
- data = L"";
- obData = PyUnicode_FromWideChar(data, retDataSize/2);
+ size_t len = wcsnlen(data, retDataSize / sizeof(wchar_t));
+ obData = PyUnicode_FromWideChar(data, len);
break;
}
case REG_MULTI_SZ: