Skip to content

Commit 3b90776

Browse files
Fix segfault if try to set data[invalid key]
1 parent 07d8ab1 commit 3b90776

File tree

12 files changed

+119
-15
lines changed

12 files changed

+119
-15
lines changed

src/interface/shared/containers.i

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ MP_ASS_SUBSCRIPT(Exiv2::base_class, PyObject*,
104104
#if SWIG_VERSION >= 0x040400
105105
invalidate_pointers(py_self, pos);
106106
#endif
107-
self->erase(pos))
107+
self->erase(pos), false)
108108
SQ_CONTAINS(
109109
Exiv2::base_class, self->findKey(Exiv2::key_type(key)) != self->end())
110110

src/interface/shared/slots.i

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020

2121

2222
// Macro to add mp_ass_subscript slot and functions
23-
%define MP_ASS_SUBSCRIPT(type, item_type, setfunc, delfunc)
23+
%define MP_ASS_SUBSCRIPT(type, item_type, setfunc, delfunc, canfail)
2424
// Use %inline so SWIG generates wrappers with type conversions.
2525
// Names start with '_' so it's invisible in normal use.
26+
#if #canfail != "false"
2627
%noexception _setitem_%mangle(type);
2728
%noexception _delitem_%mangle(type);
29+
#endif
2830
%inline %{
2931
static PyObject* _setitem_%mangle(type)(
3032
type* self, char* key, item_type value, PyObject* py_self) {

src/interface/value.i

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ MP_ASS_SUBSCRIPT(Exiv2::LangAltValue, std::string, self->value_[key] = value,
367367
if (pos == self->value_.end())
368368
return PyErr_Format(PyExc_KeyError, "'%s'", key);
369369
self->value_.erase(pos);
370-
})
370+
},)
371371
SQ_CONTAINS(
372372
Exiv2::LangAltValue, self->value_.find(key) != self->value_.end())
373373
%feature("docstring") Exiv2::LangAltValue::keys

src/swig-0_27_7/exif_wrap.cxx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7096,7 +7096,15 @@ SWIGINTERN PyObject *_wrap__setitem_Exiv2_ExifData(PyObject *self, PyObject *arg
70967096
if (obj2) {
70977097
arg3 = obj2;
70987098
}
7099-
result = (PyObject *)_setitem_Exiv2_ExifData(arg1,arg2,arg3,arg4);
7099+
{
7100+
try {
7101+
result = (PyObject *)_setitem_Exiv2_ExifData(arg1,arg2,arg3,arg4);
7102+
}
7103+
catch(std::exception const& e) {
7104+
_set_python_exception();
7105+
SWIG_fail;
7106+
}
7107+
}
71007108
resultobj = result;
71017109
if (alloc2 == SWIG_NEWOBJ) delete[] buf2;
71027110
return resultobj;
@@ -7134,7 +7142,15 @@ SWIGINTERN PyObject *_wrap__delitem_Exiv2_ExifData(PyObject *self, PyObject *arg
71347142
SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "_delitem_Exiv2_ExifData" "', argument " "2"" of type '" "char *""'");
71357143
}
71367144
arg2 = reinterpret_cast< char * >(buf2);
7137-
result = (PyObject *)_delitem_Exiv2_ExifData(arg1,arg2,arg3);
7145+
{
7146+
try {
7147+
result = (PyObject *)_delitem_Exiv2_ExifData(arg1,arg2,arg3);
7148+
}
7149+
catch(std::exception const& e) {
7150+
_set_python_exception();
7151+
SWIG_fail;
7152+
}
7153+
}
71387154
resultobj = result;
71397155
if (alloc2 == SWIG_NEWOBJ) delete[] buf2;
71407156
return resultobj;

