From aa24e6b22b159ef1c8274ac25a46df35008bf040 Mon Sep 17 00:00:00 2001 From: Olly Betts Date: Wed, 2 Mar 2022 15:53:51 +1300 Subject: [PATCH] Handle typemap argument without a value This now gives an error, previously SWIG segfaulted. Fixes #891 --- CHANGES.current | 4 +++ .../errors/swig_typemap_missing_value.i | 6 ++++ .../errors/swig_typemap_missing_value.stderr | 1 + Source/CParse/parser.y | 28 ++++++++++++------- 4 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 Examples/test-suite/errors/swig_typemap_missing_value.i create mode 100644 Examples/test-suite/errors/swig_typemap_missing_value.stderr diff --git a/CHANGES.current b/CHANGES.current index 1dc7709da..ae12d1a2c 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,10 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.1.0 (in progress) =========================== +2022-03-02: olly + #891 Give error for typemap argument without a value. Previously + SWIG segfaulted. + 2022-02-27: wsfulton [Python] #735 #1561 Function annotations containing C/C++ types are no longer generated when using the -py3 option. Function annotations support has been diff --git a/Examples/test-suite/errors/swig_typemap_missing_value.i b/Examples/test-suite/errors/swig_typemap_missing_value.i new file mode 100644 index 000000000..ec9ed1ede --- /dev/null +++ b/Examples/test-suite/errors/swig_typemap_missing_value.i @@ -0,0 +1,6 @@ +%module xxx + +%typemap(in, numinputs=1, foo) int "" + +/* SWIG segfaulted trying to use the above typemap in SWIG < 4.1.0. */ +void func(int arg); diff --git a/Examples/test-suite/errors/swig_typemap_missing_value.stderr b/Examples/test-suite/errors/swig_typemap_missing_value.stderr new file mode 100644 index 000000000..b30a412c3 --- /dev/null +++ b/Examples/test-suite/errors/swig_typemap_missing_value.stderr @@ -0,0 +1 @@ +swig_typemap_missing_value.i:3: Error: %typemap argument 'foo' is missing value diff --git a/Source/CParse/parser.y b/Source/CParse/parser.y index 8f5428915..23ac8e5df 100644 --- a/Source/CParse/parser.y +++ b/Source/CParse/parser.y @@ -2723,26 +2723,34 @@ typemap_directive : TYPEMAP LPAREN typemap_type RPAREN tm_list stringbrace { /* typemap method type (lang,method) or (method) */ typemap_type : kwargs { - Hash *p; - String *name; - p = nextSibling($1); + String *name = Getattr($1, "name"); + Hash *p = nextSibling($1); if (p && (!Getattr(p,"value"))) { /* this is the deprecated two argument typemap form */ Swig_warning(WARN_DEPRECATED_TYPEMAP_LANG,cparse_file, cparse_line, "Specifying the language name in %%typemap is deprecated - use #ifdef SWIG instead.\n"); /* two argument typemap form */ - name = Getattr($1,"name"); + Printf(stdout, "name=%s typemap_lang=%s\n", name, typemap_lang); if (!name || (Strcmp(name,typemap_lang))) { - $$.method = 0; - $$.kwargs = 0; + name = 0; + p = 0; } else { - $$.method = Getattr(p,"name"); - $$.kwargs = nextSibling(p); + name = Getattr(p,"name"); + p = nextSibling(p); } } else { /* one-argument typemap-form */ - $$.method = Getattr($1,"name"); - $$.kwargs = p; + } + $$.method = name; + $$.kwargs = p; + while (p) { + if (!Getattr(p, "value")) { + Swig_error(cparse_file, cparse_line, + "%%typemap argument '%s' is missing value\n", Getattr(p, "name")); + /* Set to empty value to avoid segfaults later. */ + Setattr(p, "value", NewStringEmpty()); + } + p = nextSibling(p); } } ;