Skip to content
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
Calling isinstance(x,t) repeatedly when x or t is not actually a CLR …
…object will leak reference counts to PyFalse, resulting in memory corruption once the reference count drops to zero and PyFalse gets freed
  • Loading branch information
Arvid Bessen committed Oct 18, 2016
commit b68e7a1e5dc5cab7238663dbea9f812aca99c857
12 changes: 9 additions & 3 deletions src/runtime/metatype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,10 @@ static IntPtr DoInstanceCheck(IntPtr tp, IntPtr args, bool checkType)
{
ClassBase cb = GetManagedObject(tp) as ClassBase;

if (cb == null)
if (cb == null) {
Runtime.Incref(Runtime.PyFalse);
return Runtime.PyFalse;
}

using (PyList argsObj = new PyList(args))
{
Expand All @@ -296,12 +298,16 @@ static IntPtr DoInstanceCheck(IntPtr tp, IntPtr args, bool checkType)
else
otherType = arg.GetPythonType();

if (Runtime.PyObject_TYPE(otherType.Handle) != PyCLRMetaType)
if (Runtime.PyObject_TYPE(otherType.Handle) != PyCLRMetaType) {
Runtime.Incref(Runtime.PyFalse);
return Runtime.PyFalse;
}

ClassBase otherCb = GetManagedObject(otherType.Handle) as ClassBase;
if (otherCb == null)
if (otherCb == null) {
Runtime.Incref(Runtime.PyFalse);
return Runtime.PyFalse;
}

return Converter.ToPython(cb.type.IsAssignableFrom(otherCb.type));
}
Expand Down
15 changes: 15 additions & 0 deletions src/tests/test_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,21 @@ def handler(self, x, args):
self.assertEqual(event_handler.value, 3)
self.assertEqual(len(d.event_handlers), 1)

def test_isinstance(self):
from System import Object
from System import String

a = [str(x) for x in range(0, 1000)]
b = [String(x) for x in a]

for x in a:
self.assertFalse(isinstance(x, Object))
self.assertFalse(isinstance(x, String))

for x in b:
self.assertTrue(isinstance(x, Object))
self.assertTrue(isinstance(x, String))


def test_suite():
return unittest.makeSuite(SubClassTests)
Expand Down