Skip to content

Commit

Permalink
Merge pull request #1401 from johnhaddon/compoundObjectBindingFix
Browse files Browse the repository at this point in the history
CompoundObjectBinding : Fix null dereference
  • Loading branch information
johnhaddon authored Jan 17, 2024
2 parents 2d7eb96 + ec056c1 commit d293bc9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
5 changes: 5 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Improvements

- USDScene : Added basic loading of UsdGeomNurbsCurves, converting them to CurvesPrimitives.

Fixes
-----

- CompoundObject : Fixed crashes in Python bindings caused by passing `None` as a key.

10.5.4.2 (relative to 10.5.4.1)
========

Expand Down
31 changes: 10 additions & 21 deletions src/IECorePython/CompoundObjectBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,47 +101,36 @@ static unsigned int len( const CompoundObject &o )
return o.members().size();
}

static ObjectPtr getItem( const CompoundObject &o, const char *n )
static ObjectPtr getItem( const CompoundObject &o, const InternedString &n )
{
CompoundObject::ObjectMap::const_iterator it = o.members().find( n );
if( it==o.members().end() )
{
PyErr_SetString( PyExc_KeyError, n );
PyErr_SetString( PyExc_KeyError, n.c_str() );
throw_error_already_set();
}
return it->second;
}

static void setItem( CompoundObject &o, const char *n, Object &v )
static void setItem( CompoundObject &o, const InternedString &n, Object &v )
{
o.members()[n] = &v;
}

static void delItem( CompoundObject &o, const char *n )
static void delItem( CompoundObject &o, const InternedString &n )
{
CompoundObject::ObjectMap::iterator it = o.members().find( n );
if( it==o.members().end() )
{
PyErr_SetString( PyExc_KeyError, n );
PyErr_SetString( PyExc_KeyError, n.c_str() );
throw_error_already_set();
}
o.members().erase( it );
}

static bool contains( const CompoundObject &o, const char *n )
static bool contains( const CompoundObject &o, const InternedString &n )
{
CompoundObject::ObjectMap::const_iterator it = o.members().find( n );
if( it==o.members().end() )
{
return false;
}
return true;
}

static bool has_key( const CompoundObject &o, const char *n )
{
CompoundObject::ObjectMap::const_iterator it = o.members().find( n );
return ( it != o.members().end() );
return o.members().find( n ) != o.members().end();
}

static boost::python::list keys( const CompoundObject &o )
Expand Down Expand Up @@ -226,7 +215,7 @@ class CompoundObjectFromPythonDict
{
key = keys[i];
value = v[key];
extract<const char *> keyElem(key);
extract<InternedString> keyElem(key);
if (!keyElem.check())
{
PyErr_SetString(PyExc_TypeError, "Incompatible key type. Only strings accepted.");
Expand Down Expand Up @@ -279,7 +268,7 @@ static CompoundObjectPtr copyConstructor( ConstCompoundObjectPtr other )
}

/// binding for get method
static ObjectPtr get( const CompoundObject &o, const char *key, ObjectPtr defaultValue )
static ObjectPtr get( const CompoundObject &o, const IECore::InternedString &key, ObjectPtr defaultValue )
{
CompoundObject::ObjectMap::const_iterator it = o.members().find( key );
if ( it == o.members().end() )
Expand Down Expand Up @@ -307,7 +296,7 @@ void bindCompoundObject()
.def( "__setitem__", &setItem )
.def( "__delitem__", &delItem )
.def( "__contains__", &contains )
.def( "has_key", &has_key )
.def( "has_key", &contains )
.def( "keys", &keys )
.def( "values", &values )
.def( "items", &items )
Expand Down
18 changes: 18 additions & 0 deletions test/IECore/CompoundObject.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,24 @@ def testHash( self ) :
self.assertEqual( h, o.hash() )
h = o.hash()

def testNoneKey( self ) :

o = IECore.CompoundObject()
with self.assertRaisesRegex( Exception, r"Python argument types" ) :
o.get( None )
with self.assertRaisesRegex( Exception, r"Python argument types" ) :
o[None]
with self.assertRaisesRegex( Exception, r"Python argument types" ) :
o[None] = IECore.IntData( 10 )
with self.assertRaisesRegex( Exception, r"Python argument types" ) :
del o[None]
with self.assertRaisesRegex( Exception, r"Python argument types" ) :
None in o
with self.assertRaisesRegex( Exception, r"Python argument types" ) :
o.has_key( None )
with self.assertRaisesRegex( Exception, r"Incompatible key type" ) :
IECore.CompoundObject( { None : IECore.IntData( 10 ) } )

if __name__ == "__main__":
unittest.main()

0 comments on commit d293bc9

Please sign in to comment.