From 91aba9f719273778fe0196f9bc8659131c9e2cf8 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Thu, 28 Jul 2016 08:31:03 +0100 Subject: [PATCH] UTL STL container descriptor checks The vector of pointers (just fixed) were not working correctly because the descriptors returned from swig::type_info() were sometimes returning zero. Zero should only be used for void * as the subsequent call to SWIG_ConvertPtr will blindly cast the pointer without checking descriptor. std::vector does not work and will require further changes: specializing traits_info to return 0 and traits_asptr. I tried this and traits_asptr also needs to be added in which seems odd and requires further investigation... Lib/python/pystdcommon.swg: template <> struct traits_info { static swig_type_info *type_info() { static swig_type_info *info = 0; } }; Lib/std/std_common.i: template <> struct traits_asptr { static int asptr(PyObject *obj, void ***val) { void **p; swig_type_info *descriptor = 0; int res = SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0); if (SWIG_IsOK(res)) { if (val) *val = p; } return res; } }; // this is needed, but am not sure this is expected template <> struct traits_asptr { static int asptr(PyObject *obj, void **val) { void **p; swig_type_info *descriptor = 0; int res = SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0); if (SWIG_IsOK(res)) { if (val) *val = p; } return res; } }; --- Lib/octave/octcontainer.swg | 4 ++-- Lib/octave/octstdcommon.swg | 3 ++- Lib/octave/std_map.i | 3 ++- Lib/octave/std_pair.i | 6 ++++-- Lib/python/pycontainer.swg | 4 ++-- Lib/python/pystdcommon.swg | 3 ++- Lib/python/std_map.i | 3 ++- Lib/python/std_multimap.i | 3 ++- Lib/python/std_pair.i | 6 ++++-- Lib/python/std_unordered_map.i | 3 ++- Lib/python/std_unordered_multimap.i | 3 ++- Lib/r/rstdcommon.swg | 3 ++- Lib/ruby/rubycontainer.swg | 17 +++++++---------- Lib/ruby/rubystdcommon.swg | 3 ++- Lib/ruby/std_map.i | 3 ++- Lib/ruby/std_pair.i | 8 ++++---- Lib/scilab/scistdcommon.swg | 3 ++- Lib/typemaps/implicit.swg | 16 ++++++++-------- 18 files changed, 53 insertions(+), 41 deletions(-) diff --git a/Lib/octave/octcontainer.swg b/Lib/octave/octcontainer.swg index 0211b33c6..af58f3aaa 100644 --- a/Lib/octave/octcontainer.swg +++ b/Lib/octave/octcontainer.swg @@ -562,8 +562,8 @@ namespace swig { static int asptr(const octave_value& obj, sequence **seq) { if (!obj.is_defined() || Swig::swig_value_deref(obj)) { sequence *p; - if (SWIG_ConvertPtr(obj,(void**)&p, - swig::type_info(),0) == SWIG_OK) { + swig_type_info *descriptor = swig::type_info(); + if (descriptor && SWIG_IsOK(SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0))) { if (seq) *seq = p; return SWIG_OLDOBJ; } diff --git a/Lib/octave/octstdcommon.swg b/Lib/octave/octstdcommon.swg index 96923f40a..799d369a7 100644 --- a/Lib/octave/octstdcommon.swg +++ b/Lib/octave/octstdcommon.swg @@ -42,7 +42,8 @@ namespace swig { struct traits_asptr { static int asptr(const octave_value& obj, Type **val) { Type *p; - int res = SWIG_ConvertPtr(obj, (void**)&p, type_info(), 0); + swig_type_info *descriptor = type_info(); + int res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res)) { if (val) *val = p; } diff --git a/Lib/octave/std_map.i b/Lib/octave/std_map.i index 7b85a548e..fd15661c3 100644 --- a/Lib/octave/std_map.i +++ b/Lib/octave/std_map.i @@ -98,7 +98,8 @@ res = traits_asptr_stdseq, std::pair >::asptr(items, val); } else { map_type *p; - res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info(),0); + swig_type_info *descriptor = swig::type_info(); + res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res) && val) *val = p; } return res; diff --git a/Lib/octave/std_pair.i b/Lib/octave/std_pair.i index ab028d144..a06498bf2 100644 --- a/Lib/octave/std_pair.i +++ b/Lib/octave/std_pair.i @@ -47,7 +47,8 @@ return get_pair(c(0),c(1),val); } else { value_type *p; - int res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info(),0); + swig_type_info *descriptor = swig::type_info(); + int res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res) && val) *val = *p; return res; @@ -100,7 +101,8 @@ return get_pair(c(0),c(1),val); } else { value_type *p; - int res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info(),0); + swig_type_info *descriptor = swig::type_info(); + int res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res) && val) *val = p; return res; diff --git a/Lib/python/pycontainer.swg b/Lib/python/pycontainer.swg index 46d04388b..4621c1b34 100644 --- a/Lib/python/pycontainer.swg +++ b/Lib/python/pycontainer.swg @@ -968,8 +968,8 @@ namespace swig { static int asptr(PyObject *obj, sequence **seq) { if (obj == Py_None || SWIG_Python_GetSwigThis(obj)) { sequence *p; - if (::SWIG_ConvertPtr(obj,(void**)&p, - swig::type_info(),0) == SWIG_OK) { + swig_type_info *descriptor = swig::type_info(); + if (descriptor && SWIG_IsOK(::SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0))) { if (seq) *seq = p; return SWIG_OLDOBJ; } diff --git a/Lib/python/pystdcommon.swg b/Lib/python/pystdcommon.swg index 2af22e2a4..8372426a0 100644 --- a/Lib/python/pystdcommon.swg +++ b/Lib/python/pystdcommon.swg @@ -46,7 +46,8 @@ namespace swig { struct traits_asptr { static int asptr(PyObject *obj, Type **val) { Type *p; - int res = SWIG_ConvertPtr(obj, (void**)&p, type_info(), 0); + swig_type_info *descriptor = type_info(); + int res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res)) { if (val) *val = p; } diff --git a/Lib/python/std_map.i b/Lib/python/std_map.i index 65dd91d9c..f61f79c44 100644 --- a/Lib/python/std_map.i +++ b/Lib/python/std_map.i @@ -102,7 +102,8 @@ res = traits_asptr_stdseq >::asptr(items, val); } else { map_type *p; - res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info(),0); + swig_type_info *descriptor = swig::type_info(); + res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res) && val) *val = p; } SWIG_PYTHON_THREAD_END_BLOCK; diff --git a/Lib/python/std_multimap.i b/Lib/python/std_multimap.i index 2c539cf29..3209fb0f8 100644 --- a/Lib/python/std_multimap.i +++ b/Lib/python/std_multimap.i @@ -26,7 +26,8 @@ return traits_asptr_stdseq, std::pair >::asptr(items, val); } else { multimap_type *p; - res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info(),0); + swig_type_info *descriptor = swig::type_info(); + res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res) && val) *val = p; } return res; diff --git a/Lib/python/std_pair.i b/Lib/python/std_pair.i index 5694e7e09..da31918c8 100644 --- a/Lib/python/std_pair.i +++ b/Lib/python/std_pair.i @@ -48,7 +48,8 @@ } } else { value_type *p; - res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info(),0); + swig_type_info *descriptor = swig::type_info(); + res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res) && val) *val = *p; } return res; @@ -98,7 +99,8 @@ } } else { value_type *p; - res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info(),0); + swig_type_info *descriptor = swig::type_info(); + res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res) && val) *val = p; } return res; diff --git a/Lib/python/std_unordered_map.i b/Lib/python/std_unordered_map.i index f956f4fb3..97b48af70 100644 --- a/Lib/python/std_unordered_map.i +++ b/Lib/python/std_unordered_map.i @@ -29,7 +29,8 @@ res = traits_asptr_stdseq, std::pair >::asptr(items, val); } else { unordered_map_type *p; - res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info(),0); + swig_type_info *descriptor = swig::type_info(); + res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res) && val) *val = p; } return res; diff --git a/Lib/python/std_unordered_multimap.i b/Lib/python/std_unordered_multimap.i index b3b723637..aadd15515 100644 --- a/Lib/python/std_unordered_multimap.i +++ b/Lib/python/std_unordered_multimap.i @@ -26,7 +26,8 @@ return traits_asptr_stdseq, std::pair >::asptr(items, val); } else { unordered_multimap_type *p; - res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info(),0); + swig_type_info *descriptor = swig::type_info(); + res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res) && val) *val = p; } return res; diff --git a/Lib/r/rstdcommon.swg b/Lib/r/rstdcommon.swg index b11cf677b..e6c873a07 100644 --- a/Lib/r/rstdcommon.swg +++ b/Lib/r/rstdcommon.swg @@ -40,7 +40,8 @@ namespace swig { struct traits_asptr { static int asptr(SWIG_Object obj, Type **val) { Type *p; - int res = SWIG_ConvertPtr(obj, (void**)&p, type_info(), 0); + swig_type_info *descriptor = type_info(); + int res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res)) { if (val) *val = p; } diff --git a/Lib/ruby/rubycontainer.swg b/Lib/ruby/rubycontainer.swg index 2908ef7b7..a6d8a59ef 100644 --- a/Lib/ruby/rubycontainer.swg +++ b/Lib/ruby/rubycontainer.swg @@ -464,8 +464,7 @@ namespace swig %typemap(in,noblock=1,fragment="RubySequence_Cont") const_iterator(swig::ConstIterator *iter = 0, int res), const_reverse_iterator(swig::ConstIterator *iter = 0, int res) { - res = SWIG_ConvertPtr($input, %as_voidptrptr(&iter), - swig::ConstIterator::descriptor(), 0); + res = SWIG_ConvertPtr($input, %as_voidptrptr(&iter), swig::ConstIterator::descriptor(), 0); if (!SWIG_IsOK(res) || !iter) { %argument_fail(SWIG_TypeError, "$type", $symname, $argnum); } else { @@ -497,16 +496,14 @@ namespace swig %typecheck(%checkcode(ITERATOR),noblock=1,fragment="RubySequence_Cont") const_iterator, const_reverse_iterator { swig::ConstIterator *iter = 0; - int res = SWIG_ConvertPtr($input, %as_voidptrptr(&iter), - swig::ConstIterator::descriptor(), 0); + int res = SWIG_ConvertPtr($input, %as_voidptrptr(&iter), swig::ConstIterator::descriptor(), 0); $1 = (SWIG_IsOK(res) && iter && (dynamic_cast *>(iter) != 0)); } %typecheck(%checkcode(ITERATOR),noblock=1,fragment="RubySequence_Cont") iterator, reverse_iterator { swig::ConstIterator *iter = 0; - int res = SWIG_ConvertPtr($input, %as_voidptrptr(&iter), - swig::Iterator::descriptor(), 0); + int res = SWIG_ConvertPtr($input, %as_voidptrptr(&iter), swig::Iterator::descriptor(), 0); $1 = (SWIG_IsOK(res) && iter && (dynamic_cast *>(iter) != 0)); } @@ -1037,8 +1034,8 @@ namespace swig { } } else { sequence *p; - if (SWIG_ConvertPtr(obj,(void**)&p, - swig::type_info(),0) == SWIG_OK) { + swig_type_info *descriptor = swig::type_info(); + if (descriptor && SWIG_IsOK(SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0))) { if (seq) *seq = p; return SWIG_OLDOBJ; } @@ -1077,8 +1074,8 @@ namespace swig { } } else { sequence *p; - if (SWIG_ConvertPtr(obj,(void**)&p, - swig::type_info(),0) == SWIG_OK) { + swig_type_info *descriptor = swig::type_info(); + if (descriptor && SWIG_IsOK(SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0))) { if (seq) *seq = p; return SWIG_OLDOBJ; } diff --git a/Lib/ruby/rubystdcommon.swg b/Lib/ruby/rubystdcommon.swg index b4ae3a3cc..f72745b56 100644 --- a/Lib/ruby/rubystdcommon.swg +++ b/Lib/ruby/rubystdcommon.swg @@ -53,7 +53,8 @@ namespace swig { struct traits_asptr { static int asptr(VALUE obj, Type **val) { Type *p; - int res = SWIG_ConvertPtr(obj, (void**)&p, type_info(), 0); + swig_type_info *descriptor = type_info(); + int res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res)) { if (val) *val = p; } diff --git a/Lib/ruby/std_map.i b/Lib/ruby/std_map.i index f706ca873..7077fa104 100644 --- a/Lib/ruby/std_map.i +++ b/Lib/ruby/std_map.i @@ -100,7 +100,8 @@ res = traits_asptr_stdseq, std::pair >::asptr(items, val); } else { map_type *p; - res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info(),0); + swig_type_info *descriptor = swig::type_info(); + res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res) && val) *val = p; } return res; diff --git a/Lib/ruby/std_pair.i b/Lib/ruby/std_pair.i index 5b4c8baf2..5bea67c7c 100644 --- a/Lib/ruby/std_pair.i +++ b/Lib/ruby/std_pair.i @@ -44,8 +44,8 @@ } } else { value_type *p; - res = SWIG_ConvertPtr(obj,(void**)&p, - swig::type_info(),0); + swig_type_info *descriptor = swig::type_info(); + res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res) && val) *val = *p; } return res; @@ -90,8 +90,8 @@ } } else { value_type *p; - res = SWIG_ConvertPtr(obj,(void**)&p, - swig::type_info(),0); + swig_type_info *descriptor = swig::type_info(); + res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res) && val) *val = p; } return res; diff --git a/Lib/scilab/scistdcommon.swg b/Lib/scilab/scistdcommon.swg index 7fdc72212..63f3ca164 100644 --- a/Lib/scilab/scistdcommon.swg +++ b/Lib/scilab/scistdcommon.swg @@ -42,7 +42,8 @@ namespace swig { struct traits_asptr { static int asptr(const SwigSciObject& obj, Type **val) { Type *p; - int res = SWIG_ConvertPtr(obj, (void**)&p, type_info(), 0); + swig_type_info *descriptor = type_info(); + int res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res)) { if (val) *val = p; } diff --git a/Lib/typemaps/implicit.swg b/Lib/typemaps/implicit.swg index 702fb52b8..2fc3108e7 100644 --- a/Lib/typemaps/implicit.swg +++ b/Lib/typemaps/implicit.swg @@ -73,8 +73,8 @@ namespace swig { typedef Type value_type; static int asptr(SWIG_Object obj, value_type **val) { Type *vptr; - static swig_type_info* desc = SWIG_TypeQuery("Type *"); - int res = SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0); + static swig_type_info* descriptor = SWIG_TypeQuery("Type *"); + int res = descriptor ? SWIG_ConvertPtr(obj, (void **)&vptr, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res)) { if (val) *val = vptr; return res; @@ -109,8 +109,8 @@ namespace swig { typedef Type value_type; static int asptr(SWIG_Object obj, value_type **val) { Type *vptr; - static swig_type_info* desc = SWIG_TypeQuery("Type *"); - int res = SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0); + static swig_type_info* descriptor = SWIG_TypeQuery("Type *"); + int res = descriptor ? SWIG_ConvertPtr(obj, (void **)&vptr, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res)) { if (val) *val = vptr; return res; @@ -147,8 +147,8 @@ namespace swig { typedef Type value_type; static int asptr(SWIG_Object obj, value_type **val) { Type *vptr; - static swig_type_info* desc = SWIG_TypeQuery("Type *"); - int res = SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0); + static swig_type_info* descriptor = SWIG_TypeQuery("Type *"); + int res = descriptor ? SWIG_ConvertPtr(obj, (void **)&vptr, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res)) { if (val) *val = vptr; return SWIG_OLDOBJ; @@ -188,8 +188,8 @@ namespace swig { typedef Type value_type; static int asptr(SWIG_Object obj, value_type **val) { Type *vptr; - static swig_type_info* desc = SWIG_TypeQuery("Type *"); - int res = SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0); + static swig_type_info* descriptor = SWIG_TypeQuery("Type *"); + int res = descriptor ? SWIG_ConvertPtr(obj, (void **)&vptr, descriptor, 0) : SWIG_ERROR; if (SWIG_IsOK(res)) { if (val) *val = vptr; return res;