errors.lua 17 KB

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