[Patches] trailing backslash breaks os.stat

Guido van Rossum guido@python.org
Fri, 21 Apr 2000 14:50:02 -0400


Brian,

Thanks for this patch.  As presented, it's got one flaw: it modifies
an incoming string object.  I've changed this by making a copy in a
buffer on the stack.

I'm also taking the liberty to swap the tests for trailing backslash
with the test for the drive root (and add a comment to the latter).
Here's the modified patch, which I'll check in in a bit:

Index: posixmodule.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Modules/posixmodule.c,v
retrieving revision 2.130
diff -c -r2.130 posixmodule.c
*** posixmodule.c	2000/04/13 15:20:40	2.130
--- posixmodule.c	2000/04/21 18:48:42
***************
*** 549,556 ****
--- 549,584 ----
  	struct stat st;
  	char *path;
  	int res;
+ 
+ #ifdef MS_WIN32
+       int pathlen;
+       char pathcopy[MAX_PATH];
+ #endif /* MS_WIN32 */
+ 
  	if (!PyArg_ParseTuple(args, format, &path))
  		return NULL;
+ 
+ #ifdef MS_WIN32
+ 	pathlen = strlen(path);
+ 	/* the library call can blow up if the file name is too long! */
+ 	if (pathlen > MAX_PATH) {
+ 		errno = ENAMETOOLONG;
+ 		return posix_error();
+ 	}
+ 
+ 	if ((pathlen > 0) && (path[pathlen-1] == '\\' || path[pathlen-1] == '/')) {
+ 		/* exception for drive root */
+ 		if (!((pathlen == 3) &&
+ 		      (path[1] == ':') &&
+ 		      (path[2] == '\\' || path[2] == '/')))
+ 		{
+ 			strncpy(pathcopy, path, pathlen);
+ 			pathcopy[pathlen-1] = '\0'; /* nuke the trailing backslash */
+ 			path = pathcopy;
+ 		}
+ 	}
+ #endif /* MS_WIN32 */
+ 
  	Py_BEGIN_ALLOW_THREADS
  	res = (*statfunc)(path, &st);
  	Py_END_ALLOW_THREADS

--Guido van Rossum (home page: http://www.python.org/~guido/)