From e263a26c900263c02f52a624feb6bea3b4d408bc Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Fri, 10 May 2019 15:30:14 -0600 Subject: [PATCH 1/2] bpo-32587: Make winreg.REG_MULTI_SZ support PendingFileRenameOperations --- .../2019-05-10-15-25-44.bpo-32587.-0g2O3.rst | 1 + PC/winreg.c | 36 +++++++++++++------ 2 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 Misc/NEWS.d/next/Windows/2019-05-10-15-25-44.bpo-32587.-0g2O3.rst diff --git a/Misc/NEWS.d/next/Windows/2019-05-10-15-25-44.bpo-32587.-0g2O3.rst b/Misc/NEWS.d/next/Windows/2019-05-10-15-25-44.bpo-32587.-0g2O3.rst new file mode 100644 index 00000000000000..8b51658273acdf --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2019-05-10-15-25-44.bpo-32587.-0g2O3.rst @@ -0,0 +1 @@ +Make :data:`winreg.REG_MULTI_SZ` support PendingFileRenameOperations. diff --git a/PC/winreg.c b/PC/winreg.c index 5469fcba044432..5509e83f9fbde5 100644 --- a/PC/winreg.c +++ b/PC/winreg.c @@ -518,11 +518,18 @@ fixupMultiSZ(wchar_t **str, wchar_t *data, int len) int i; wchar_t *Q; - Q = data + len; - for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) { + if (len > 0 && data[len - 1] == '\0') { + Q = data + len - 1; + } + else { + Q = data + len; + } + + for (P = data, i = 0; P < Q; P++, i++) { str[i] = P; - for (; P < Q && *P != '\0'; P++) + for (; P < Q && *P != '\0'; P++) { ; + } } } @@ -530,12 +537,20 @@ static int countStrings(wchar_t *data, int len) { int strings; - wchar_t *P; - wchar_t *Q = data + len; + wchar_t *P, *Q; - for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++) - for (; P < Q && *P != '\0'; P++) + if (len > 0 && data[len - 1] == '\0') { + Q = data + len - 1; + } + else { + Q = data + len; + } + + for (P = data, strings = 0; P < Q; P++, strings++) { + for (; P < Q && *P != '\0'; P++) { ; + } + } return strings; } @@ -749,21 +764,22 @@ Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ) } for (index = 0; index < s; index++) { - size_t len = wcslen(str[index]); - if (len > INT_MAX) { + size_t slen = wcsnlen(str[index], len); + if (slen > INT_MAX) { PyErr_SetString(PyExc_OverflowError, "registry string is too long for a Python string"); Py_DECREF(obData); PyMem_Free(str); return NULL; } - PyObject *uni = PyUnicode_FromWideChar(str[index], len); + PyObject *uni = PyUnicode_FromWideChar(str[index], slen); if (uni == NULL) { Py_DECREF(obData); PyMem_Free(str); return NULL; } PyList_SET_ITEM(obData, index, uni); + len -= slen; } PyMem_Free(str); From 406ecdccadc979a5222887d8cbd9ccf93f380c82 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Mon, 13 May 2019 20:29:31 -0600 Subject: [PATCH 2/2] Address review comments. --- Lib/test/test_winreg.py | 1 + .../Windows/2019-05-10-15-25-44.bpo-32587.-0g2O3.rst | 2 +- PC/winreg.c | 9 +-------- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_winreg.py b/Lib/test/test_winreg.py index dc2b46e42521f3..7e12f51f5f1173 100644 --- a/Lib/test/test_winreg.py +++ b/Lib/test/test_winreg.py @@ -41,6 +41,7 @@ ("String Val", "A string value", REG_SZ), ("StringExpand", "The path is %path%", REG_EXPAND_SZ), ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ), + ("Multi-nul", ["", "", "", ""], REG_MULTI_SZ), ("Raw Data", b"binary\x00data", REG_BINARY), ("Big String", "x"*(2**14-1), REG_SZ), ("Big Binary", b"x"*(2**14), REG_BINARY), diff --git a/Misc/NEWS.d/next/Windows/2019-05-10-15-25-44.bpo-32587.-0g2O3.rst b/Misc/NEWS.d/next/Windows/2019-05-10-15-25-44.bpo-32587.-0g2O3.rst index 8b51658273acdf..41483aa8b74a9f 100644 --- a/Misc/NEWS.d/next/Windows/2019-05-10-15-25-44.bpo-32587.-0g2O3.rst +++ b/Misc/NEWS.d/next/Windows/2019-05-10-15-25-44.bpo-32587.-0g2O3.rst @@ -1 +1 @@ -Make :data:`winreg.REG_MULTI_SZ` support PendingFileRenameOperations. +Make :data:`winreg.REG_MULTI_SZ` support zero-length strings. diff --git a/PC/winreg.c b/PC/winreg.c index 5509e83f9fbde5..0461617044e66c 100644 --- a/PC/winreg.c +++ b/PC/winreg.c @@ -765,13 +765,6 @@ Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ) for (index = 0; index < s; index++) { size_t slen = wcsnlen(str[index], len); - if (slen > INT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "registry string is too long for a Python string"); - Py_DECREF(obData); - PyMem_Free(str); - return NULL; - } PyObject *uni = PyUnicode_FromWideChar(str[index], slen); if (uni == NULL) { Py_DECREF(obData); @@ -779,7 +772,7 @@ Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ) return NULL; } PyList_SET_ITEM(obData, index, uni); - len -= slen; + len -= slen + 1; } PyMem_Free(str);