Skip to content

Commit d293bc9

Browse files
authored
Merge pull request #1401 from johnhaddon/compoundObjectBindingFix
CompoundObjectBinding : Fix null dereference
2 parents 2d7eb96 + ec056c1 commit d293bc9

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

Changes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ Improvements
66

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

9+
Fixes
10+
-----
11+
12+
- CompoundObject : Fixed crashes in Python bindings caused by passing `None` as a key.
13+
914
10.5.4.2 (relative to 10.5.4.1)
1015
========
1116

src/IECorePython/CompoundObjectBinding.cpp

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -101,47 +101,36 @@ static unsigned int len( const CompoundObject &o )
101101
return o.members().size();
102102
}
103103

104-
static ObjectPtr getItem( const CompoundObject &o, const char *n )
104+
static ObjectPtr getItem( const CompoundObject &o, const InternedString &n )
105105
{
106106
CompoundObject::ObjectMap::const_iterator it = o.members().find( n );
107107
if( it==o.members().end() )
108108
{
109-
PyErr_SetString( PyExc_KeyError, n );
109+
PyErr_SetString( PyExc_KeyError, n.c_str() );
110110
throw_error_already_set();
111111
}
112112
return it->second;
113113
}
114114

115-
static void setItem( CompoundObject &o, const char *n, Object &v )
115+
static void setItem( CompoundObject &o, const InternedString &n, Object &v )
116116
{
117117
o.members()[n] = &v;
118118
}
119119

120-
static void delItem( CompoundObject &o, const char *n )
120+
static void delItem( CompoundObject &o, const InternedString &n )
121121
{
122122
CompoundObject::ObjectMap::iterator it = o.members().find( n );
123123
if( it==o.members().end() )
124124
{
125-
PyErr_SetString( PyExc_KeyError, n );
125+
PyErr_SetString( PyExc_KeyError, n.c_str() );
126126
throw_error_already_set();
127127
}
128128
o.members().erase( it );
129129
}
130130

131-
static bool contains( const CompoundObject &o, const char *n )
131+
static bool contains( const CompoundObject &o, const InternedString &n )
132132
{
133-
CompoundObject::ObjectMap::const_iterator it = o.members().find( n );
134-
if( it==o.members().end() )
135-
{
136-
return false;
137-
}
138-
return true;
139-
}
140-
141-
static bool has_key( const CompoundObject &o, const char *n )
142-
{
143-
CompoundObject::ObjectMap::const_iterator it = o.members().find( n );
144-
return ( it != o.members().end() );
133+
return o.members().find( n ) != o.members().end();
145134
}
146135

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

281270
/// binding for get method
282-
static ObjectPtr get( const CompoundObject &o, const char *key, ObjectPtr defaultValue )
271+
static ObjectPtr get( const CompoundObject &o, const IECore::InternedString &key, ObjectPtr defaultValue )
283272
{
284273
CompoundObject::ObjectMap::const_iterator it = o.members().find( key );
285274
if ( it == o.members().end() )
@@ -307,7 +296,7 @@ void bindCompoundObject()
307296
.def( "__setitem__", &setItem )
308297
.def( "__delitem__", &delItem )
309298
.def( "__contains__", &contains )
310-
.def( "has_key", &has_key )
299+
.def( "has_key", &contains )
311300
.def( "keys", &keys )
312301
.def( "values", &values )
313302
.def( "items", &items )

test/IECore/CompoundObject.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,24 @@ def testHash( self ) :
223223
self.assertEqual( h, o.hash() )
224224
h = o.hash()
225225

226+
def testNoneKey( self ) :
227+
228+
o = IECore.CompoundObject()
229+
with self.assertRaisesRegex( Exception, r"Python argument types" ) :
230+
o.get( None )
231+
with self.assertRaisesRegex( Exception, r"Python argument types" ) :
232+
o[None]
233+
with self.assertRaisesRegex( Exception, r"Python argument types" ) :
234+
o[None] = IECore.IntData( 10 )
235+
with self.assertRaisesRegex( Exception, r"Python argument types" ) :
236+
del o[None]
237+
with self.assertRaisesRegex( Exception, r"Python argument types" ) :
238+
None in o
239+
with self.assertRaisesRegex( Exception, r"Python argument types" ) :
240+
o.has_key( None )
241+
with self.assertRaisesRegex( Exception, r"Incompatible key type" ) :
242+
IECore.CompoundObject( { None : IECore.IntData( 10 ) } )
243+
226244
if __name__ == "__main__":
227245
unittest.main()
228246

0 commit comments

Comments
 (0)