summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam S Fulton <wsf@fultondesigns.co.uk>2023-01-03 23:53:34 +0000
committerWilliam S Fulton <wsf@fultondesigns.co.uk>2023-01-03 23:53:34 +0000
commitb18b75369cd4b2795abad1283629b13a62630b58 (patch)
treed34da10c118526e099bd79df3a0da7bfcc6917df
parent2fc0edc4fd9da1c25254127128aaa601cad30916 (diff)
downloadswig-b18b75369cd4b2795abad1283629b13a62630b58.tar.gz
Fix seg fault using %template
Fix seg fault when instantiating templates with parameters that are function parameters containing templates, such as: %template(MyC) C<int(std::vector<int>)>; Closes #983
-rw-r--r--CHANGES.current6
-rw-r--r--Examples/test-suite/java/template_function_parm_runme.java25
-rw-r--r--Examples/test-suite/template_function_parm.i28
-rw-r--r--Source/Swig/symbol.c2
4 files changed, 60 insertions, 1 deletions
diff --git a/CHANGES.current b/CHANGES.current
index 6213afcf1..81a4b7c4d 100644
--- a/CHANGES.current
+++ b/CHANGES.current
@@ -8,6 +8,12 @@ Version 4.2.0 (in progress)
===========================
2023-01-03: wsfulton
+ #983 Fix seg fault when instantiating templates with parameters that are function
+ parameters containing templates, such as:
+
+ %template(MyC) C<int(std::vector<int>)>;
+
+2023-01-03: wsfulton
Complete support for C++11 variadic function templates. Support was previously limited
to just one template parameter. Now zero or more template parameters are supported
in the %template instantiation.
diff --git a/Examples/test-suite/java/template_function_parm_runme.java b/Examples/test-suite/java/template_function_parm_runme.java
new file mode 100644
index 000000000..3c8f3d1a9
--- /dev/null
+++ b/Examples/test-suite/java/template_function_parm_runme.java
@@ -0,0 +1,25 @@
+import template_function_parm.*;
+
+public class template_function_parm_runme {
+
+ static {
+ try {
+ System.loadLibrary("template_function_parm");
+ } catch (UnsatisfiedLinkError e) {
+ System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
+ System.exit(1);
+ }
+ }
+
+ public static void main(String argv[]) {
+ VectorInt vi = new VectorInt();
+ vi.add(10);
+ vi.add(20);
+ vi.add(30);
+
+ MyC myc = new MyC();
+ int sum = myc.take_function(template_function_parmConstants.accumulate_integers, vi);
+ if (sum != 60)
+ throw new RuntimeException("Expected sum of 60, got " + sum);
+ }
+}
diff --git a/Examples/test-suite/template_function_parm.i b/Examples/test-suite/template_function_parm.i
new file mode 100644
index 000000000..3dc011b67
--- /dev/null
+++ b/Examples/test-suite/template_function_parm.i
@@ -0,0 +1,28 @@
+%module template_function_parm
+
+%include <std_vector.i>
+
+%{
+#include <numeric>
+static int accumulate_integers(std::vector<int> vi) {
+ int sum = std::accumulate(vi.begin(), vi.end(), 0);
+ return sum;
+}
+%}
+
+%inline %{
+template<typename A>
+struct C {
+ int take_function(int fp(std::vector<int>), std::vector<int> v) {
+ return fp(v);
+ }
+};
+%}
+
+%constant int accumulate_integers(std::vector<int>);
+
+%template(VectorInt) std::vector<int>;
+
+// seg fault #983
+%template(MyC) C<int(std::vector<int>)>;
+
diff --git a/Source/Swig/symbol.c b/Source/Swig/symbol.c
index 107b1caef..7c7c4d100 100644
--- a/Source/Swig/symbol.c
+++ b/Source/Swig/symbol.c
@@ -1746,7 +1746,7 @@ SwigType *Swig_symbol_typedef_reduce(const SwigType *ty, Symtab *tab) {
n = Swig_symbol_clookup(base, tab);
if (!n) {
- if (SwigType_istemplate(ty)) {
+ if (SwigType_istemplate(base)) {
SwigType *qt = Swig_symbol_template_reduce(base, tab);
Append(prefix, qt);
Delete(qt);