aboutsummaryrefslogtreecommitdiff
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-ba7da13ec5938f978c37d63aa40a3e340b301f79.tar.gz
lua-ba7da13ec5938f978c37d63aa40a3e340b301f79.tar.bz2
lua-ba7da13ec5938f978c37d63aa40a3e340b301f79.zip
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 '')
-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 @@
1-- $Id: testes/cstack.lua $
2-- See Copyright Notice in file all.lua
3
4print"testing C-stack overflow detection"
5
6-- Segmentation faults in these tests probably result from a C-stack
7-- overflow. To avoid these errors, recompile Lua with a smaller
8-- value for the constant 'LUAI_MAXCCALLS' or else ensure a larger
9-- stack for the program.
10
11local function checkerror (msg, f, ...)
12 local s, err = pcall(f, ...)
13 assert(not s and string.find(err, msg))
14end
15
16
17do -- simple recursion
18 local count = 0
19 local function foo ()
20 count = count + 1
21 foo()
22 end
23 checkerror("stack overflow", foo)
24 print(" maximum recursion: " .. count)
25end
26
27
28-- bug since 2.5 (C-stack overflow in recursion inside pattern matching)
29do
30 local function f (size)
31 local s = string.rep("a", size)
32 local p = string.rep(".?", size)
33 return string.match(s, p)
34 end
35 local m = f(80)
36 assert(#m == 80)
37 checkerror("too complex", f, 200000)
38end
39
40
41-- testing stack-overflow in recursive 'gsub'
42do
43 local count = 0
44 local function foo ()
45 count = count + 1
46 string.gsub("a", ".", foo)
47 end
48 checkerror("stack overflow", foo)
49 print(" maximum 'gsub' nest (calls): " .. count)
50
51 -- can be done with metamethods, too
52 count = 0
53 local t = setmetatable({}, {__index = foo})
54 foo = function ()
55 count = count + 1
56 string.gsub("a", ".", t)
57 end
58 checkerror("stack overflow", foo)
59 print(" maximum 'gsub' nest (metamethods): " .. count)
60end
61
62print'OK'