From b18b75369cd4b2795abad1283629b13a62630b58 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Tue, 3 Jan 2023 23:53:34 +0000 Subject: [PATCH] Fix seg fault using %template Fix seg fault when instantiating templates with parameters that are function parameters containing templates, such as: %template(MyC) C)>; Closes #983 --- CHANGES.current | 6 ++++ .../java/template_function_parm_runme.java | 25 +++++++++++++++++ Examples/test-suite/template_function_parm.i | 28 +++++++++++++++++++ Source/Swig/symbol.c | 2 +- 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 Examples/test-suite/java/template_function_parm_runme.java create mode 100644 Examples/test-suite/template_function_parm.i diff --git a/CHANGES.current b/CHANGES.current index 6213afcf1..81a4b7c4d 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,12 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ 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)>; + 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 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 + +%{ +#include +static int accumulate_integers(std::vector vi) { + int sum = std::accumulate(vi.begin(), vi.end(), 0); + return sum; +} +%} + +%inline %{ +template +struct C { + int take_function(int fp(std::vector), std::vector v) { + return fp(v); + } +}; +%} + +%constant int accumulate_integers(std::vector); + +%template(VectorInt) std::vector; + +// seg fault #983 +%template(MyC) C)>; + 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);