Skip to content

Commit

Permalink
Thumbnail keeps reference to Exif data
Browse files Browse the repository at this point in the history
This reduces the risk of a segfault if the exif data (and hence image)
gets deleted.
  • Loading branch information
jim-easterbrook committed Nov 29, 2023
1 parent 1cac464 commit 1a7dd16
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/interface/exif.i
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
%import "metadatum.i"
%import "tags.i"

// ExifThumb keeps a reference to the ExifData it uses
%typemap(ret) Exiv2::ExifThumb* %{
if ($result != Py_None) {
if (PyObject_SetAttrString($result, "_refers_to", swig_obj[0])) {
SWIG_fail;
}
}
%}

#if EXIV2_VERSION_HEX < 0x001c0000
INPUT_BUFFER_RO(const Exiv2::byte* buf, long size)
#else
Expand Down
7 changes: 7 additions & 0 deletions src/swig-0_27_0/exif_wrap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9369,6 +9369,13 @@ SWIGINTERN int _wrap_new_ExifThumb(PyObject *self, PyObject *args, PyObject *kwa
}
}
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Exiv2__ExifThumb, SWIG_BUILTIN_INIT | 0 );

if (resultobj != Py_None) {
if (PyObject_SetAttrString(resultobj, "_refers_to", swig_obj[0])) {
SWIG_fail;
}
}

return resultobj == Py_None ? -1 : 0;
fail:
return -1;
Expand Down
7 changes: 7 additions & 0 deletions src/swig-0_27_4/exif_wrap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9369,6 +9369,13 @@ SWIGINTERN int _wrap_new_ExifThumb(PyObject *self, PyObject *args, PyObject *kwa
}
}
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Exiv2__ExifThumb, SWIG_BUILTIN_INIT | 0 );

if (resultobj != Py_None) {
if (PyObject_SetAttrString(resultobj, "_refers_to", swig_obj[0])) {
SWIG_fail;
}
}

return resultobj == Py_None ? -1 : 0;
fail:
return -1;
Expand Down
7 changes: 7 additions & 0 deletions src/swig-0_28_0/exif_wrap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9803,6 +9803,13 @@ SWIGINTERN int _wrap_new_ExifThumb(PyObject *self, PyObject *args, PyObject *kwa
}
}
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Exiv2__ExifThumb, SWIG_BUILTIN_INIT | 0 );

if (resultobj != Py_None) {
if (PyObject_SetAttrString(resultobj, "_refers_to", swig_obj[0])) {
SWIG_fail;
}
}

return resultobj == Py_None ? -1 : 0;
fail:
return -1;
Expand Down
6 changes: 6 additions & 0 deletions tests/test_exif.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ def test_ref_counts(self):
self.assertEqual(sys.getrefcount(self.image), 2)
data = self.image.exifData()
self.assertEqual(sys.getrefcount(self.image), 3)
# thumbnail keeps a reference to exifData
self.assertEqual(sys.getrefcount(data), 2)
thumb = exiv2.ExifThumb(data)
self.assertEqual(sys.getrefcount(data), 3)
del thumb
self.assertEqual(sys.getrefcount(data), 2)
# iterator keeps a reference to data
self.assertEqual(sys.getrefcount(data), 2)
b = data.begin()
Expand Down

0 comments on commit 1a7dd16

Please sign in to comment.