cstack.lua 1.4 KB

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