浏览代码

Bug: luaL_traceback may need more than 5 stack slots

Roberto Ierusalimschy 1 年之前
父节点
当前提交
2db966fcbf
共有 3 个文件被更改,包括 20 次插入1 次删除
  1. 1 0
      lauxlib.c
  2. 5 0
      ltests.c
  3. 14 1
      testes/errors.lua

+ 1 - 0
lauxlib.c

@@ -80,6 +80,7 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
   int top = lua_gettop(L);
   lua_getinfo(L, "f", ar);  /* push function */
   lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
+  luaL_checkstack(L, 6, "not enough stack");  /* slots for 'findfield' */
   if (findfield(L, top + 1, 2)) {
     const char *name = lua_tostring(L, -1);
     if (strncmp(name, LUA_GNAME ".", 3) == 0) {  /* name start with '_G.'? */

+ 5 - 0
ltests.c

@@ -1650,6 +1650,11 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
       int nres;
       status = lua_resume(lua_tothread(L1, i), L, getnum, &nres);
     }
+    else if EQ("traceback") {
+      const char *msg = getstring;
+      int level = getnum;
+      luaL_traceback(L1, L1, msg, level);
+    }
     else if EQ("return") {
       int n = getnum;
       if (L1 != L) {

+ 14 - 1
testes/errors.lua

@@ -91,7 +91,7 @@ end
 
 if not T then
   (Message or print)
-    ('\n >>> testC not active: skipping memory message test <<<\n')
+    ('\n >>> testC not active: skipping tests for messages in C <<<\n')
 else
   print "testing memory error message"
   local a = {}
@@ -104,6 +104,19 @@ else
   end)
   T.totalmem(0)
   assert(not st and msg == "not enough" .. " memory")
+
+  -- stack space for luaL_traceback (bug in 5.4.6)
+  local res = T.testC[[
+    # push 16 elements on the stack
+    pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1;
+    pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1;
+    pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1;
+    pushnum 1;
+    # traceback should work with 4 remaining slots
+    traceback xuxu 1;
+    return 1
+  ]]
+  assert(string.find(res, "xuxu.-main chunk"))
 end