src/swig-0_27_7/iptc_wrap.cxx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6894,7 +6894,15 @@ SWIGINTERN PyObject *_wrap__setitem_Exiv2_IptcData(PyObject *self, PyObject *arg
68946894
if (obj2) {
68956895
arg3 = obj2;
68966896
}
6897-
result = (PyObject *)_setitem_Exiv2_IptcData(arg1,arg2,arg3,arg4);
6897+
{
6898+
try {
6899+
result = (PyObject *)_setitem_Exiv2_IptcData(arg1,arg2,arg3,arg4);
6900+
}
6901+
catch(std::exception const& e) {
6902+
_set_python_exception();
6903+
SWIG_fail;
6904+
}
6905+
}
68986906
resultobj = result;
68996907
if (alloc2 == SWIG_NEWOBJ) delete[] buf2;
69006908
return resultobj;
@@ -6932,7 +6940,15 @@ SWIGINTERN PyObject *_wrap__delitem_Exiv2_IptcData(PyObject *self, PyObject *arg
69326940
SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "_delitem_Exiv2_IptcData" "', argument " "2"" of type '" "char *""'");
69336941
}
69346942
arg2 = reinterpret_cast< char * >(buf2);
6935-
result = (PyObject *)_delitem_Exiv2_IptcData(arg1,arg2,arg3);
6943+
{
6944+
try {
6945+
result = (PyObject *)_delitem_Exiv2_IptcData(arg1,arg2,arg3);
6946+
}
6947+
catch(std::exception const& e) {
6948+
_set_python_exception();
6949+
SWIG_fail;
6950+
}
6951+
}
69366952
resultobj = result;
69376953
if (alloc2 == SWIG_NEWOBJ) delete[] buf2;
69386954
return resultobj;

src/swig-0_27_7/xmp_wrap.cxx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6811,7 +6811,15 @@ SWIGINTERN PyObject *_wrap__setitem_Exiv2_XmpData(PyObject *self, PyObject *args
68116811
if (obj2) {
68126812
arg3 = obj2;
68136813
}
6814-
result = (PyObject *)_setitem_Exiv2_XmpData(arg1,arg2,arg3,arg4);
6814+
{
6815+
try {
6816+
result = (PyObject *)_setitem_Exiv2_XmpData(arg1,arg2,arg3,arg4);
6817+
}
6818+
catch(std::exception const& e) {
6819+
_set_python_exception();
6820+
SWIG_fail;
6821+
}
6822+
}
68156823
resultobj = result;
68166824
if (alloc2 == SWIG_NEWOBJ) delete[] buf2;
68176825
return resultobj;
@@ -6849,7 +6857,15 @@ SWIGINTERN PyObject *_wrap__delitem_Exiv2_XmpData(PyObject *self, PyObject *args
68496857
SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "_delitem_Exiv2_XmpData" "', argument " "2"" of type '" "char *""'");
68506858
}
68516859
arg2 = reinterpret_cast< char * >(buf2);
6852-
result = (PyObject *)_delitem_Exiv2_XmpData(arg1,arg2,arg3);
6860+
{
6861+
try {
6862+
result = (PyObject *)_delitem_Exiv2_XmpData(arg1,arg2,arg3);
6863+
}
6864+
catch(std::exception const& e) {
6865+
_set_python_exception();
6866+
SWIG_fail;
6867+
}
6868+
}
68536869
resultobj = result;
68546870
if (alloc2 == SWIG_NEWOBJ) delete[] buf2;
68556871
return resultobj;

src/swig-0_28_7/exif_wrap.cxx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7253,7 +7253,15 @@ SWIGINTERN PyObject *_wrap__setitem_Exiv2_ExifData(PyObject *self, PyObject *arg
72537253
if (obj2) {
72547254
arg3 = obj2;
72557255
}
7256-
result = (PyObject *)_setitem_Exiv2_ExifData(arg1,arg2,arg3,arg4);
7256+
{
7257+
try {
7258+
result = (PyObject *)_setitem_Exiv2_ExifData(arg1,arg2,arg3,arg4);
7259+
}
7260+
catch(std::exception const& e) {
7261+
_set_python_exception();
7262+
SWIG_fail;
7263+
}
7264+
}
72577265
resultobj = result;
72587266
if (alloc2 == SWIG_NEWOBJ) delete[] buf2;
72597267
return resultobj;
@@ -7291,7 +7299,15 @@ SWIGINTERN PyObject *_wrap__delitem_Exiv2_ExifData(PyObject *self, PyObject *arg
72917299
SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "_delitem_Exiv2_ExifData" "', argument " "2"" of type '" "char *""'");
72927300
}
72937301
arg2 = reinterpret_cast< char * >(buf2);
7294-
result = (PyObject *)_delitem_Exiv2_ExifData(arg1,arg2,arg3);
7302+
{
7303+
try {
7304+
result = (PyObject *)_delitem_Exiv2_ExifData(arg1,arg2,arg3);
7305+
}
7306+
catch(std::exception const& e) {
7307+
_set_python_exception();
7308+
SWIG_fail;
7309+
}
7310+
}
72957311
resultobj = result;
72967312
if (alloc2 == SWIG_NEWOBJ) delete[] buf2;
72977313
return resultobj;

