Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CompoundObjectBinding : Fix null dereference #1401

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()

Loading