diff options
Diffstat (limited to 'testes/cstack.lua')
| -rw-r--r-- | testes/cstack.lua | 62 |
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 | |||
| 4 | print"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 | |||
| 11 | local function checkerror (msg, f, ...) | ||
| 12 | local s, err = pcall(f, ...) | ||
| 13 | assert(not s and string.find(err, msg)) | ||
| 14 | end | ||
| 15 | |||
| 16 | |||
| 17 | do -- 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) | ||
| 25 | end | ||
| 26 | |||
| 27 | |||
| 28 | -- bug since 2.5 (C-stack overflow in recursion inside pattern matching) | ||
| 29 | do | ||
| 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) | ||
| 38 | end | ||
| 39 | |||
| 40 | |||
| 41 | -- testing stack-overflow in recursive 'gsub' | ||
| 42 | do | ||
| 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) | ||
| 60 | end | ||
| 61 | |||
| 62 | print'OK' | ||
