cstack.lua 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. local count
  13. local back = string.rep("\b", 8)
  14. local function progress ()
  15. count = count + 1
  16. local n = string.format("%-8d", count)
  17. io.stderr:write(back, n)
  18. end
  19. do print("testing simple recursion:")
  20. count = 0
  21. local function foo ()
  22. progress()
  23. foo()
  24. end
  25. checkerror("stack overflow", foo)
  26. print("\tfinal count: ", count)
  27. end
  28. do print("testing stack overflow in message handling")
  29. count = 0
  30. local function loop (x, y, z)
  31. progress()
  32. return 1 + loop(x, y, z)
  33. end
  34. local res, msg = xpcall(loop, loop)
  35. assert(msg == "error in error handling")
  36. print("\tfinal count: ", count)
  37. end
  38. -- bug since 2.5 (C-stack overflow in recursion inside pattern matching)
  39. do print("testing recursion inside pattern matching")
  40. local function f (size)
  41. local s = string.rep("a", size)
  42. local p = string.rep(".?", size)
  43. return string.match(s, p)
  44. end
  45. local m = f(80)
  46. assert(#m == 80)
  47. checkerror("too complex", f, 200000)
  48. end
  49. do print("testing stack-overflow in recursive 'gsub'")
  50. count = 0
  51. local function foo ()
  52. progress()
  53. string.gsub("a", ".", foo)
  54. end
  55. checkerror("stack overflow", foo)
  56. print("\tfinal count: ", count)
  57. print("testing stack-overflow in recursive 'gsub' with metatables")
  58. count = 0
  59. local t = setmetatable({}, {__index = foo})
  60. foo = function ()
  61. count = count + 1
  62. progress(count)
  63. string.gsub("a", ".", t)
  64. end
  65. checkerror("stack overflow", foo)
  66. print("\tfinal count: ", count)
  67. end
  68. print'OK'