Skip to content

Commit 8911098

Browse files
committed
bug in tailcall of vararg functions
(when adjusting missing parameters)
1 parent 53979df commit 8911098

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

ldo.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: ldo.c,v 2.186 2018/01/10 19:19:27 roberto Exp roberto $
2+
** $Id: ldo.c,v 2.187 2018/01/28 12:08:04 roberto Exp roberto $
33
** Stack and Call structure of Lua
44
** See Copyright Notice in lua.h
55
*/
@@ -405,25 +405,27 @@ void luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) {
405405

406406
/*
407407
** Prepare a function for a tail call, building its call info on top
408-
** of the current call info. 'n' is the number of arguments plus 1
408+
** of the current call info. 'narg1' is the number of arguments plus 1
409409
** (so that it includes the function itself).
410410
*/
411-
void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n) {
411+
void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) {
412412
Proto *p = clLvalue(s2v(func))->p;
413413
int fsize = p->maxstacksize; /* frame size */
414+
int nfixparams = p->numparams;
414415
int i;
415-
for (i = 0; i < n; i++) /* move down function and arguments */
416+
for (i = 0; i < narg1; i++) /* move down function and arguments */
416417
setobjs2s(L, ci->func + i, func + i);
417418
checkstackp(L, fsize, func);
418-
for (; i <= p->numparams; i++)
419-
setnilvalue(s2v(ci->func + i)); /* complete missing arguments */
420-
ci->top = ci->func + 1 + fsize; /* top for new function */
419+
func = ci->func; /* moved-down function */
420+
for (; narg1 <= nfixparams; narg1++)
421+
setnilvalue(s2v(func + narg1)); /* complete missing arguments */
422+
ci->top = func + 1 + fsize; /* top for new function */
421423
lua_assert(ci->top <= L->stack_last);
422424
ci->u.l.savedpc = p->code; /* starting point */
423425
ci->callstatus |= CIST_TAIL;
424426
if (p->is_vararg) {
425-
L->top -= (func - ci->func); /* move down top */
426-
luaT_adjustvarargs(L, p, n - 1);
427+
L->top = func + narg1; /* set top */
428+
luaT_adjustvarargs(L, nfixparams, narg1 - 1);
427429
}
428430
if (L->hookmask)
429431
hookcall(L, ci, 1);
@@ -464,12 +466,13 @@ void luaD_call (lua_State *L, StkId func, int nresults) {
464466
luaD_poscall(L, ci, L->top - n, n);
465467
break;
466468
}
467-
case LUA_TLCL: { /* Lua function: prepare its call */
469+
case LUA_TLCL: { /* Lua function */
468470
Proto *p = clLvalue(funcv)->p;
469-
int n = cast_int(L->top - func) - 1; /* number of real arguments */
471+
int narg = cast_int(L->top - func) - 1; /* number of real arguments */
472+
int nfixparams = p->numparams;
470473
int fsize = p->maxstacksize; /* frame size */
471474
checkstackp(L, fsize, func);
472-
for (; n < p->numparams; n++)
475+
for (; narg < nfixparams; narg++)
473476
setnilvalue(s2v(L->top++)); /* complete missing arguments */
474477
ci = next_ci(L); /* now 'enter' new function */
475478
ci->nresults = nresults;
@@ -479,7 +482,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) {
479482
ci->u.l.savedpc = p->code; /* starting point */
480483
ci->callstatus = 0;
481484
if (p->is_vararg)
482-
luaT_adjustvarargs(L, p, n); /* may invoke GC */
485+
luaT_adjustvarargs(L, nfixparams, narg); /* may invoke GC */
483486
if (L->hookmask)
484487
hookcall(L, ci, 0);
485488
luaV_execute(L, ci); /* run the function */

ltm.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: ltm.c,v 2.56 2017/12/28 15:42:57 roberto Exp roberto $
2+
** $Id: ltm.c,v 2.57 2018/01/28 12:08:04 roberto Exp roberto $
33
** Tag methods
44
** See Copyright Notice in lua.h
55
*/
@@ -216,21 +216,20 @@ int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
216216
}
217217

218218

219-
void luaT_adjustvarargs (lua_State *L, Proto *p, int actual) {
219+
void luaT_adjustvarargs (lua_State *L, int nfixparams, int actual) {
220220
int i;
221221
Table *vtab;
222222
TValue nname;
223-
int nfixparams = p->numparams; /* number of fixed parameters */
224-
actual -= nfixparams; /* number of extra arguments */
223+
int nextra = actual - nfixparams; /* number of extra arguments */
225224
vtab = luaH_new(L); /* create vararg table */
226225
sethvalue2s(L, L->top, vtab); /* anchor it for resizing */
227226
L->top++; /* space ensured by caller */
228-
luaH_resize(L, vtab, actual, 1);
229-
for (i = 0; i < actual; i++) /* put extra arguments into vararg table */
230-
setobj2n(L, &vtab->array[i], s2v(L->top - actual + i - 1));
227+
luaH_resize(L, vtab, nextra, 1);
228+
for (i = 0; i < nextra; i++) /* put extra arguments into vararg table */
229+
setobj2n(L, &vtab->array[i], s2v(L->top - nextra + i - 1));
231230
setsvalue(L, &nname, G(L)->nfield); /* get field 'n' */
232-
setivalue(luaH_set(L, vtab, &nname), actual); /* store counter there */
233-
L->top -= actual; /* remove extra elements from the stack */
231+
setivalue(luaH_set(L, vtab, &nname), nextra); /* store counter there */
232+
L->top -= nextra; /* remove extra elements from the stack */
234233
sethvalue2s(L, L->top - 1, vtab); /* move table to new top */
235234
luaC_checkGC(L);
236235
}

ltm.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: ltm.h,v 2.27 2017/11/27 17:44:31 roberto Exp roberto $
2+
** $Id: ltm.h,v 2.28 2017/12/13 18:32:09 roberto Exp roberto $
33
** Tag methods
44
** See Copyright Notice in lua.h
55
*/
@@ -77,7 +77,7 @@ LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1,
7777
LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
7878
int inv, TMS event);
7979

80-
LUAI_FUNC void luaT_adjustvarargs (lua_State *L, Proto *p, int actual);
80+
LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, int actual);
8181
LUAI_FUNC void luaT_getvarargs (lua_State *L, TValue *t, StkId where,
8282
int wanted);
8383

0 commit comments

Comments
 (0)