aboutsummaryrefslogtreecommitdiff
path: root/testes/cstack.lua
blob: 9e5bbaefd7e8b68e338dc42edb731626732fe804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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'