Opened 11 years ago

Closed 11 years ago

Last modified 10 years ago

#3000 closed defect (fixed)

OGRFeature.SetField segfault

Reported by: lpinner Owned by: Even Rouault
Priority: normal Milestone: 1.6.2
Component: PythonBindings Version: unspecified
Severity: normal Keywords:
Cc: hobu


Error occurs on Windows XP (SP2) & Redhat Linux

GDAL 1.6.0, released 2008/12/04 Python 2.5.4

I passed a unicode string containing a non-ascii character to SetField? which caused a segmentation fault crash.

The string was "Point D´Entrecasteaux" (note backward quote thing) which I get from an external source. Now I'm aware of the problem I strip out the characters and convert the unicode to ascii, but perhaps ogr should just return an error rather than crashing.

Short script to reproduce: import os try: from osgeo import gdal from osgeo import osr from osgeo import ogr except ImportError: import gdal import osr import ogr shapefile='segfault.shp' fieldvalue=u'Point D\xb4Entrecasteaux' fieldname='SOMEFIELD' driver = ogr.GetDriverByName('ESRI Shapefile') shp_ds = driver.CreateDataSource(shapefile) lyr=os.path.splitext(os.path.basename(shapefile))[0] lyr = shp_ds.CreateLayer(lyr,geom_type=ogr.wkbPolygon) fld = ogr.FieldDefn(fieldname, ogr.OFTString) fld.SetWidth(50) lyr.CreateField(fld) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField(fieldname, fieldvalue) #<<<Segmentation fault...

NB - Redhat Linux version: 2.6.9-78.0.8.ELsmp (brewbuilder@…) (gcc version 3.4.6 20060404 (Red Hat 3.4.6-10)) #1 SMP Wed Nov 5 07:14:58 EST 2008

Change History (3)

comment:1 Changed 11 years ago by Even Rouault

Cc: hobu added
Component: OGR_SFPythonBindings
Milestone: 1.6.2
Owner: changed from warmerdam to Even Rouault

comment:2 Changed 11 years ago by Even Rouault

Resolution: fixed
Status: newclosed

I've fixed the crash in trunk (r17050) and in branches/1.6 (r17051).

Basically I think you can't pass unicode python strings this way to GDAL/OGR. So just remove the u in front of u'Point D\xb4Entrecasteaux' and it will work. You are supposed to pass an UTF-8 string, although the shapefile driver would not handle it correctly (yet), but other drivers such as GML, KML, etc... would.

comment:3 in reply to:  2 Changed 10 years ago by lpinner

Replying to rouault:

So just remove the u in front of u'Point D\xb4Entrecasteaux' and it will work.

I get the string from parsing an XML doc so have no control over it. The above was just a short example to reproduce. As a workaround in case anyone else runs into the same issue, the following encoding works fine and gets written to the shapefile correctly:

fieldvalue=u'Point D\xb4Entrecasteaux'.encode('iso-8859-1')

Note: See TracTickets for help on using tickets.