Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 25e1726

Browse files
committed
[*** Not tested as I don't have Windows running right now! ***]
Trent Mick <[email protected]>: Fix PC/msvcrtmodule.c and PC/winreg.c for Win64. Basically: - sizeof(HKEY) > sizeof(long) on Win64, so use PyLong_FromVoidPtr() instead of PyInt_FromLong() to return HKEY values on Win64 - Check for string overflow of an arbitrary registry value (I know that ensuring that a registry value does not overflow 2**31 characters seems ridiculous but it is *possible*). Closes SourceForge patch #100517.
1 parent 7efcafb commit 25e1726

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

PC/_winreg.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,6 @@ PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
592592
*pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
593593
if (PyErr_Occurred())
594594
return FALSE;
595-
*pHANDLE = (HKEY)PyInt_AsLong(ob);
596595
}
597596
else {
598597
PyErr_SetString(
@@ -628,12 +627,21 @@ PyWinObject_CloseHKEY(PyObject *obHandle)
628627
if (PyHKEY_Check(obHandle)) {
629628
ok = PyHKEY_Close(obHandle);
630629
}
630+
#if SIZEOF_LONG >= SIZEOF_HKEY
631631
else if (PyInt_Check(obHandle)) {
632632
long rc = RegCloseKey((HKEY)PyInt_AsLong(obHandle));
633633
ok = (rc == ERROR_SUCCESS);
634634
if (!ok)
635635
PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
636636
}
637+
#else
638+
else if (PyLong_Check(obHandle)) {
639+
long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
640+
ok = (rc == ERROR_SUCCESS);
641+
if (!ok)
642+
PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
643+
}
644+
#endif
637645
else {
638646
PyErr_SetString(
639647
PyExc_TypeError,
@@ -880,13 +888,22 @@ Reg2Py(char *retDataBuf, DWORD retDataSize, DWORD typ)
880888

881889
fixupMultiSZ(str, retDataBuf, retDataSize);
882890
obData = PyList_New(s);
891+
if (obData == NULL)
892+
return NULL;
883893
for (index = 0; index < s; index++)
884894
{
895+
size_t len = _mbstrlen(str[index]);
896+
if (len > INT_MAX) {
897+
PyErr_SetString(PyExc_OverflowError,
898+
"registry string is too long for a Python string");
899+
Py_DECREF(obData);
900+
return NULL;
901+
}
885902
PyList_SetItem(obData,
886903
index,
887904
PyUnicode_DecodeMBCS(
888905
(const char *)str[index],
889-
_mbstrlen(str[index]),
906+
(int)len,
890907
NULL)
891908
);
892909
}

PC/msvcrtmodule.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static PyObject *msvcrt_open_osfhandle(PyObject *self, PyObject *args)
9090
static PyObject *msvcrt_get_osfhandle(PyObject *self, PyObject *args)
9191
{
9292
int fd;
93-
long handle;
93+
intptr_t handle;
9494

9595
if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
9696
return NULL;
@@ -99,7 +99,10 @@ static PyObject *msvcrt_get_osfhandle(PyObject *self, PyObject *args)
9999
if (handle == -1)
100100
return PyErr_SetFromErrno(PyExc_IOError);
101101

102-
return PyInt_FromLong(handle);
102+
/* technically 'handle' is not a pointer, but a integer as
103+
large as a pointer, Python's *VoidPtr interface is the
104+
most appropriate here */
105+
return PyLong_FromVoidPtr((void*)handle);
103106
}
104107

105108
/* Console I/O */

0 commit comments

Comments
 (0)