Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor QueryValueEx
  • Loading branch information
maxbachmann authored Feb 28, 2023
commit e0b256d791073e8133b8cbdf310e1bde1bb40908
56 changes: 26 additions & 30 deletions PC/winreg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1582,54 +1582,50 @@ static PyObject *
winreg_QueryValueEx_impl(PyObject *module, HKEY key, const Py_UNICODE *name)
/*[clinic end generated code: output=f1b85b1c3d887ec7 input=cf366cada4836891]*/
{
long rc;
BYTE *retBuf, *tmp;
LONG rc;
WCHAR buf[256], *pbuf = buf;
DWORD size = sizeof(buf);
DWORD bufSize = 0, retSize;
DWORD typ;
PyObject *obData;
PyObject *result;
PyObject *obData = NULL;
PyObject *result = NULL;

if (PySys_Audit("winreg.QueryValue", "nuu",
(Py_ssize_t)key, NULL, name) < 0) {
return NULL;
}
rc = RegQueryValueExW(key, name, NULL, NULL, NULL, &bufSize);
if (rc == ERROR_MORE_DATA)
bufSize = 256;
else if (rc != ERROR_SUCCESS)
return PyErr_SetFromWindowsErrWithFunction(rc,
"RegQueryValueEx");
retBuf = (BYTE *)PyMem_Malloc(bufSize);
if (retBuf == NULL)
return PyErr_NoMemory();

while (1) {
retSize = bufSize;
rc = RegQueryValueExW(key, name, NULL, &typ,
(BYTE *)retBuf, &retSize);
if (rc != ERROR_MORE_DATA)
Py_BEGIN_ALLOW_THREADS
rc = RegQueryValueExW(key, name, NULL, &typ, (LPBYTE)pbuf,
&size);
Py_END_ALLOW_THREADS
if (rc != ERROR_MORE_DATA) {
break;

bufSize *= 2;
tmp = (char *) PyMem_Realloc(retBuf, bufSize);
}
void *tmp = PyMem_Realloc(pbuf != buf ? pbuf : NULL, size);
if (tmp == NULL) {
PyMem_Free(retBuf);
return PyErr_NoMemory();
PyErr_NoMemory();
goto exit;
}
retBuf = tmp;
pbuf = tmp;
}

if (rc != ERROR_SUCCESS) {
PyMem_Free(retBuf);
return PyErr_SetFromWindowsErrWithFunction(rc,
"RegQueryValueEx");
PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryValueEx");
goto exit;
}
obData = Reg2Py(pbuf, length, typ);
if (obData == NULL) {
goto exit;
}
obData = Reg2Py(retBuf, bufSize, typ);
PyMem_Free(retBuf);
if (obData == NULL)
return NULL;
result = Py_BuildValue("Oi", obData, typ);
Py_DECREF(obData);

exit:
if (pbuf != buf) {
PyMem_Free(pbuf);
}
return result;
}

Expand Down