]> granicus.if.org Git - python/commitdiff
Fix for #1236090: FSSpec.as_pathname() crashes.
authorJack Jansen <jack.jansen@cwi.nl>
Tue, 12 Jul 2005 21:25:05 +0000 (21:25 +0000)
committerJack Jansen <jack.jansen@cwi.nl>
Tue, 12 Jul 2005 21:25:05 +0000 (21:25 +0000)
Turns out patch #1035255 was incomplete, it only patched _Filemodule.c
and not filesupport.py. So regenerating caused as_pathname() to go into
an infinite loop.

Mac/Modules/file/_Filemodule.c
Mac/Modules/file/filesupport.py

index c3d1abf300ab747b7981a6e350dd1a7c77ca24c2..36ef03ce9cb59f987d79516ebf7acc48ca5d8e65 100644 (file)
@@ -92,6 +92,50 @@ PyMac_BuildHFSUniStr255(HFSUniStr255 *itself)
         return Py_BuildValue("u#", itself->unicode, itself->length);
 }
 
+static OSErr
+_PyMac_GetFullPathname(FSSpec *fss, char *path, int len)
+{
+       FSRef fsr;
+       OSErr err;
+
+       *path = '\0';
+       err = FSpMakeFSRef(fss, &fsr);
+       if (err == fnfErr) {
+               /* FSSpecs can point to non-existing files, fsrefs can't. */
+               FSSpec fss2;
+               int tocopy;
+
+               err = FSMakeFSSpec(fss->vRefNum, fss->parID, "", &fss2);
+               if (err)
+                       return err;
+               err = FSpMakeFSRef(&fss2, &fsr);
+               if (err)
+                       return err;
+               err = (OSErr)FSRefMakePath(&fsr, path, len-1);
+               if (err)
+                       return err;
+               /* This part is not 100% safe: we append the filename part, but
+               ** I'm not sure that we don't run afoul of the various 8bit
+               ** encodings here. Will have to look this up at some point...
+               */
+               strcat(path, "/");
+               tocopy = fss->name[0];
+               if ((strlen(path) + tocopy) >= len)
+                       tocopy = len - strlen(path) - 1;
+               if (tocopy > 0)
+                       strncat(path, fss->name+1, tocopy);
+       }
+       else {
+               if (err)
+                       return err;
+               err = (OSErr)FSRefMakePath(&fsr, path, len);
+               if (err)
+                       return err;
+       }
+       return 0;
+}
+
+
 static PyObject *File_Error;
 
 /* ------------------- Object type FSCatalogInfo -------------------- */
@@ -1265,7 +1309,7 @@ static PyObject *FSSpec_as_pathname(FSSpecObject *_self, PyObject *_args)
 
        if (!PyArg_ParseTuple(_args, ""))
                return NULL;
-       err = PyMac_GetFullPathname(&_self->ob_itself, strbuf, sizeof(strbuf));
+       err = _PyMac_GetFullPathname(&_self->ob_itself, strbuf, sizeof(strbuf));
        if ( err ) {
                PyMac_Error(err);
                return NULL;
index 15ad8a3cc942de47e6906d196586132f73d1fb55..a6ac5362a4e10d0e584c6083de6fe4fb5c8a626f 100644 (file)
@@ -198,6 +198,53 @@ PyMac_BuildHFSUniStr255(HFSUniStr255 *itself)
 
         return Py_BuildValue("u#", itself->unicode, itself->length);
 }
+
+/*
+** Get pathname for a given FSSpec
+*/
+static OSErr
+_PyMac_GetFullPathname(FSSpec *fss, char *path, int len)
+{
+       FSRef fsr;
+       OSErr err;
+
+       *path = '\0';
+       err = FSpMakeFSRef(fss, &fsr);
+       if (err == fnfErr) {
+               /* FSSpecs can point to non-existing files, fsrefs can't. */
+               FSSpec fss2;
+               int tocopy;
+
+               err = FSMakeFSSpec(fss->vRefNum, fss->parID, "", &fss2);
+               if (err)
+                       return err;
+               err = FSpMakeFSRef(&fss2, &fsr);
+               if (err)
+                       return err;
+               err = (OSErr)FSRefMakePath(&fsr, path, len-1);
+               if (err)
+                       return err;
+               /* This part is not 100% safe: we append the filename part, but
+               ** I'm not sure that we don't run afoul of the various 8bit
+               ** encodings here. Will have to look this up at some point...
+               */
+               strcat(path, "/");
+               tocopy = fss->name[0];
+               if ((strlen(path) + tocopy) >= len)
+                       tocopy = len - strlen(path) - 1;
+               if (tocopy > 0)
+                       strncat(path, fss->name+1, tocopy);
+       }
+       else {
+               if (err)
+                       return err;
+               err = (OSErr)FSRefMakePath(&fsr, path, len);
+               if (err)
+                       return err;
+       }
+       return 0;
+}
+
 """
 
 finalstuff = finalstuff + """
@@ -798,7 +845,7 @@ OSErr err;
 
 if (!PyArg_ParseTuple(_args, ""))
         return NULL;
-err = PyMac_GetFullPathname(&_self->ob_itself, strbuf, sizeof(strbuf));
+err = _PyMac_GetFullPathname(&_self->ob_itself, strbuf, sizeof(strbuf));
 if ( err ) {
         PyMac_Error(err);
         return NULL;