octave.cxx: fix exception raising for newer Octave versions

- Since (at least) Octave 5.1.0, the Octave error() function now raises a C++
  exception, which if uncaught immediately exits a SWIG wrapper function,
  bypassing any cleanup code that may appear after a "fail:" label.
- This patch adds a "try { ... } catch(...) { }" block around the contents of
  SWIG wrapper functions to first execute the cleanup code before rethrowing any
  exception raised.
- It is backward compatible with earlier versions of Octave where error() does
  not raise an exception, which will still branch to the "fail:" block to
  execute cleanup code if an error is encountered.
This commit is contained in:
Karl Wette 2020-05-02 03:35:05 +10:00
commit d9c94848ec
2 changed files with 32 additions and 1 deletions

View file

@ -16,6 +16,22 @@ Version 4.0.2 (in progress)
2020-05-24: vapier
[JS] #1796 Fix pkg-config invocation in configure.
2020-04-30: kwwette
[Octave] Fix exception raising for newer Octave versions
Since (at least) Octave 5.1.0, the Octave error() function now raises a C++ exception,
which if uncaught immediately exits a SWIG wrapper function, bypassing any cleanup code
that may appear after a "fail:" label. This patch adds a "try { ... } catch(...) { }"
block around the contents of SWIG wrapper functions to first execute the cleanup code
before rethrowing any exception raised. It is backward compatible with earlier versions
of Octave where error() does not raise an exception, which will still branch to the
"fail:" block to execute cleanup code if an error is encountered.
Note that the new "try { ... } catch(...) { }" block will localise any local variables
used in typemaps that were NOT declared through SWIG's %typemap(...) syntax, so it's
possible this could break existing SWIG wrappers which were implicitly sharing local
variables between typemaps. This can be fixed, however, by declaring local variables
which need to be shared between typemaps through SWIG's %typemap(...) syntax.
2020-02-18: ryannevell
[Lua] #1728 Add support for LUA lightuserdata to SWIG_Lua_ConvertPtr.

View file

@ -567,6 +567,10 @@ public:
Wrapper *f = NewWrapper();
Octave_begin_function(n, f->def, iname, overname, !overloaded);
// Start default try block to execute
// cleanup code if exception is thrown
Printf(f->code, "try {\n");
emit_parameter_variables(l, f);
emit_attach_parmmaps(l, f);
Setattr(n, "wrap:parms", l);
@ -754,9 +758,20 @@ public:
}
Printf(f->code, "return _out;\n");
Printf(f->code, "fail:\n"); // we should free locals etc if this happens
// Execute cleanup code if branched to fail: label
Printf(f->code, "fail:\n");
Printv(f->code, cleanup, NIL);
Printf(f->code, "return octave_value_list();\n");
// Execute cleanup code if exception was thrown
Printf(f->code, "}\n");
Printf(f->code, "catch(...) {\n");
Printv(f->code, cleanup, NIL);
Printf(f->code, "throw;\n");
Printf(f->code, "}\n");
// End wrapper function
Printf(f->code, "}\n");
/* Substitute the cleanup code */