From 74345c92e47938a75817eff5ba48dc9d3ce2a281 Mon Sep 17 00:00:00 2001 From: Mike Romberg Date: Sat, 1 Jul 2017 17:38:48 -0600 Subject: [PATCH 1/4] supress clang warning about vtable ptr overwrite. Use calloc to zero memory just once. Document memory leak. --- Lib/python/pystdcommon.swg | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/python/pystdcommon.swg b/Lib/python/pystdcommon.swg index 93ee34bc7..45f27375e 100644 --- a/Lib/python/pystdcommon.swg +++ b/Lib/python/pystdcommon.swg @@ -135,12 +135,11 @@ namespace swig { } } else { // Uninitialized return value, no Type() constructor required. - static Type *v_def = (Type*) malloc(sizeof(Type)); + static Type *v_def = (Type*) calloc(1, sizeof(Type)); // memory leak? if (!PyErr_Occurred()) { %type_error(swig::type_name()); } if (throw_error) throw std::invalid_argument("bad type"); - memset(v_def,0,sizeof(Type)); return *v_def; } } From 6c0885ff677139a0c3c9719dd9fae7eac4f0167f Mon Sep 17 00:00:00 2001 From: Mike Romberg Date: Sat, 1 Jul 2017 17:58:59 -0600 Subject: [PATCH 2/4] Use the Type() ctor and avoid: - clang warning about memset overwrite of vtable - memory leak - Undefined behavior in Type copy/move operations and dtor. --- Lib/python/pystdcommon.swg | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/python/pystdcommon.swg b/Lib/python/pystdcommon.swg index 45f27375e..70746a58e 100644 --- a/Lib/python/pystdcommon.swg +++ b/Lib/python/pystdcommon.swg @@ -134,13 +134,12 @@ namespace swig { return *v; } } else { - // Uninitialized return value, no Type() constructor required. - static Type *v_def = (Type*) calloc(1, sizeof(Type)); // memory leak? + static Type v_def; if (!PyErr_Occurred()) { %type_error(swig::type_name()); } if (throw_error) throw std::invalid_argument("bad type"); - return *v_def; + return v_def; } } }; From bfaafde020dc7e38d3a147b0fdbd79adaf68df96 Mon Sep 17 00:00:00 2001 From: Mike Romberg Date: Mon, 3 Jul 2017 12:25:04 -0600 Subject: [PATCH 3/4] traits_as::as() ignores throw_error and always throws. --- Lib/python/pystdcommon.swg | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Lib/python/pystdcommon.swg b/Lib/python/pystdcommon.swg index 70746a58e..c8ce2c8e6 100644 --- a/Lib/python/pystdcommon.swg +++ b/Lib/python/pystdcommon.swg @@ -122,7 +122,7 @@ namespace swig { template struct traits_as { - static Type as(PyObject *obj, bool throw_error) { + static Type as(PyObject *obj, bool /* throw_error */) { Type *v = 0; int res = (obj ? traits_asptr::asptr(obj, &v) : SWIG_ERROR); if (SWIG_IsOK(res) && v) { @@ -134,12 +134,10 @@ namespace swig { return *v; } } else { - static Type v_def; if (!PyErr_Occurred()) { %type_error(swig::type_name()); } - if (throw_error) throw std::invalid_argument("bad type"); - return v_def; + throw std::invalid_argument("bad type"); } } }; From b229d92f9bfbefb6d11c3b92d590ead1ce93dc4a Mon Sep 17 00:00:00 2001 From: Mike Romberg Date: Mon, 3 Jul 2017 12:49:30 -0600 Subject: [PATCH 4/4] remove bool from as() (it now always throws). catch std::invalid_argument (what is expected) instread of std::exception. --- Lib/python/pycontainer.swg | 4 ++-- Lib/python/pystdcommon.swg | 15 +++++++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Lib/python/pycontainer.swg b/Lib/python/pycontainer.swg index 1687aca42..36557d75e 100644 --- a/Lib/python/pycontainer.swg +++ b/Lib/python/pycontainer.swg @@ -434,8 +434,8 @@ namespace swig { swig::SwigVar_PyObject item = PySequence_GetItem(_seq, _index); try { - return swig::as(item, true); - } catch (std::exception& e) { + return swig::as(item); + } catch (const std::invalid_argument& e) { char msg[1024]; sprintf(msg, "in sequence element %d ", (int)_index); if (!PyErr_Occurred()) { diff --git a/Lib/python/pystdcommon.swg b/Lib/python/pystdcommon.swg index c8ce2c8e6..0242e4d35 100644 --- a/Lib/python/pystdcommon.swg +++ b/Lib/python/pystdcommon.swg @@ -107,14 +107,14 @@ namespace swig { template struct traits_as { - static Type as(PyObject *obj, bool throw_error) { + static Type as(PyObject *obj) { Type v; int res = asval(obj, &v); if (!obj || !SWIG_IsOK(res)) { if (!PyErr_Occurred()) { ::%type_error(swig::type_name()); } - if (throw_error) throw std::invalid_argument("bad type"); + throw std::invalid_argument("bad type"); } return v; } @@ -122,7 +122,7 @@ namespace swig { template struct traits_as { - static Type as(PyObject *obj, bool /* throw_error */) { + static Type as(PyObject *obj) { Type *v = 0; int res = (obj ? traits_asptr::asptr(obj, &v) : SWIG_ERROR); if (SWIG_IsOK(res) && v) { @@ -144,7 +144,7 @@ namespace swig { template struct traits_as { - static Type* as(PyObject *obj, bool throw_error) { + static Type* as(PyObject *obj) { Type *v = 0; int res = (obj ? traits_asptr::asptr(obj, &v) : SWIG_ERROR); if (SWIG_IsOK(res)) { @@ -153,15 +153,14 @@ namespace swig { if (!PyErr_Occurred()) { %type_error(swig::type_name()); } - if (throw_error) throw std::invalid_argument("bad type"); - return 0; + throw std::invalid_argument("bad type"); } } }; template - inline Type as(PyObject *obj, bool te = false) { - return traits_as::category>::as(obj, te); + inline Type as(PyObject *obj) { + return traits_as::category>::as(obj); } template