bpo-32587: Make winreg.REG_MULTI_SZ support zero-length strings (GH-13239)

* bpo-32587: Make winreg.REG_MULTI_SZ support PendingFileRenameOperations

* Address review comments.
(cherry picked from commit e223ba13d8)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
This commit is contained in:
Miss Islington (bot) 2019-09-09 03:11:00 -07:00 committed by GitHub
parent 14f7de72b6
commit ebca7eb093
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 16 deletions

View File

@ -41,6 +41,7 @@ test_data = [
("String Val", "A string value", REG_SZ), ("String Val", "A string value", REG_SZ),
("StringExpand", "The path is %path%", REG_EXPAND_SZ), ("StringExpand", "The path is %path%", REG_EXPAND_SZ),
("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ), ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ),
("Multi-nul", ["", "", "", ""], REG_MULTI_SZ),
("Raw Data", b"binary\x00data", REG_BINARY), ("Raw Data", b"binary\x00data", REG_BINARY),
("Big String", "x"*(2**14-1), REG_SZ), ("Big String", "x"*(2**14-1), REG_SZ),
("Big Binary", b"x"*(2**14), REG_BINARY), ("Big Binary", b"x"*(2**14), REG_BINARY),

View File

@ -0,0 +1 @@
Make :data:`winreg.REG_MULTI_SZ` support zero-length strings.

View File

@ -518,11 +518,18 @@ fixupMultiSZ(wchar_t **str, wchar_t *data, int len)
int i; int i;
wchar_t *Q; wchar_t *Q;
Q = data + len; if (len > 0 && data[len - 1] == '\0') {
for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) { Q = data + len - 1;
}
else {
Q = data + len;
}
for (P = data, i = 0; P < Q; P++, i++) {
str[i] = P; 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) countStrings(wchar_t *data, int len)
{ {
int strings; int strings;
wchar_t *P; wchar_t *P, *Q;
wchar_t *Q = data + len;
for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++) if (len > 0 && data[len - 1] == '\0') {
for (; P < Q && *P != '\0'; P++) 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; return strings;
} }
@ -749,21 +764,15 @@ Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
} }
for (index = 0; index < s; index++) for (index = 0; index < s; index++)
{ {
size_t len = wcslen(str[index]); size_t slen = wcsnlen(str[index], len);
if (len > INT_MAX) { PyObject *uni = PyUnicode_FromWideChar(str[index], slen);
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);
if (uni == NULL) { if (uni == NULL) {
Py_DECREF(obData); Py_DECREF(obData);
PyMem_Free(str); PyMem_Free(str);
return NULL; return NULL;
} }
PyList_SET_ITEM(obData, index, uni); PyList_SET_ITEM(obData, index, uni);
len -= slen + 1;
} }
PyMem_Free(str); PyMem_Free(str);