[Patches] fix up msvcrtmodule.c and winreg.c for Win64

Trent Mick trentm@activestate.com
Thu, 1 Jun 2000 20:47:57 -0700


Discussion:

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*).


Legal:

I confirm that, to the best of my knowledge and belief, this
contribution is free of any claims of third parties under
copyright, patent or other rights or interests ("claims").  To
the extent that I have any such claims, I hereby grant to CNRI a
nonexclusive, irrevocable, royalty-free, worldwide license to
reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part
of the Python software and its related documentation, or any
derivative versions thereof, at no cost to CNRI or its licensed
users, and to authorize others to do so.

I acknowledge that CNRI may, at its sole discretion, decide
whether or not to incorporate this contribution in the Python
software and its related documentation.  I further grant CNRI
permission to use my name and other identifying information
provided to CNRI by me for use in connection with the Python
software and its related documentation.


Patch:

*** /home/trentm/main/contrib/python/dist/src/PC/msvcrtmodule.c	Thu Jun  1 00:13:40 2000
--- /home/trentm/main/Apps/Perlium/Python/dist/src/PC/msvcrtmodule.c	Wed May 31 23:54:19 2000
***************
*** 90,96 ****
  static PyObject *msvcrt_get_osfhandle(PyObject *self, PyObject *args)
  {
  	int fd;
! 	long handle;
  
  	if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
  		return NULL;
--- 90,96 ----
  static PyObject *msvcrt_get_osfhandle(PyObject *self, PyObject *args)
  {
  	int fd;
! 	intptr_t handle;
  
  	if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
  		return NULL;
***************
*** 99,105 ****
  	if (handle == -1)
  		return PyErr_SetFromErrno(PyExc_IOError);
  
! 	return PyInt_FromLong(handle);
  }
  
  /* Console I/O */
--- 99,108 ----
  	if (handle == -1)
  		return PyErr_SetFromErrno(PyExc_IOError);
  
! 	/* technically 'handle' is not a pointer, but a integer as
! 	   large as a pointer, Python's *VoidPtr interface is the
! 	   most appropriate here */
! 	return PyLong_FromVoidPtr((void*)handle);
  }
  
  /* Console I/O */
*** /home/trentm/main/contrib/python/dist/src/PC/winreg.c	Thu Jun  1 01:02:33 2000
--- /home/trentm/main/Apps/Perlium/Python/dist/src/PC/winreg.c	Wed May 31 23:54:20 2000
***************
*** 590,596 ****
  		*pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
  		if (PyErr_Occurred())
  			return FALSE;
- 		*pHANDLE = (HKEY)PyInt_AsLong(ob);
  	}
  	else {
  		PyErr_SetString(
--- 590,595 ----
***************
*** 626,637 ****
--- 625,645 ----
  	if (PyHKEY_Check(obHandle)) {
  		ok = PyHKEY_Close(obHandle);
  	}
+ #if SIZEOF_LONG >= SIZEOF_HKEY
  	else if (PyInt_Check(obHandle)) {
  		long rc = RegCloseKey((HKEY)PyInt_AsLong(obHandle));
  		ok = (rc == ERROR_SUCCESS);
  		if (!ok)
  			PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
  	}
+ #else
+ 	else if (PyLong_Check(obHandle)) {
+ 		long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
+ 		ok = (rc == ERROR_SUCCESS);
+ 		if (!ok)
+ 			PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
+ 	}
+ #endif
  	else {
  		PyErr_SetString(
  			PyExc_TypeError,
***************
*** 878,890 ****
  
  				fixupMultiSZ(str, retDataBuf, retDataSize);
  				obData = PyList_New(s);
  				for (index = 0; index < s; index++)
  				{
  					PyList_SetItem(obData,
  						       index,
  						       PyUnicode_DecodeMBCS(
  						            (const char *)str[index],
! 							    _mbstrlen(str[index]),
  							    NULL)
  						       );
  				}
--- 886,907 ----
  
  				fixupMultiSZ(str, retDataBuf, retDataSize);
  				obData = PyList_New(s);
+ 				if (obData == NULL)
+ 					return NULL;
  				for (index = 0; index < s; index++)
  				{
+ 					size_t len = _mbstrlen(str[index]);
+ 					if (len > INT_MAX) {
+ 						PyErr_SetString(PyExc_OverflowError,
+ 							"registry string is too long for a Python string");
+ 						Py_DECREF(obData);
+ 						return NULL;
+ 					}
  					PyList_SetItem(obData,
  						       index,
  						       PyUnicode_DecodeMBCS(
  						            (const char *)str[index],
! 							   (int)len,
  							    NULL)
  						       );
  				}

-- 
Trent Mick
trentm@activestate.com