Skip to content

Commit e223ba1

Browse files
ZackerySpytzzooba
authored andcommitted
bpo-32587: Make winreg.REG_MULTI_SZ support zero-length strings (#13239)
* bpo-32587: Make winreg.REG_MULTI_SZ support PendingFileRenameOperations * Address review comments.
1 parent a656365 commit e223ba1

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

Lib/test/test_winreg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
("String Val", "A string value", REG_SZ),
4242
("StringExpand", "The path is %path%", REG_EXPAND_SZ),
4343
("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ),
44+
("Multi-nul", ["", "", "", ""], REG_MULTI_SZ),
4445
("Raw Data", b"binary\x00data", REG_BINARY),
4546
("Big String", "x"*(2**14-1), REG_SZ),
4647
("Big Binary", b"x"*(2**14), REG_BINARY),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make :data:`winreg.REG_MULTI_SZ` support zero-length strings.

PC/winreg.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -518,24 +518,39 @@ fixupMultiSZ(wchar_t **str, wchar_t *data, int len)
518518
int i;
519519
wchar_t *Q;
520520

521-
Q = data + len;
522-
for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
521+
if (len > 0 && data[len - 1] == '\0') {
522+
Q = data + len - 1;
523+
}
524+
else {
525+
Q = data + len;
526+
}
527+
528+
for (P = data, i = 0; P < Q; P++, i++) {
523529
str[i] = P;
524-
for (; P < Q && *P != '\0'; P++)
530+
for (; P < Q && *P != '\0'; P++) {
525531
;
532+
}
526533
}
527534
}
528535

529536
static int
530537
countStrings(wchar_t *data, int len)
531538
{
532539
int strings;
533-
wchar_t *P;
534-
wchar_t *Q = data + len;
540+
wchar_t *P, *Q;
541+
542+
if (len > 0 && data[len - 1] == '\0') {
543+
Q = data + len - 1;
544+
}
545+
else {
546+
Q = data + len;
547+
}
535548

536-
for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
537-
for (; P < Q && *P != '\0'; P++)
549+
for (P = data, strings = 0; P < Q; P++, strings++) {
550+
for (; P < Q && *P != '\0'; P++) {
538551
;
552+
}
553+
}
539554
return strings;
540555
}
541556

@@ -749,21 +764,15 @@ Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
749764
}
750765
for (index = 0; index < s; index++)
751766
{
752-
size_t len = wcslen(str[index]);
753-
if (len > INT_MAX) {
754-
PyErr_SetString(PyExc_OverflowError,
755-
"registry string is too long for a Python string");
756-
Py_DECREF(obData);
757-
PyMem_Free(str);
758-
return NULL;
759-
}
760-
PyObject *uni = PyUnicode_FromWideChar(str[index], len);
767+
size_t slen = wcsnlen(str[index], len);
768+
PyObject *uni = PyUnicode_FromWideChar(str[index], slen);
761769
if (uni == NULL) {
762770
Py_DECREF(obData);
763771
PyMem_Free(str);
764772
return NULL;
765773
}
766774
PyList_SET_ITEM(obData, index, uni);
775+
len -= slen + 1;
767776
}
768777
PyMem_Free(str);
769778

0 commit comments

Comments
 (0)