errors.lua 15 KB

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