errors.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. -- $Id: testes/errors.lua $
  2. -- See Copyright Notice in file all.lua
  3. print("testing errors")
  4. local debug = require"debug"
  5. -- avoid problems with 'strict' module (which may generate other error messages)
  6. local mt = getmetatable(_G) or {}
  7. local oldmm = mt.__index
  8. mt.__index = nil
  9. local function checkerr (msg, f, ...)
  10. local st, err = pcall(f, ...)
  11. assert(not st and string.find(err, msg))
  12. end
  13. local function doit (s)
  14. local f, msg = load(s)
  15. if f == nil then return msg end
  16. local cond, msg = pcall(f)
  17. return (not cond) and msg
  18. end
  19. local function checkmessage (prog, msg)
  20. local m = doit(prog)
  21. assert(string.find(m, msg, 1, true))
  22. end
  23. local function checksyntax (prog, extra, token, line)
  24. local msg = doit(prog)
  25. if not string.find(token, "^<%a") and not string.find(token, "^char%(")
  26. then token = "'"..token.."'" end
  27. token = string.gsub(token, "(%p)", "%%%1")
  28. local pt = string.format([[^%%[string ".*"%%]:%d: .- near %s$]],
  29. line, token)
  30. assert(string.find(msg, pt))
  31. assert(string.find(msg, msg, 1, true))
  32. end
  33. -- test error message with no extra info
  34. assert(doit("error('hi', 0)") == 'hi')
  35. -- test error message with no info
  36. assert(doit("error()") == nil)
  37. -- test common errors/errors that crashed in the past
  38. assert(doit("table.unpack({}, 1, n=2^30)"))
  39. assert(doit("a=math.sin()"))
  40. assert(not doit("tostring(1)") and doit("tostring()"))
  41. assert(doit"tonumber()")
  42. assert(doit"repeat until 1; a")
  43. assert(doit"return;;")
  44. assert(doit"assert(false)")
  45. assert(doit"assert(nil)")
  46. assert(doit("function a (... , ...) end"))
  47. assert(doit("function a (, ...) end"))
  48. assert(doit("local t={}; t = t[#t] + 1"))
  49. checksyntax([[
  50. local a = {4
  51. ]], "'}' expected (to close '{' at line 1)", "<eof>", 3)
  52. if not T then
  53. (Message or print)
  54. ('\n >>> testC not active: skipping memory message test <<<\n')
  55. else
  56. print "testing memory error message"
  57. local a = {}
  58. for i = 1, 10000 do a[i] = true end -- preallocate array
  59. collectgarbage()
  60. T.totalmem(T.totalmem() + 10000)
  61. -- force a memory error (by a small margin)
  62. local st, msg = pcall(function()
  63. for i = 1, 100000 do a[i] = tostring(i) end
  64. end)
  65. T.totalmem(0)
  66. assert(not st and msg == "not enough" .. " memory")
  67. end
  68. -- tests for better error messages
  69. checkmessage("a = {} + 1", "arithmetic")
  70. checkmessage("a = {} | 1", "bitwise operation")
  71. checkmessage("a = {} < 1", "attempt to compare")
  72. checkmessage("a = {} <= 1", "attempt to compare")
  73. checkmessage("a=1; bbbb=2; a=math.sin(3)+bbbb(3)", "global 'bbbb'")
  74. checkmessage("a={}; do local a=1 end a:bbbb(3)", "method 'bbbb'")
  75. checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'")
  76. assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'"))
  77. checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number")
  78. checkmessage("a=(1)..{}", "a table value")
  79. checkmessage("a = #print", "length of a function value")
  80. checkmessage("a = #3", "length of a number value")
  81. aaa = nil
  82. checkmessage("aaa.bbb:ddd(9)", "global 'aaa'")
  83. checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'")
  84. checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'")
  85. checkmessage("local a,b,c; (function () a = b+1.1 end)()", "upvalue 'b'")
  86. assert(not doit"local aaa={bbb={ddd=next}}; aaa.bbb:ddd(nil)")
  87. -- upvalues being indexed do not go to the stack
  88. checkmessage("local a,b,cc; (function () a = cc[1] end)()", "upvalue 'cc'")
  89. checkmessage("local a,b,cc; (function () a.x = 1 end)()", "upvalue 'a'")
  90. checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'")
  91. checkmessage("b=1; local aaa={}; x=aaa+b", "local 'aaa'")
  92. checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'")
  93. checkmessage("aaa=2; b=nil;x=aaa*b", "global 'b'")
  94. checkmessage("aaa={}; x=-aaa", "global 'aaa'")
  95. -- short circuit
  96. checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(3)",
  97. "local 'bbbb'")
  98. checkmessage("a=1; local a,bbbb=2,3; a = bbbb(1) or a(3)", "local 'bbbb'")
  99. checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'")
  100. checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value")
  101. assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'"))
  102. assert(not string.find(doit"aaa={}; (aaa or aaa)()", "'aaa'"))
  103. checkmessage("print(print < 10)", "function with number")
  104. checkmessage("print(print < print)", "two function values")
  105. checkmessage("print('10' < 10)", "string with number")
  106. checkmessage("print(10 < '23')", "number with string")
  107. -- float->integer conversions
  108. checkmessage("local a = 2.0^100; x = a << 2", "local a")
  109. checkmessage("local a = 1 >> 2.0^100", "has no integer representation")
  110. checkmessage("local a = 10.1 << 2.0^100", "has no integer representation")
  111. checkmessage("local a = 2.0^100 & 1", "has no integer representation")
  112. checkmessage("local a = 2.0^100 & 1e100", "has no integer representation")
  113. checkmessage("local a = 2.0 | 1e40", "has no integer representation")
  114. checkmessage("local a = 2e100 ~ 1", "has no integer representation")
  115. checkmessage("string.sub('a', 2.0^100)", "has no integer representation")
  116. checkmessage("string.rep('a', 3.3)", "has no integer representation")
  117. checkmessage("return 6e40 & 7", "has no integer representation")
  118. checkmessage("return 34 << 7e30", "has no integer representation")
  119. checkmessage("return ~-3e40", "has no integer representation")
  120. checkmessage("return ~-3.009", "has no integer representation")
  121. checkmessage("return 3.009 & 1", "has no integer representation")
  122. checkmessage("return 34 >> {}", "table value")
  123. checkmessage("a = 24 // 0", "divide by zero")
  124. checkmessage("a = 1 % 0", "'n%0'")
  125. -- numeric for loops
  126. checkmessage("for i = {}, 10 do end", "table")
  127. checkmessage("for i = io.stdin, 10 do end", "FILE")
  128. checkmessage("for i = {}, 10 do end", "initial value")
  129. checkmessage("for i = 1, 'x', 10 do end", "string")
  130. checkmessage("for i = 1, {}, 10 do end", "limit")
  131. checkmessage("for i = 1, {} do end", "limit")
  132. checkmessage("for i = 1, 10, print do end", "step")
  133. checkmessage("for i = 1, 10, print do end", "function")
  134. -- passing light userdata instead of full userdata
  135. _G.D = debug
  136. checkmessage([[
  137. -- create light udata
  138. local x = D.upvalueid(function () return debug end, 1)
  139. D.setuservalue(x, {})
  140. ]], "light userdata")
  141. _G.D = nil
  142. do -- named objects (field '__name')
  143. checkmessage("math.sin(io.input())", "(number expected, got FILE*)")
  144. _G.XX = setmetatable({}, {__name = "My Type"})
  145. assert(string.find(tostring(XX), "^My Type"))
  146. checkmessage("io.input(XX)", "(FILE* expected, got My Type)")
  147. checkmessage("return XX + 1", "on a My Type value")
  148. checkmessage("return ~io.stdin", "on a FILE* value")
  149. checkmessage("return XX < XX", "two My Type values")
  150. checkmessage("return {} < XX", "table with My Type")
  151. checkmessage("return XX < io.stdin", "My Type with FILE*")
  152. _G.XX = nil
  153. end
  154. -- global functions
  155. checkmessage("(io.write or print){}", "io.write")
  156. checkmessage("(collectgarbage or print){}", "collectgarbage")
  157. -- errors in functions without debug info
  158. do
  159. local f = function (a) return a + 1 end
  160. f = assert(load(string.dump(f, true)))
  161. assert(f(3) == 4)
  162. checkerr("^%?:%-1:", f, {})
  163. -- code with a move to a local var ('OP_MOV A B' with A<B)
  164. f = function () local a; a = {}; return a + 2 end
  165. -- no debug info (so that 'a' is unknown)
  166. f = assert(load(string.dump(f, true)))
  167. -- symbolic execution should not get lost
  168. checkerr("^%?:%-1:.*table value", f)
  169. end
  170. -- tests for field accesses after RK limit
  171. local t = {}
  172. for i = 1, 1000 do
  173. t[i] = "a = x" .. i
  174. end
  175. local s = table.concat(t, "; ")
  176. t = nil
  177. checkmessage(s.."; a = bbb + 1", "global 'bbb'")
  178. checkmessage("local _ENV=_ENV;"..s.."; a = bbb + 1", "global 'bbb'")
  179. checkmessage(s.."; local t = {}; a = t.bbb + 1", "field 'bbb'")
  180. checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'")
  181. checkmessage([[aaa=9
  182. repeat until 3==3
  183. local x=math.sin(math.cos(3))
  184. if math.sin(1) == x then return math.sin(1) end -- tail call
  185. local a,b = 1, {
  186. {x='a'..'b'..'c', y='b', z=x},
  187. {1,2,3,4,5} or 3+3<=3+3,
  188. 3+1>3+1,
  189. {d = x and aaa[x or y]}}
  190. ]], "global 'aaa'")
  191. checkmessage([[
  192. local x,y = {},1
  193. if math.sin(1) == 0 then return 3 end -- return
  194. x.a()]], "field 'a'")
  195. checkmessage([[
  196. prefix = nil
  197. insert = nil
  198. while 1 do
  199. local a
  200. if nil then break end
  201. insert(prefix, a)
  202. end]], "global 'insert'")
  203. checkmessage([[ -- tail call
  204. return math.sin("a")
  205. ]], "'sin'")
  206. checkmessage([[collectgarbage("nooption")]], "invalid option")
  207. checkmessage([[x = print .. "a"]], "concatenate")
  208. checkmessage([[x = "a" .. false]], "concatenate")
  209. checkmessage([[x = {} .. 2]], "concatenate")
  210. checkmessage("getmetatable(io.stdin).__gc()", "no value")
  211. checkmessage([[
  212. local Var
  213. local function main()
  214. NoSuchName (function() Var=0 end)
  215. end
  216. main()
  217. ]], "global 'NoSuchName'")
  218. print'+'
  219. a = {}; setmetatable(a, {__index = string})
  220. checkmessage("a:sub()", "bad self")
  221. checkmessage("string.sub('a', {})", "#2")
  222. checkmessage("('a'):sub{}", "#1")
  223. checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'")
  224. checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'")
  225. -- tests for errors in coroutines
  226. local function f (n)
  227. local c = coroutine.create(f)
  228. local a,b = coroutine.resume(c)
  229. return b
  230. end
  231. assert(string.find(f(), "C stack overflow"))
  232. checkmessage("coroutine.yield()", "outside a coroutine")
  233. f = coroutine.wrap(function () table.sort({1,2,3}, coroutine.yield) end)
  234. checkerr("yield across", f)
  235. -- testing size of 'source' info; size of buffer for that info is
  236. -- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'.
  237. idsize = 60 - 1
  238. local function checksize (source)
  239. -- syntax error
  240. local _, msg = load("x", source)
  241. msg = string.match(msg, "^([^:]*):") -- get source (1st part before ':')
  242. assert(msg:len() <= idsize)
  243. end
  244. for i = 60 - 10, 60 + 10 do -- check border cases around 60
  245. checksize("@" .. string.rep("x", i)) -- file names
  246. checksize(string.rep("x", i - 10)) -- string sources
  247. checksize("=" .. string.rep("x", i)) -- exact sources
  248. end
  249. -- testing line error
  250. local function lineerror (s, l)
  251. local err,msg = pcall(load(s))
  252. local line = string.match(msg, ":(%d+):")
  253. assert(tonumber(line) == l)
  254. end
  255. lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2)
  256. lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3)
  257. lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4)
  258. lineerror("function a.x.y ()\na=a+1\nend", 1)
  259. lineerror("a = \na\n+\n{}", 3)
  260. lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6)
  261. lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3)
  262. lineerror("a\n=\n-\n\nprint\n;", 3)
  263. lineerror([[
  264. a
  265. (
  266. 23)
  267. ]], 1)
  268. lineerror([[
  269. local a = {x = 13}
  270. a
  271. .
  272. x
  273. (
  274. 23
  275. )
  276. ]], 2)
  277. lineerror([[
  278. local a = {x = 13}
  279. a
  280. .
  281. x
  282. (
  283. 23 + a
  284. )
  285. ]], 6)
  286. local p = [[
  287. function g() f() end
  288. function f(x) error('a', X) end
  289. g()
  290. ]]
  291. X=3;lineerror((p), 3)
  292. X=0;lineerror((p), nil)
  293. X=1;lineerror((p), 2)
  294. X=2;lineerror((p), 1)
  295. lineerror([[
  296. local b = false
  297. if not b then
  298. error 'test'
  299. end]], 3)
  300. lineerror([[
  301. local b = false
  302. if not b then
  303. if not b then
  304. if not b then
  305. error 'test'
  306. end
  307. end
  308. end]], 5)
  309. if not _soft then
  310. -- several tests that exaust the Lua stack
  311. collectgarbage()
  312. print"testing stack overflow"
  313. C = 0
  314. local l = debug.getinfo(1, "l").currentline; function y () C=C+1; y() end
  315. local function checkstackmessage (m)
  316. return (string.find(m, "stack overflow"))
  317. end
  318. -- repeated stack overflows (to check stack recovery)
  319. assert(checkstackmessage(doit('y()')))
  320. print('+')
  321. assert(checkstackmessage(doit('y()')))
  322. print('+')
  323. assert(checkstackmessage(doit('y()')))
  324. print('+')
  325. -- error lines in stack overflow
  326. C = 0
  327. local l1
  328. local function g(x)
  329. l1 = debug.getinfo(x, "l").currentline; y()
  330. end
  331. local _, stackmsg = xpcall(g, debug.traceback, 1)
  332. print('+')
  333. local stack = {}
  334. for line in string.gmatch(stackmsg, "[^\n]*") do
  335. local curr = string.match(line, ":(%d+):")
  336. if curr then table.insert(stack, tonumber(curr)) end
  337. end
  338. local i=1
  339. while stack[i] ~= l1 do
  340. assert(stack[i] == l)
  341. i = i+1
  342. end
  343. assert(i > 15)
  344. -- error in error handling
  345. local res, msg = xpcall(error, error)
  346. assert(not res and type(msg) == 'string')
  347. print('+')
  348. local function f (x)
  349. if x==0 then error('a\n')
  350. else
  351. local aux = function () return f(x-1) end
  352. local a,b = xpcall(aux, aux)
  353. return a,b
  354. end
  355. end
  356. f(3)
  357. local function loop (x,y,z) return 1 + loop(x, y, z) end
  358. local res, msg = xpcall(loop, function (m)
  359. assert(string.find(m, "stack overflow"))
  360. checkerr("error handling", loop)
  361. assert(math.sin(0) == 0)
  362. return 15
  363. end)
  364. assert(msg == 15)
  365. local f = function ()
  366. for i = 999900, 1000000, 1 do table.unpack({}, 1, i) end
  367. end
  368. checkerr("too many results", f)
  369. end
  370. do
  371. -- non string messages
  372. local t = {}
  373. local res, msg = pcall(function () error(t) end)
  374. assert(not res and msg == t)
  375. res, msg = pcall(function () error(nil) end)
  376. assert(not res and msg == nil)
  377. local function f() error{msg='x'} end
  378. res, msg = xpcall(f, function (r) return {msg=r.msg..'y'} end)
  379. assert(msg.msg == 'xy')
  380. -- 'assert' with extra arguments
  381. res, msg = pcall(assert, false, "X", t)
  382. assert(not res and msg == "X")
  383. -- 'assert' with no message
  384. res, msg = pcall(function () assert(false) end)
  385. local line = string.match(msg, "%w+%.lua:(%d+): assertion failed!$")
  386. assert(tonumber(line) == debug.getinfo(1, "l").currentline - 2)
  387. -- 'assert' with non-string messages
  388. res, msg = pcall(assert, false, t)
  389. assert(not res and msg == t)
  390. res, msg = pcall(assert, nil, nil)
  391. assert(not res and msg == nil)
  392. -- 'assert' without arguments
  393. res, msg = pcall(assert)
  394. assert(not res and string.find(msg, "value expected"))
  395. end
  396. -- xpcall with arguments
  397. a, b, c = xpcall(string.find, error, "alo", "al")
  398. assert(a and b == 1 and c == 2)
  399. a, b, c = xpcall(string.find, function (x) return {} end, true, "al")
  400. assert(not a and type(b) == "table" and c == nil)
  401. print("testing tokens in error messages")
  402. checksyntax("syntax error", "", "error", 1)
  403. checksyntax("1.000", "", "1.000", 1)
  404. checksyntax("[[a]]", "", "[[a]]", 1)
  405. checksyntax("'aa'", "", "'aa'", 1)
  406. checksyntax("while << do end", "", "<<", 1)
  407. checksyntax("for >> do end", "", ">>", 1)
  408. -- test invalid non-printable char in a chunk
  409. checksyntax("a\1a = 1", "", "<\\1>", 1)
  410. -- test 255 as first char in a chunk
  411. checksyntax("\255a = 1", "", "<\\255>", 1)
  412. doit('I = load("a=9+"); a=3')
  413. assert(a==3 and I == nil)
  414. print('+')
  415. lim = 1000
  416. if _soft then lim = 100 end
  417. for i=1,lim do
  418. doit('a = ')
  419. doit('a = 4+nil')
  420. end
  421. -- testing syntax limits
  422. local function testrep (init, rep, close, repc)
  423. local s = init .. string.rep(rep, 100) .. close .. string.rep(repc, 100)
  424. assert(load(s)) -- 100 levels is OK
  425. s = init .. string.rep(rep, 10000)
  426. local res, msg = load(s) -- 10000 levels not ok
  427. assert(not res and (string.find(msg, "too many registers") or
  428. string.find(msg, "stack overflow")))
  429. end
  430. testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment
  431. testrep("local a; a=", "{", "0", "}")
  432. testrep("local a; a=", "(", "2", ")")
  433. testrep("local a; ", "a(", "2", ")")
  434. testrep("", "do ", "", " end")
  435. testrep("", "while a do ", "", " end")
  436. testrep("local a; ", "if a then else ", "", " end")
  437. testrep("", "function foo () ", "", " end")
  438. testrep("local a; a=", "a..", "a", "")
  439. testrep("local a; a=", "a^", "a", "")
  440. checkmessage("a = f(x" .. string.rep(",x", 260) .. ")", "too many registers")
  441. -- testing other limits
  442. -- upvalues
  443. local lim = 127
  444. local s = "local function fooA ()\n local "
  445. for j = 1,lim do
  446. s = s.."a"..j..", "
  447. end
  448. s = s.."b,c\n"
  449. s = s.."local function fooB ()\n local "
  450. for j = 1,lim do
  451. s = s.."b"..j..", "
  452. end
  453. s = s.."b\n"
  454. s = s.."function fooC () return b+c"
  455. local c = 1+2
  456. for j = 1,lim do
  457. s = s.."+a"..j.."+b"..j
  458. c = c + 2
  459. end
  460. s = s.."\nend end end"
  461. local a,b = load(s)
  462. assert(c > 255 and string.find(b, "too many upvalues") and
  463. string.find(b, "line 5"))
  464. -- local variables
  465. s = "\nfunction foo ()\n local "
  466. for j = 1,300 do
  467. s = s.."a"..j..", "
  468. end
  469. s = s.."b\n"
  470. local a,b = load(s)
  471. assert(string.find(b, "line 2") and string.find(b, "too many local variables"))
  472. mt.__index = oldmm
  473. print('OK')