[vtkusers] python reader.GetFileName() seg faults

David Gobbi dgobbi at irus.rri.on.ca
Sun Jul 30 23:50:58 EDT 2000


I've just committed a fix to CVS.

The problem:  if (this->FileName == NULL), then when 
GetFileName() is called, Python will try to turn NULL
into a string and segfault as a result.

solution 1: if (this->FileName == NULL), return None

solution 2: if (this->FileName == NULL), return '' (empty string)

My preference was #1, because it allows you to distinguish between
NULL and "", while #2 converts NULL to '' and doesn't allow you
to distinguish the two.  

The changes are made in vtkWrapPython.c, i.e. they impact all methods
that return strings, not just vtkImageReader::GetFileName().
I would have replied to this email earlier, but I was at a conference
last week.

 - David

P.S. I found a bug in vtkImageReader::SetFileName() that was unrelated
     to the python wrappers, if you SetFileName("<anything>") and then
     SetFileName(NULL) then you get a segfault.  This bug might also
     be present in other readers.

--
  David Gobbi, MSc                    dgobbi at irus.rri.on.ca
  Advanced Imaging Research Group
  Robarts Research Institute, University of Western Ontario
  

On Thu, 27 Jul 2000, Randy Heiland wrote:

> On Jul 27, 12:42pm, Chris Myers wrote:
> > Subject: Re: [vtkusers] python reader.GetFileName() seg faults
> > Randy,
> >
> > Re: Python seg faulting with reader.GetFileName()...
> >
> > >From the trace below, you can see that Python is seg faulting in
> > PyString_FromString().  Poking around comp.lang.python, I find that,
> > at least as of a few months ago (i.e., in version 1.5.2),
> > PyString_FromString applied to a NULL char* pointer will seg fault
> > rather than return Py_None, i.e., that one must explicitly check that
> > the pointer is not NULL before calling the string conversion function.
> >
> > I am running Python 1.5.2; you are running 1.6a2.  Perhaps Python
> > has changed the behavior of PyString_FromString in 1.6a2, but I haven't
> > yet found any explicit mention of that in the 1.6 Change Logs.
> >
> > I am running a reasonably recent nightly release of VTK (a few weeks
> > old), so I doubt that the Python wrapper generation is significantly
> > different between your version and mine.  Looking at
> > python/vtkUnstructuredGridReaderPython.cxx, I find the following
> > fragment:
> >
> >    if (PyArg_ParseTuple(args, ""))
> >    {
> > 	if (!error)
> >         {
> >              temp20 = op->GetFileName();
> >              return PyString_FromString(temp20);
> >         }
> >    }
> >
> > If you have that file still lying around, you might want to check
> > whether the pointer is being checked before the function call,
> > but I suspect the difference is in the upgrade to Python.
> 
> It's not -- very same code as yours.
> 
> >
> > At the very least, the VTK wrapper code generation seems insufficient
> > for Python 1.5.2.
> >
> > Can this be easily fixed in VTK?
> 
> Actually, I just tried it using:
> 
> Python 1.5.2 (#10, Jan  4 2000, 08:17:24) [C] on irix646
> 
> and it worked too.  Not sure what 1.5.2 build you were using.  Guess I'd
> recommend that you update your Python - which would mean rebuilding VTK :(
> Of course, I don't thoroughly understand your predicament.  Why can't you just
> set a dummy filename and check for it later?
> 
> As David Gobbi has pointed out recently, yes, there are known problems with the
> Python wrappers - which you haven't stumbled upon yet.
> 
> --Randy
> 
> _______________________________________________
> This is the private VTK discussion list. 
> Please keep messages on-topic. Check the FAQ at: <http://public.kitware.com/cgi-bin/vtkfaq>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
> 





More information about the vtkusers mailing list