summaryrefslogtreecommitdiff
path: root/testes/cstack.lua
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-12-27 14:32:29 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-12-27 14:32:29 -0200
commitba7da13ec5938f978c37d63aa40a3e340b301f79 (patch)
treec1f22403954f6e0c6d17c8495c11509103313c9a /testes/cstack.lua
parentda37ac9c7894186a0e2e0e6f1f5f00b825fd1555 (diff)
downloadlua-github-ba7da13ec5938f978c37d63aa40a3e340b301f79.tar.gz
Changes in the control of C-stack overflow
* unification of the 'nny' and 'nCcalls' counters; * external C functions ('lua_CFunction') count more "slots" in the C stack (to allow for their possible use of buffers) * added a new test script specific for C-stack overflows. (Most of those tests were already present, but concentrating them in a single script easies the task of checking whether 'LUAI_MAXCCALLS' is adequate in a system.)
Diffstat (limited to 'testes/cstack.lua')
-rw-r--r--testes/cstack.lua62
1 files changed, 62 insertions, 0 deletions
diff --git a/testes/cstack.lua b/testes/cstack.lua
new file mode 100644
index 00000000..9e5bbaef
--- /dev/null
+++ b/testes/cstack.lua
@@ -0,0 +1,62 @@
+-- $Id: testes/cstack.lua $
+-- See Copyright Notice in file all.lua
+
+print"testing C-stack overflow detection"
+
+-- Segmentation faults in these tests probably result from a C-stack
+-- overflow. To avoid these errors, recompile Lua with a smaller
+-- value for the constant 'LUAI_MAXCCALLS' or else ensure a larger
+-- stack for the program.
+
+local function checkerror (msg, f, ...)
+ local s, err = pcall(f, ...)
+ assert(not s and string.find(err, msg))
+end
+
+
+do -- simple recursion
+ local count = 0
+ local function foo ()
+ count = count + 1
+ foo()
+ end
+ checkerror("stack overflow", foo)
+ print(" maximum recursion: " .. count)
+end
+
+
+-- bug since 2.5 (C-stack overflow in recursion inside pattern matching)
+do
+ local function f (size)
+ local s = string.rep("a", size)
+ local p = string.rep(".?", size)
+ return string.match(s, p)
+ end
+ local m = f(80)
+ assert(#m == 80)
+ checkerror("too complex", f, 200000)
+end
+
+
+-- testing stack-overflow in recursive 'gsub'
+do
+ local count = 0
+ local function foo ()
+ count = count + 1
+ string.gsub("a", ".", foo)
+ end
+ checkerror("stack overflow", foo)
+ print(" maximum 'gsub' nest (calls): " .. count)
+
+ -- can be done with metamethods, too
+ count = 0
+ local t = setmetatable({}, {__index = foo})
+ foo = function ()
+ count = count + 1
+ string.gsub("a", ".", t)
+ end
+ checkerror("stack overflow", foo)
+ print(" maximum 'gsub' nest (metamethods): " .. count)
+end
+
+print'OK'