# HG changeset patch # User Rik # Date 1324260918 28800 # Node ID 80bda7c4f01232795db4928d312600448d201d0c # Parent 3709aaf73715a3b2882df014c74e18669a173724 Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981) * test.m, demo.m: Search code block to be executed for any embedded functions. Return an error if any functions found without executing the code block to avoid segmentation fault. diff -r 3709aaf73715 -r 80bda7c4f012 scripts/testfun/demo.m --- a/scripts/testfun/demo.m Fri Dec 16 15:31:14 2011 +0000 +++ b/scripts/testfun/demo.m Sun Dec 18 18:15:18 2011 -0800 @@ -122,11 +122,19 @@ ## Process each demo without failing try block = code(idx(doidx(i)):idx(doidx(i)+1)-1); - ## Use an environment without variables - eval (cstrcat ("function __demo__()\n", block, "\nendfunction")); - ## Display the code that will be executed before executing it - printf ("%s example %d:%s\n\n", name, doidx(i), block); - __demo__; + ## FIXME: need to check for embedded test functions, which cause + ## segfaults, until issues with subfunctions in functions are resolved. + embed_func = regexp (block, '^\s*function ', 'once', 'lineanchors'); + if (isempty (embed_func)) + ## Use an environment without variables + eval (cstrcat ("function __demo__()\n", block, "\nendfunction")); + ## Display the code that will be executed before executing it + printf ("%s example %d:%s\n\n", name, doidx(i), block); + __demo__; + else + error (["Functions embedded in %!demo blocks are not allowed.\n", ... + "Use the %!function/%!endfunction syntax instead to define shared functions for testing.\n"]); + endif catch ## Let the programmer know which demo failed. printf ("%s example %d: failed\n%s\n", name, doidx(i), lasterr ()); diff -r 3709aaf73715 -r 80bda7c4f012 scripts/testfun/test.m --- a/scripts/testfun/test.m Fri Dec 16 15:31:14 2011 +0000 +++ b/scripts/testfun/test.m Sun Dec 18 18:15:18 2011 -0800 @@ -491,9 +491,17 @@ ## evaluate code for test, shared, and assert. if (! isempty(__code)) try - eval (sprintf ("function %s__test__(%s)\n%s\nendfunction", - __shared_r,__shared, __code)); - eval (sprintf ("%s__test__(%s);", __shared_r, __shared)); + ## FIXME: need to check for embedded test functions, which cause + ## segfaults, until issues with subfunctions in functions are resolved. + embed_func = regexp (__code, '^\s*function ', 'once', 'lineanchors'); + if (isempty (embed_func)) + eval (sprintf ("function %s__test__(%s)\n%s\nendfunction", + __shared_r,__shared, __code)); + eval (sprintf ("%s__test__(%s);", __shared_r, __shared)); + else + error (["Functions embedded in %!test blocks are not allowed.\n", ... + "Use the %!function/%!endfunction syntax instead to define shared functions for testing.\n"]); + endif catch if (strcmp (__type, "xtest")) __msg = sprintf ("%sknown failure\n%s", __signal_fail, lasterr ());