Merge pull request #1017 from mromberg/tctor

fix: memory leak, clang memset warning and undefined behavior
This commit is contained in:
Olly Betts 2017-07-20 17:02:45 +12:00 • committed by GitHub
commit 8dd5ed2406
2 changed files with 10 additions and 15 deletions

View file

@ -434,8 +434,8 @@ namespace swig
{ {
swig::SwigVar_PyObject item = PySequence_GetItem(_seq, _index); swig::SwigVar_PyObject item = PySequence_GetItem(_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 ", (int)_index); sprintf(msg, "in sequence element %d ", (int)_index);
if (!PyErr_Occurred()) { if (!PyErr_Occurred()) {

View file

@ -107,14 +107,14 @@ namespace swig {
template <class Type> template <class Type>
struct traits_as<Type, value_category> { struct traits_as<Type, value_category> {
static Type as(PyObject *obj, bool throw_error) { static Type as(PyObject *obj) {
Type v; Type v;
int res = asval(obj, &v); int res = asval(obj, &v);
if (!obj || !SWIG_IsOK(res)) { if (!obj || !SWIG_IsOK(res)) {
if (!PyErr_Occurred()) { if (!PyErr_Occurred()) {
::%type_error(swig::type_name<Type>()); ::%type_error(swig::type_name<Type>());
} }
if (throw_error) throw std::invalid_argument("bad type"); throw std::invalid_argument("bad type");
} }
return v; return v;
} }
@ -122,7 +122,7 @@ namespace swig {
template <class Type> template <class Type>
struct traits_as<Type, pointer_category> { struct traits_as<Type, pointer_category> {
static Type as(PyObject *obj, bool throw_error) { static Type as(PyObject *obj) {
Type *v = 0; Type *v = 0;
int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR); int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
if (SWIG_IsOK(res) && v) { if (SWIG_IsOK(res) && v) {
@ -134,21 +134,17 @@ namespace swig {
return *v; return *v;
} }
} else { } else {
// Uninitialized return value, no Type() constructor required.
static Type *v_def = (Type*) malloc(sizeof(Type));
if (!PyErr_Occurred()) { if (!PyErr_Occurred()) {
%type_error(swig::type_name<Type>()); %type_error(swig::type_name<Type>());
} }
if (throw_error) throw std::invalid_argument("bad 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(PyObject *obj, bool throw_error) { static Type* as(PyObject *obj) {
Type *v = 0; Type *v = 0;
int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR); int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
if (SWIG_IsOK(res)) { if (SWIG_IsOK(res)) {
@ -157,15 +153,14 @@ namespace swig {
if (!PyErr_Occurred()) { if (!PyErr_Occurred()) {
%type_error(swig::type_name<Type>()); %type_error(swig::type_name<Type>());
} }
if (throw_error) throw std::invalid_argument("bad type"); throw std::invalid_argument("bad type");
return 0;
} }
} }
}; };
template <class Type> template <class Type>
inline Type as(PyObject *obj, bool te = false) { inline Type as(PyObject *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>