as() now always throws and never creates an invalid object with memset()

This commit is contained in:
Mike Romberg 2017-07-03 15:35:24 -06:00
commit dd26e8a014
2 changed files with 14 additions and 19 deletions

View file

@ -183,8 +183,8 @@ namespace swig
{ {
VALUE item = rb_ary_entry(_seq, _index ); VALUE item = rb_ary_entry(_seq, _index );
try { try {
return swig::as<T>(item, true); return swig::as<T>(item);
} catch (std::exception& e) { } catch (const std::invalid_argument& e) {
char msg[1024]; char msg[1024];
sprintf(msg, "in sequence element %d ", _index); sprintf(msg, "in sequence element %d ", _index);
VALUE lastErr = rb_gv_get("$!"); VALUE lastErr = rb_gv_get("$!");
@ -926,7 +926,7 @@ namespace swig
VALUE elem = argv[0]; VALUE elem = argv[0];
int idx = 0; int idx = 0;
try { try {
Sequence::value_type val = swig::as<Sequence::value_type>( elem, true ); Sequence::value_type val = swig::as<Sequence::value_type>( elem );
if ( i >= len ) { if ( i >= len ) {
$self->resize(i-1, val); $self->resize(i-1, val);
return $self; return $self;
@ -943,7 +943,7 @@ namespace swig
} }
} }
catch( std::invalid_argument ) catch( const std::invalid_argument & )
{ {
rb_raise( rb_eArgError, "%s", rb_raise( rb_eArgError, "%s",
Ruby_Format_TypeError( "", Ruby_Format_TypeError( "",
@ -967,10 +967,10 @@ namespace swig
Sequence::iterator start = $self->begin(); Sequence::iterator start = $self->begin();
VALUE elem = argv[idx]; VALUE elem = argv[idx];
try { try {
Sequence::value_type val = swig::as<Sequence::value_type>( elem, true ); Sequence::value_type val = swig::as<Sequence::value_type>( elem );
$self->insert( start, val ); $self->insert( start, val );
} }
catch( std::invalid_argument ) catch( const std::invalid_argument & )
{ {
rb_raise( rb_eArgError, "%s", rb_raise( rb_eArgError, "%s",
Ruby_Format_TypeError( "", Ruby_Format_TypeError( "",

View file

@ -115,15 +115,15 @@ namespace swig {
template <class Type> template <class Type>
struct traits_as<Type, value_category> { struct traits_as<Type, value_category> {
static Type as(VALUE obj, bool throw_error) { static Type as(VALUE obj) {
Type v; Type v;
int res = asval(obj, &v); int res = asval(obj, &v);
if (!SWIG_IsOK(res)) { if (!SWIG_IsOK(res)) {
if (throw_error) throw std::invalid_argument("bad type");
VALUE lastErr = rb_gv_get("$!"); VALUE lastErr = rb_gv_get("$!");
if (lastErr == Qnil) { if (lastErr == Qnil) {
%type_error(swig::type_name<Type>()); %type_error(swig::type_name<Type>());
} }
throw std::invalid_argument("bad type");
} }
return v; return v;
} }
@ -131,7 +131,7 @@ namespace swig {
template <class Type> template <class Type>
struct traits_as<Type, pointer_category> { struct traits_as<Type, pointer_category> {
static Type as(VALUE obj, bool throw_error) { static Type as(VALUE obj) {
Type *v = 0; Type *v = 0;
int res = traits_asptr<Type>::asptr(obj, &v); int res = traits_asptr<Type>::asptr(obj, &v);
if (SWIG_IsOK(res) && v) { if (SWIG_IsOK(res) && v) {
@ -143,40 +143,35 @@ namespace swig {
return *v; return *v;
} }
} else { } else {
// Uninitialized return value, no Type() constructor required.
if (throw_error) throw std::invalid_argument("bad type");
VALUE lastErr = rb_gv_get("$!"); VALUE lastErr = rb_gv_get("$!");
if (lastErr == Qnil) { if (lastErr == Qnil) {
%type_error(swig::type_name<Type>()); %type_error(swig::type_name<Type>());
} }
static Type *v_def = (Type*) malloc(sizeof(Type)); throw std::invalid_argument("bad type");
memset(v_def,0,sizeof(Type));
return *v_def;
} }
} }
}; };
template <class Type> template <class Type>
struct traits_as<Type*, pointer_category> { struct traits_as<Type*, pointer_category> {
static Type* as(VALUE obj, bool throw_error) { static Type* as(VALUE obj) {
Type *v = 0; Type *v = 0;
int res = traits_asptr<Type>::asptr(obj, &v); int res = traits_asptr<Type>::asptr(obj, &v);
if (SWIG_IsOK(res)) { if (SWIG_IsOK(res)) {
return v; return v;
} else { } else {
if (throw_error) throw std::invalid_argument("bad type");
VALUE lastErr = rb_gv_get("$!"); VALUE lastErr = rb_gv_get("$!");
if (lastErr == Qnil) { if (lastErr == Qnil) {
%type_error(swig::type_name<Type>()); %type_error(swig::type_name<Type>());
} }
return 0; throw std::invalid_argument("bad type");
} }
} }
}; };
template <class Type> template <class Type>
inline Type as(VALUE obj, bool te = false) { inline Type as(VALUE obj) {
return traits_as< Type, typename traits< Type >::category >::as(obj, te); return traits_as< Type, typename traits< Type >::category >::as(obj);
} }
template <class Type> template <class Type>