src/swig-0_28_7/iptc_wrap.cxx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7058,7 +7058,15 @@ SWIGINTERN PyObject *_wrap__setitem_Exiv2_IptcData(PyObject *self, PyObject *arg
70587058
if (obj2) {
70597059
arg3 = obj2;
70607060
}
7061-
result = (PyObject *)_setitem_Exiv2_IptcData(arg1,arg2,arg3,arg4);
7061+
{
7062+
try {
7063+
result = (PyObject *)_setitem_Exiv2_IptcData(arg1,arg2,arg3,arg4);
7064+
}
7065+
catch(std::exception const& e) {
7066+
_set_python_exception();
7067+
SWIG_fail;
7068+
}
7069+
}
70627070
resultobj = result;
70637071
if (alloc2 == SWIG_NEWOBJ) delete[] buf2;
70647072
return resultobj;
@@ -7096,7 +7104,15 @@ SWIGINTERN PyObject *_wrap__delitem_Exiv2_IptcData(PyObject *self, PyObject *arg
70967104
SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "_delitem_Exiv2_IptcData" "', argument " "2"" of type '" "char *""'");
70977105
}
70987106
arg2 = reinterpret_cast< char * >(buf2);
7099-
result = (PyObject *)_delitem_Exiv2_IptcData(arg1,arg2,arg3);
7107+
{
7108+
try {
7109+
result = (PyObject *)_delitem_Exiv2_IptcData(arg1,arg2,arg3);
7110+
}
7111+
catch(std::exception const& e) {
7112+
_set_python_exception();
7113+
SWIG_fail;
7114+
}
7115+
}
71007116
resultobj = result;
71017117
if (alloc2 == SWIG_NEWOBJ) delete[] buf2;
71027118
return resultobj;

src/swig-0_28_7/xmp_wrap.cxx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7027,7 +7027,15 @@ SWIGINTERN PyObject *_wrap__setitem_Exiv2_XmpData(PyObject *self, PyObject *args
70277027
if (obj2) {
70287028
arg3 = obj2;
70297029
}
7030-
result = (PyObject *)_setitem_Exiv2_XmpData(arg1,arg2,arg3,arg4);
7030+
{
7031+
try {
7032+
result = (PyObject *)_setitem_Exiv2_XmpData(arg1,arg2,arg3,arg4);
7033+
}
7034+
catch(std::exception const& e) {
7035+
_set_python_exception();
7036+
SWIG_fail;
7037+
}
7038+
}
70317039
resultobj = result;
70327040
if (alloc2 == SWIG_NEWOBJ) delete[] buf2;
70337041
return resultobj;
@@ -7065,7 +7073,15 @@ SWIGINTERN PyObject *_wrap__delitem_Exiv2_XmpData(PyObject *self, PyObject *args
70657073
SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "_delitem_Exiv2_XmpData" "', argument " "2"" of type '" "char *""'");
70667074
}
70677075
arg2 = reinterpret_cast< char * >(buf2);
7068-
result = (PyObject *)_delitem_Exiv2_XmpData(arg1,arg2,arg3);
7076+
{
7077+
try {
7078+
result = (PyObject *)_delitem_Exiv2_XmpData(arg1,arg2,arg3);
7079+
}
7080+
catch(std::exception const& e) {
7081+
_set_python_exception();
7082+
SWIG_fail;
7083+
}
7084+
}
70697085
resultobj = result;
70707086
if (alloc2 == SWIG_NEWOBJ) delete[] buf2;
70717087
return resultobj;

tests/test_exif.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ def test_ExifData(self):
129129
self.assertEqual('Exif.Image.Orientation' in data, True)
130130
self.assertIsInstance(data['Exif.Image.Orientation'],
131131
exiv2.Exifdatum_reference)
132+
with self.assertRaises(exiv2.Exiv2Error):
133+
data['Exif.Image.NotAKey'] = 'Text'
132134
# sorting
133135
data.sortByKey()
134136
self.assertEqual(data.begin().key(), 'Exif.Image.Artist')

0 commit comments

Comments
 (0)