[Patches] simple 64-bit fixes in Python/ dir

Trent Mick trentm@activestate.com
Mon, 5 Jun 2000 15:42:46 -0700


Discussion:

This patch is one of a set of five (one per python/dist/src/* directory).
They have been broken up to keep the patches small such that people might
spend the time to review them. If one single patch would be prefered I can
provide that.

This patch makes some simple changes for the benefit of 64-bit platforms (to
avoid possible overflows where C data types have changed sizes and to
eliminate unnecessary warnings). Each change fits into one of the following
categories:

- Change a local variable declaration from int to size_t where appropriate.
  This is typically done for variables that hold strlen() or sizeof()
  results. On 64-bit platforms sizeof(int) < sizeof(size_t), so using size_t
  avoids a downcast warning (or overflow). The variable is often later
  downcast to an int anyway but it is (IMO) more appropriate to do the
  downcast where the downcast is necessary and not before. 32-bit platforms
  are not affected by this change.
- Change (int) casts to (size_t) casts within comparisons where appropriate.
  This can avoid an unnecessary possible overflow (in somecases) and a
  warning on 64-bit platforms.
- Remove pointer downcasts to (long) for comparison (see Objects/object.c).
  This is unreliable on Win64 where sizeof(void*) > sizeof(long).
- Add a check for int overflow and raise and OverflowError where it cannot be
  proven a priori that int will not overflow. This is usually associated with
  string lengths. While it may seem overkill to have a check to ensure that a
  string does not overflow 2G characters it *is* a potential for silent
  overflow.
- [Python/thread_nt.c] Use %p instead of %ld printf formatter for debugging
  output to print a pointer. %ld results in data loss on Win64.


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/Python/ceval.c	Thu Jun  1 00:13:40 2000
--- /home/trentm/main/Apps/Perlium/Python/dist/src/Python/ceval.c	Fri Jun  2 15:53:44 2000
***************
*** 2876,2882 ****
  	}
  	else {
  		char *s = PyString_AsString(prog);
! 		if ((int)strlen(s) != PyString_Size(prog)) {
  			PyErr_SetString(PyExc_ValueError,
  					"embedded '\\0' in exec string");
  			return -1;
--- 2876,2882 ----
  	}
  	else {
  		char *s = PyString_AsString(prog);
! 		if (strlen(s) != (size_t)PyString_Size(prog)) {
  			PyErr_SetString(PyExc_ValueError,
  					"embedded '\\0' in exec string");
  			return -1;
*** /home/trentm/main/contrib/python/dist/src/Python/codecs.c	Thu Jun  1 00:13:40 2000
--- /home/trentm/main/Apps/Perlium/Python/dist/src/Python/codecs.c	Fri Jun  2 15:53:44 2000
***************
*** 83,93 ****
  PyObject *normalizestring(const char *string)
  {
      register int i;
!     int len = strlen(string);
      char *p;
      PyObject *v;
      
!     v = PyString_FromStringAndSize(NULL, len);
      if (v == NULL)
  	return NULL;
      p = PyString_AS_STRING(v);
--- 83,98 ----
  PyObject *normalizestring(const char *string)
  {
      register int i;
!     size_t len = strlen(string);
      char *p;
      PyObject *v;
      
! 	if (len > INT_MAX) {
! 		PyErr_SetString(PyExc_OverflowError, "string is too large");
! 		return NULL;
! 	}
! 	
!     v = PyString_FromStringAndSize(NULL, (int)len);
      if (v == NULL)
  	return NULL;
      p = PyString_AS_STRING(v);
*** /home/trentm/main/contrib/python/dist/src/Python/compile.c	Fri Jun  2 11:21:14 2000
--- /home/trentm/main/Apps/Perlium/Python/dist/src/Python/compile.c	Fri Jun  2 15:53:44 2000
***************
*** 265,272 ****
  		if (!PyString_Check(v))
  			continue;
  		p = PyString_AsString(v);
! 		if ((int)strspn(p, NAME_CHARS)
! 		    != PyString_Size(v))
  			continue;
  		PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
  	}
--- 265,272 ----
  		if (!PyString_Check(v))
  			continue;
  		p = PyString_AsString(v);
! 		if (strspn(p, NAME_CHARS)
! 		    != (size_t)PyString_Size(v))
  			continue;
  		PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
  	}
***************
*** 340,346 ****
  	PyObject *exc;
  	char *msg;
  {
! 	int n = strlen(msg);
  	PyObject *v;
  	char buffer[30];
  	char *s;
--- 340,346 ----
  	PyObject *exc;
  	char *msg;
  {
! 	size_t n = strlen(msg);
  	PyObject *v;
  	char buffer[30];
  	char *s;
***************
*** 720,731 ****
  	struct compiling *c;
  	char *name;
  	char *buffer;
! 	int maxlen;
  {
  	/* Name mangling: __private becomes _classname__private.
  	   This is independent from how the name is used. */
  	char *p;
! 	int nlen, plen;
  	nlen = strlen(name);
  	if (nlen+2 >= maxlen)
  		return 0; /* Don't mangle __extremely_long_names */
--- 720,731 ----
  	struct compiling *c;
  	char *name;
  	char *buffer;
! 	size_t maxlen;
  {
  	/* Name mangling: __private becomes _classname__private.
  	   This is independent from how the name is used. */
  	char *p;
! 	size_t nlen, plen;
  	nlen = strlen(name);
  	if (nlen+2 >= maxlen)
  		return 0; /* Don't mangle __extremely_long_names */
***************
*** 761,767 ****
  	char buffer[256];
  	if (name != NULL && name[0] == '_' && name[1] == '_' &&
  	    c->c_private != NULL &&
! 	    com_mangle(c, name, buffer, (int)sizeof(buffer)))
  		name = buffer;
  #endif
  	if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
--- 761,767 ----
  	char buffer[256];
  	if (name != NULL && name[0] == '_' && name[1] == '_' &&
  	    c->c_private != NULL &&
! 	    com_mangle(c, name, buffer, sizeof(buffer)))
  		name = buffer;
  #endif
  	if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
***************
*** 883,889 ****
  	char *s;
  {
  	PyObject *v;
! 	int len;
  	char *buf;
  	char *p;
  	char *end;
--- 883,889 ----
  	char *s;
  {
  	PyObject *v;
! 	size_t len;
  	char *buf;
  	char *p;
  	char *end;
***************
*** 908,913 ****
--- 908,917 ----
  	}
  	s++;
  	len = strlen(s);
+ 	if (len > INT_MAX) {
+ 		PyErr_SetString(PyExc_OverflowError, "string to parse is too long");
+ 		return NULL;
+ 	}
  	if (s[--len] != quote) {
  		PyErr_BadInternalCall();
  		return NULL;
***************
*** 2201,2207 ****
  		char buffer[256];
  		if (s != NULL && s[0] == '_' && s[1] == '_' &&
  		    c->c_private != NULL &&
! 		    com_mangle(c, s, buffer, (int)sizeof(buffer)))
  			s = buffer;
  #endif
  		if (PyDict_GetItemString(c->c_locals, s) != NULL) {
--- 2205,2211 ----
  		char buffer[256];
  		if (s != NULL && s[0] == '_' && s[1] == '_' &&
  		    c->c_private != NULL &&
! 		    com_mangle(c, s, buffer, sizeof(buffer)))
  			s = buffer;
  #endif
  		if (PyDict_GetItemString(c->c_locals, s) != NULL) {
*** /home/trentm/main/contrib/python/dist/src/Python/dynload_win.c	Thu Jun  1 00:13:40 2000
--- /home/trentm/main/Apps/Perlium/Python/dist/src/Python/dynload_win.c	Fri Jun  2 15:53:44 2000
***************
*** 104,110 ****
  					"DLL load failed with error code %d",
  					errorCode);
  			} else {
! 				int len;
  				/* For some reason a \r\n
  				   is appended to the text */
  				if (theLength >= 2 &&
--- 104,110 ----
  					"DLL load failed with error code %d",
  					errorCode);
  			} else {
! 				size_t len;
  				/* For some reason a \r\n
  				   is appended to the text */
  				if (theLength >= 2 &&
*** /home/trentm/main/contrib/python/dist/src/Python/getcwd.c	Thu Jun  1 00:13:40 2000
--- /home/trentm/main/Apps/Perlium/Python/dist/src/Python/getcwd.c	Fri Jun  2 15:53:44 2000
***************
*** 62,68 ****
  		return NULL;
  	}
  	ret = getwd(localbuf);
! 	if (ret != NULL && strlen(localbuf) >= size) {
  		errno = ERANGE;
  		return NULL;
  	}
--- 62,68 ----
  		return NULL;
  	}
  	ret = getwd(localbuf);
! 	if (ret != NULL && strlen(localbuf) >= (size_t)size) {
  		errno = ERANGE;
  		return NULL;
  	}
*** /home/trentm/main/contrib/python/dist/src/Python/modsupport.c	Thu Jun  1 00:13:40 2000
--- /home/trentm/main/Apps/Perlium/Python/dist/src/Python/modsupport.c	Fri Jun  2 15:53:44 2000
***************
*** 355,362 ****
  				Py_INCREF(v);
  			}
  			else {
! 				if (n < 0)
! 					n = strlen(str);
  				v = PyString_FromStringAndSize(str, n);
  			}
  			return v;
--- 355,369 ----
  				Py_INCREF(v);
  			}
  			else {
! 				if (n < 0) {
! 					size_t m = strlen(str);
! 					if (m > INT_MAX) {
! 						PyErr_SetString(PyExc_OverflowError,
! 							"string too long for Python string");
! 						return NULL;
! 					}
! 					n = (int)m;
! 				}
  				v = PyString_FromStringAndSize(str, n);
  			}
  			return v;
*** /home/trentm/main/contrib/python/dist/src/Python/sysmodule.c	Thu Jun  1 00:13:40 2000
--- /home/trentm/main/Apps/Perlium/Python/dist/src/Python/sysmodule.c	Fri Jun  2 15:53:44 2000
***************
*** 491,497 ****
  	Py_XDECREF(v);
  #ifdef MS_COREDLL
  	PyDict_SetItemString(sysdict, "dllhandle",
! 			     v = PyInt_FromLong((int)PyWin_DLLhModule));
  	Py_XDECREF(v);
  	PyDict_SetItemString(sysdict, "winver",
  			     v = PyString_FromString(PyWin_DLLVersionString));
--- 491,497 ----
  	Py_XDECREF(v);
  #ifdef MS_COREDLL
  	PyDict_SetItemString(sysdict, "dllhandle",
! 			     v = PyLong_FromVoidPtr(PyWin_DLLhModule));
  	Py_XDECREF(v);
  	PyDict_SetItemString(sysdict, "winver",
  			     v = PyString_FromString(PyWin_DLLVersionString));
*** /home/trentm/main/contrib/python/dist/src/Python/thread_nt.h	Fri Jun  2 11:21:14 2000
--- /home/trentm/main/Apps/Perlium/Python/dist/src/Python/thread_nt.h	Fri Jun  2 15:53:44 2000
***************
*** 101,106 ****
--- 101,109 ----
  	return mutex->hevent != NULL ;	/* TRUE if the mutex is created */
  }
  
+ #ifdef InterlockedCompareExchange
+ #undef InterlockedCompareExchange
+ #endif
  #define InterlockedCompareExchange(dest,exchange,comperand) (ixchg((dest), (exchange), (comperand)))
  
  VOID DeleteNonRecursiveMutex(PNRMUTEX mutex)
***************
*** 179,185 ****
   */
  int PyThread_start_new_thread(void (*func)(void *), void *arg)
  {
! 	long rv;
  	int success = 0;
  
  	dprintf(("%ld: PyThread_start_new_thread called\n", PyThread_get_thread_ident()));
--- 182,188 ----
   */
  int PyThread_start_new_thread(void (*func)(void *), void *arg)
  {
! 	INT_PTR rv;
  	int success = 0;
  
  	dprintf(("%ld: PyThread_start_new_thread called\n", PyThread_get_thread_ident()));
***************
*** 190,196 ****
   
  	if (rv != -1) {
  		success = 1;
! 		dprintf(("%ld: PyThread_start_new_thread succeeded: %ld\n", PyThread_get_thread_ident(), rv));
  	}
  
  	return success;
--- 193,199 ----
   
  	if (rv != -1) {
  		success = 1;
! 		dprintf(("%ld: PyThread_start_new_thread succeeded: %p\n", PyThread_get_thread_ident(), rv));
  	}
  
  	return success;
*** /home/trentm/main/contrib/python/dist/src/Python/traceback.c	Thu Jun  1 00:13:40 2000
--- /home/trentm/main/Apps/Perlium/Python/dist/src/Python/traceback.c	Fri Jun  2 15:53:44 2000
***************
*** 166,172 ****
  		path = PySys_GetObject("path");
  		if (path != NULL && PyList_Check(path)) {
  			int npath = PyList_Size(path);
! 			int taillen = strlen(tail);
  			char namebuf[MAXPATHLEN+1];
  			for (i = 0; i < npath; i++) {
  				PyObject *v = PyList_GetItem(path, i);
--- 166,172 ----
  		path = PySys_GetObject("path");
  		if (path != NULL && PyList_Check(path)) {
  			int npath = PyList_Size(path);
! 			size_t taillen = strlen(tail);
  			char namebuf[MAXPATHLEN+1];
  			for (i = 0; i < npath; i++) {
  				PyObject *v = PyList_GetItem(path, i);
***************
*** 175,186 ****
  					break;
  				}
  				if (PyString_Check(v)) {
! 					int len;
  					len = PyString_Size(v);
  					if (len + 1 + taillen >= MAXPATHLEN)
  						continue; /* Too long */
  					strcpy(namebuf, PyString_AsString(v));
! 					if ((int)strlen(namebuf) != len)
  						continue; /* v contains '\0' */
  					if (len > 0 && namebuf[len-1] != SEP)
  						namebuf[len++] = SEP;
--- 175,186 ----
  					break;
  				}
  				if (PyString_Check(v)) {
! 					size_t len;
  					len = PyString_Size(v);
  					if (len + 1 + taillen >= MAXPATHLEN)
  						continue; /* Too long */
  					strcpy(namebuf, PyString_AsString(v));
! 					if (strlen(namebuf) != len)
  						continue; /* v contains '\0' */
  					if (len > 0 && namebuf[len-1] != SEP)
  						namebuf[len++] = SEP;


-- 
Trent Mick
trentm@activestate.com