Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-32587: Make winreg.REG_MULTI_SZ support zero-length strings #13239

Merged
merged 2 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions Lib/test/test_winreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make :data:`winreg.REG_MULTI_SZ` support zero-length strings.
41 changes: 25 additions & 16 deletions PC/winreg.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,24 +518,39 @@ 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++) {
;
}
}
}

static int
countStrings(wchar_t *data, int len)
{
int strings;
wchar_t *P;
wchar_t *Q = data + len;
wchar_t *P, *Q;

if (len > 0 && data[len - 1] == '\0') {
Q = data + len - 1;
}
else {
Q = data + len;
}

for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
for (; P < Q && *P != '\0'; P++)
for (P = data, strings = 0; P < Q; P++, strings++) {
for (; P < Q && *P != '\0'; P++) {
;
}
}
return strings;
}

Expand Down Expand Up @@ -749,21 +764,15 @@ Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
}
for (index = 0; index < s; index++)
{
size_t len = wcslen(str[index]);
if (len > 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);
size_t slen = wcsnlen(str[index], len);
ZackerySpytz marked this conversation as resolved.
Show resolved Hide resolved
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 + 1;
}
PyMem_Free(str);

Expand Down