errors.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. print("testing errors")
  2. local debug = require"debug"
  3. -- avoid problems with 'strict' module (which may generate other error messages)
  4. local mt = getmetatable(_G) or {}
  5. local oldmm = mt.__index
  6. mt.__index = nil
  7. function doit (s)
  8. local f, msg = load(s)
  9. if f == nil then return msg end
  10. local cond, msg = pcall(f)
  11. return (not cond) and msg
  12. end
  13. function checkmessage (prog, msg)
  14. local m = doit(prog)
  15. assert(string.find(m, msg, 1, true))
  16. end
  17. function checksyntax (prog, extra, token, line)
  18. local msg = doit(prog)
  19. if not string.find(token, "^<%a") and not string.find(token, "^char%(")
  20. then token = "'"..token.."'" end
  21. token = string.gsub(token, "(%p)", "%%%1")
  22. local pt = string.format([[^%%[string ".*"%%]:%d: .- near %s$]],
  23. line, token)
  24. assert(string.find(msg, pt))
  25. assert(string.find(msg, msg, 1, true))
  26. end
  27. -- test error message with no extra info
  28. assert(doit("error('hi', 0)") == 'hi')
  29. -- test error message with no info
  30. assert(doit("error()") == nil)
  31. -- test common errors/errors that crashed in the past
  32. if not _no32 then
  33. assert(doit("table.unpack({}, 1, n=2^30)"))
  34. end
  35. assert(doit("a=math.sin()"))
  36. assert(not doit("tostring(1)") and doit("tostring()"))
  37. assert(doit"tonumber()")
  38. assert(doit"repeat until 1; a")
  39. assert(doit"return;;")
  40. assert(doit"assert(false)")
  41. assert(doit"assert(nil)")
  42. assert(doit("function a (... , ...) end"))
  43. assert(doit("function a (, ...) end"))
  44. assert(doit("local t={}; t = t[#t] + 1"))
  45. checksyntax([[
  46. local a = {4
  47. ]], "'}' expected (to close '{' at line 1)", "<eof>", 3)
  48. -- tests for better error messages
  49. checkmessage("a=1; bbbb=2; a=math.sin(3)+bbbb(3)", "global 'bbbb'")
  50. checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(3)",
  51. "local 'bbbb'")
  52. checkmessage("a={}; do local a=1 end a:bbbb(3)", "method 'bbbb'")
  53. checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'")
  54. assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'"))
  55. checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number")
  56. checkmessage("a=(1)..{}", "a table value")
  57. aaa = nil
  58. checkmessage("aaa.bbb:ddd(9)", "global 'aaa'")
  59. checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'")
  60. checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'")
  61. checkmessage("local a,b,c; (function () a = b+1 end)()", "upvalue 'b'")
  62. assert(not doit"local aaa={bbb={ddd=next}}; aaa.bbb:ddd(nil)")
  63. checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'")
  64. checkmessage("b=1; local aaa='a'; x=aaa+b", "local 'aaa'")
  65. checkmessage("aaa={}; x=3/aaa", "global 'aaa'")
  66. checkmessage("aaa='2'; b=nil;x=aaa*b", "global 'b'")
  67. checkmessage("aaa={}; x=-aaa", "global 'aaa'")
  68. assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'"))
  69. assert(not string.find(doit"aaa={}; (aaa or aaa)()", "'aaa'"))
  70. checkmessage("print(print < 10)", "function")
  71. checkmessage("print(print < print)", "two function")
  72. -- passing light userdata instead of full userdata
  73. _G.D = debug
  74. checkmessage([[
  75. -- create light udata
  76. local x = D.upvalueid(function () return debug end, 1)
  77. D.setuservalue(x, {})
  78. ]], "light userdata")
  79. _G.D = nil
  80. -- global functions
  81. checkmessage("(io.write or print){}", "io.write")
  82. checkmessage("(collectgarbage or print){}", "collectgarbage")
  83. -- tests for field accesses after RK limit
  84. local t = {}
  85. for i = 1, 1000 do
  86. t[i] = "a = x" .. i
  87. end
  88. local s = table.concat(t, "; ")
  89. t = nil
  90. checkmessage(s.."; a = bbb + 1", "global 'bbb'")
  91. checkmessage("local _ENV=_ENV;"..s.."; a = bbb + 1", "global 'bbb'")
  92. checkmessage(s.."; local t = {}; a = t.bbb + 1", "field 'bbb'")
  93. checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'")
  94. checkmessage([[aaa=9
  95. repeat until 3==3
  96. local x=math.sin(math.cos(3))
  97. if math.sin(1) == x then return math.sin(1) end -- tail call
  98. local a,b = 1, {
  99. {x='a'..'b'..'c', y='b', z=x},
  100. {1,2,3,4,5} or 3+3<=3+3,
  101. 3+1>3+1,
  102. {d = x and aaa[x or y]}}
  103. ]], "global 'aaa'")
  104. checkmessage([[
  105. local x,y = {},1
  106. if math.sin(1) == 0 then return 3 end -- return
  107. x.a()]], "field 'a'")
  108. checkmessage([[
  109. prefix = nil
  110. insert = nil
  111. while 1 do
  112. local a
  113. if nil then break end
  114. insert(prefix, a)
  115. end]], "global 'insert'")
  116. checkmessage([[ -- tail call
  117. return math.sin("a")
  118. ]], "'sin'")
  119. checkmessage([[collectgarbage("nooption")]], "invalid option")
  120. checkmessage([[x = print .. "a"]], "concatenate")
  121. checkmessage("getmetatable(io.stdin).__gc()", "no value")
  122. checkmessage([[
  123. local Var
  124. local function main()
  125. NoSuchName (function() Var=0 end)
  126. end
  127. main()
  128. ]], "global 'NoSuchName'")
  129. print'+'
  130. a = {}; setmetatable(a, {__index = string})
  131. checkmessage("a:sub()", "bad self")
  132. checkmessage("string.sub('a', {})", "#2")
  133. checkmessage("('a'):sub{}", "#1")
  134. checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'")
  135. -- next message may be 'setmetatable' or '_G.setmetatable'
  136. checkmessage("string.gsub('s', 's', setmetatable)", "setmetatable'")
  137. -- tests for errors in coroutines
  138. function f (n)
  139. local c = coroutine.create(f)
  140. local a,b = coroutine.resume(c)
  141. return b
  142. end
  143. assert(string.find(f(), "C stack overflow"))
  144. checkmessage("coroutine.yield()", "outside a coroutine")
  145. f1 = function () table.sort({1,2,3}, coroutine.yield) end
  146. f = coroutine.wrap(function () return pcall(f1) end)
  147. assert(string.find(select(2, f()), "yield across"))
  148. -- testing size of 'source' info; size of buffer for that info is
  149. -- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'.
  150. idsize = 60 - 1
  151. local function checksize (source)
  152. -- syntax error
  153. local _, msg = load("x", source)
  154. msg = string.match(msg, "^([^:]*):") -- get source (1st part before ':')
  155. assert(msg:len() <= idsize)
  156. end
  157. for i = 60 - 10, 60 + 10 do -- check border cases around 60
  158. checksize("@" .. string.rep("x", i)) -- file names
  159. checksize(string.rep("x", i - 10)) -- string sources
  160. checksize("=" .. string.rep("x", i)) -- exact sources
  161. end
  162. -- testing line error
  163. local function lineerror (s, l)
  164. local err,msg = pcall(load(s))
  165. local line = string.match(msg, ":(%d+):")
  166. assert((line and line+0) == l)
  167. end
  168. lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2)
  169. lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3)
  170. lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4)
  171. lineerror("function a.x.y ()\na=a+1\nend", 1)
  172. lineerror("a = \na\n+\n{}", 3)
  173. lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6)
  174. lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3)
  175. lineerror("a\n=\n-\n\nprint\n;", 3)
  176. lineerror([[
  177. a
  178. (
  179. 23)
  180. ]], 1)
  181. lineerror([[
  182. local a = {x = 13}
  183. a
  184. .
  185. x
  186. (
  187. 23
  188. )
  189. ]], 2)
  190. lineerror([[
  191. local a = {x = 13}
  192. a
  193. .
  194. x
  195. (
  196. 23 + a
  197. )
  198. ]], 6)
  199. local p = [[
  200. function g() f() end
  201. function f(x) error('a', X) end
  202. g()
  203. ]]
  204. X=3;lineerror((p), 3)
  205. X=0;lineerror((p), nil)
  206. X=1;lineerror((p), 2)
  207. X=2;lineerror((p), 1)
  208. if not _soft then
  209. -- several tests that exaust the Lua stack
  210. C = 0
  211. local l = debug.getinfo(1, "l").currentline; function y () C=C+1; y() end
  212. local function checkstackmessage (m)
  213. return (string.find(m, "^.-:%d+: stack overflow"))
  214. end
  215. -- repeated stack overflows (to check stack recovery)
  216. assert(checkstackmessage(doit('y()')))
  217. print('+')
  218. assert(checkstackmessage(doit('y()')))
  219. print('+')
  220. assert(checkstackmessage(doit('y()')))
  221. print('+')
  222. -- error lines in stack overflow
  223. C = 0
  224. local l1
  225. local function g(x)
  226. l1 = debug.getinfo(x, "l").currentline; y()
  227. end
  228. local _, stackmsg = xpcall(g, debug.traceback, 1)
  229. print('+')
  230. local stack = {}
  231. for line in string.gmatch(stackmsg, "[^\n]*") do
  232. local curr = string.match(line, ":(%d+):")
  233. if curr then table.insert(stack, tonumber(curr)) end
  234. end
  235. local i=1
  236. while stack[i] ~= l1 do
  237. assert(stack[i] == l)
  238. i = i+1
  239. end
  240. assert(i > 15)
  241. -- error in error handling
  242. local res, msg = xpcall(error, error)
  243. assert(not res and type(msg) == 'string')
  244. print('+')
  245. local function f (x)
  246. if x==0 then error('a\n')
  247. else
  248. local aux = function () return f(x-1) end
  249. local a,b = xpcall(aux, aux)
  250. return a,b
  251. end
  252. end
  253. f(3)
  254. local function loop (x,y,z) return 1 + loop(x, y, z) end
  255. local res, msg = xpcall(loop, function (m)
  256. assert(string.find(m, "stack overflow"))
  257. local res, msg = pcall(loop)
  258. assert(string.find(msg, "error handling"))
  259. assert(math.sin(0) == 0)
  260. return 15
  261. end)
  262. assert(msg == 15)
  263. res, msg = pcall(function ()
  264. for i = 999900, 1000000, 1 do table.unpack({}, 1, i) end
  265. end)
  266. assert(string.find(msg, "too many results"))
  267. end
  268. -- non string messages
  269. function f() error{msg='x'} end
  270. res, msg = xpcall(f, function (r) return {msg=r.msg..'y'} end)
  271. assert(msg.msg == 'xy')
  272. -- xpcall with arguments
  273. a, b, c = xpcall(string.find, error, "alo", "al")
  274. assert(a and b == 1 and c == 2)
  275. a, b, c = xpcall(string.find, function (x) return {} end, true, "al")
  276. assert(not a and type(b) == "table" and c == nil)
  277. print('+')
  278. checksyntax("syntax error", "", "error", 1)
  279. checksyntax("1.000", "", "1.000", 1)
  280. checksyntax("[[a]]", "", "[[a]]", 1)
  281. checksyntax("'aa'", "", "'aa'", 1)
  282. -- test 255 as first char in a chunk
  283. checksyntax("\255a = 1", "", "char(255)", 1)
  284. doit('I = load("a=9+"); a=3')
  285. assert(a==3 and I == nil)
  286. print('+')
  287. lim = 1000
  288. if _soft then lim = 100 end
  289. for i=1,lim do
  290. doit('a = ')
  291. doit('a = 4+nil')
  292. end
  293. -- testing syntax limits
  294. local function testrep (init, rep)
  295. local s = "local a; "..init .. string.rep(rep, 400)
  296. local a,b = load(s)
  297. assert(not a and string.find(b, "levels"))
  298. end
  299. testrep("a=", "{")
  300. testrep("a=", "(")
  301. testrep("", "a(")
  302. testrep("", "do ")
  303. testrep("", "while a do ")
  304. testrep("", "if a then else ")
  305. testrep("", "function foo () ")
  306. testrep("a=", "a..")
  307. testrep("a=", "a^")
  308. local s = ("a,"):rep(200).."a=nil"
  309. local a,b = load(s)
  310. assert(not a and string.find(b, "levels"))
  311. -- testing other limits
  312. -- upvalues
  313. local lim = 127
  314. local s = "local function fooA ()\n local "
  315. for j = 1,lim do
  316. s = s.."a"..j..", "
  317. end
  318. s = s.."b,c\n"
  319. s = s.."local function fooB ()\n local "
  320. for j = 1,lim do
  321. s = s.."b"..j..", "
  322. end
  323. s = s.."b\n"
  324. s = s.."function fooC () return b+c"
  325. local c = 1+2
  326. for j = 1,lim do
  327. s = s.."+a"..j.."+b"..j
  328. c = c + 2
  329. end
  330. s = s.."\nend end end"
  331. local a,b = load(s)
  332. assert(c > 255 and string.find(b, "too many upvalues") and
  333. string.find(b, "line 5"))
  334. -- local variables
  335. s = "\nfunction foo ()\n local "
  336. for j = 1,300 do
  337. s = s.."a"..j..", "
  338. end
  339. s = s.."b\n"
  340. local a,b = load(s)
  341. assert(string.find(b, "line 2"))
  342. mt.__index = oldmm
  343. print('OK')