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

Trent Mick trentm@activestate.com
Mon, 5 Jun 2000 15:41:44 -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/PC/getpathp.c	Thu Jun  1 00:13:40 2000
--- /home/trentm/main/Apps/Perlium/Python/dist/src/PC/getpathp.c	Fri Jun  2 15:53:43 2000
***************
*** 134,140 ****
  reduce(dir)
  	char *dir;
  {
! 	int i = strlen(dir);
  	while (i > 0 && !is_sep(dir[i]))
  		--i;
  	dir[i] = '\0';
--- 134,140 ----
  reduce(dir)
  	char *dir;
  {
! 	size_t i = strlen(dir);
  	while (i > 0 && !is_sep(dir[i]))
  		--i;
  	dir[i] = '\0';
***************
*** 172,178 ****
  	char *buffer;
  	char *stuff;
  {
! 	int n, k;
  	if (is_sep(stuff[0]))
  		n = 0;
  	else {
--- 172,178 ----
  	char *buffer;
  	char *stuff;
  {
! 	size_t n, k;
  	if (is_sep(stuff[0]))
  		n = 0;
  	else {
***************
*** 207,213 ****
  	char *argv0_path;
  	char *landmark;
  {
- 
  	/* Search from argv0_path, until landmark is found */
  	strcpy(prefix, argv0_path);
  	do {
--- 207,212 ----
***************
*** 244,250 ****
  	TCHAR *dataBuf = NULL;
  	static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\");
  	static const TCHAR keySuffix[] = _T("\\PythonPath");
! 	int versionLen;
  	DWORD index;
  	TCHAR *keyBuf = NULL;
  	TCHAR *keyBufPtr;
--- 243,249 ----
  	TCHAR *dataBuf = NULL;
  	static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\");
  	static const TCHAR keySuffix[] = _T("\\PythonPath");
! 	size_t versionLen;
  	DWORD index;
  	TCHAR *keyBuf = NULL;
  	TCHAR *keyBufPtr;
***************
*** 402,408 ****
  			char *delim = strchr(path, DELIM);
  
  			if (delim) {
! 				int len = delim - path;
  				strncpy(progpath, path, len);
  				*(progpath + len) = '\0';
  			}
--- 401,407 ----
  			char *delim = strchr(path, DELIM);
  
  			if (delim) {
! 				size_t len = delim - path;
  				strncpy(progpath, path, len);
  				*(progpath + len) = '\0';
  			}
***************
*** 429,435 ****
  {
  	char argv0_path[MAXPATHLEN+1];
  	char *buf;
! 	int bufsz;
  	char *pythonhome = Py_GetPythonHome();
  	char *envpath = getenv("PYTHONPATH");
  
--- 428,434 ----
  {
  	char argv0_path[MAXPATHLEN+1];
  	char *buf;
! 	size_t bufsz;
  	char *pythonhome = Py_GetPythonHome();
  	char *envpath = getenv("PYTHONPATH");
  
***************
*** 554,560 ****
  	else {
  		char *p = PYTHONPATH;
  		char *q;
! 		int n;
  		for (;;) {
  			q = strchr(p, DELIM);
  			if (q == NULL)
--- 553,559 ----
  	else {
  		char *p = PYTHONPATH;
  		char *q;
! 		size_t n;
  		for (;;) {
  			q = strchr(p, DELIM);
  			if (q == NULL)
*** /home/trentm/main/contrib/python/dist/src/PC/import_nt.c	Thu Jun  1 00:13:40 2000
--- /home/trentm/main/Apps/Perlium/Python/dist/src/PC/import_nt.c	Fri Jun  2 15:53:43 2000
***************
*** 33,39 ****
  
  	// Calculate the size for the sprintf buffer.
  	// Get the size of the chars only, plus 1 NULL.
! 	int bufSize = sizeof(keyPrefix)-1 + strlen(PyWin_DLLVersionString) + sizeof(keySuffix) + strlen(moduleName) + sizeof(debugString) - 1;
  	// alloca == no free required, but memory only local to fn, also no heap fragmentation!
  	moduleKey = alloca(bufSize); 
  	sprintf(moduleKey, "Software\\Python\\PythonCore\\%s\\Modules\\%s%s", PyWin_DLLVersionString, moduleName, debugString);
--- 33,39 ----
  
  	// Calculate the size for the sprintf buffer.
  	// Get the size of the chars only, plus 1 NULL.
! 	size_t bufSize = sizeof(keyPrefix)-1 + strlen(PyWin_DLLVersionString) + sizeof(keySuffix) + strlen(moduleName) + sizeof(debugString) - 1;
  	// alloca == no free required, but memory only local to fn, also no heap fragmentation!
  	moduleKey = alloca(bufSize); 
  	sprintf(moduleKey, "Software\\Python\\PythonCore\\%s\\Modules\\%s%s", PyWin_DLLVersionString, moduleName, debugString);
***************
*** 44,50 ****
  		return NULL;
  	// use the file extension to locate the type entry.
  	for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
! 		int extLen=strlen(fdp->suffix);
  		if (modNameSize>extLen && strnicmp(pathBuf+(modNameSize-extLen-1),fdp->suffix,extLen)==0)
  			break;
  	}
--- 44,50 ----
  		return NULL;
  	// use the file extension to locate the type entry.
  	for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
! 		size_t extLen = strlen(fdp->suffix);
  		if (modNameSize>extLen && strnicmp(pathBuf+(modNameSize-extLen-1),fdp->suffix,extLen)==0)
  			break;
  	}




-- 
Trent Mick
trentm@activestate.com