Skip to content

Commit

Permalink
issue #163 simplify defer statement and fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyendumajumdar committed Oct 6, 2019
1 parent 4b92f52 commit c8e0163
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
11 changes: 6 additions & 5 deletions src/lfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,15 @@ int luaF_close (lua_State *L, StkId level, int status) {
UpVal *uv;
while (L->openupval != NULL && (uv = L->openupval)->v >= level) {
lua_assert(upisopen(uv));
L->openupval = uv->u.open.next; /* remove from 'open' list */
if (uv->refcount == 0) { /* no references? */
if (uv->flags && ttisfunction(uv->v)) {
L->openupval = uv->u.open.next; /* remove from 'open' list */
if (uv->refcount == 0) { /* no references? */
UpVal uv1 = *uv; /* copy the upvalue as we will free it below */
luaM_free(L, uv); /* free upvalue before invoking any deferred functions */
if (uv1.flags && ttisfunction(uv1.v)) {
ptrdiff_t levelrel = savestack(L, level);
status = calldeferredfunction(L, uv->v, status);
status = calldeferredfunction(L, uv1.v, status);
level = restorestack(L, levelrel);
}
luaM_free(L, uv); /* free upvalue */ /* FIXME leak if error occurs above */
}
else {
setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */
Expand Down
17 changes: 9 additions & 8 deletions src/lparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1298,13 +1298,15 @@ static void body (LexState *ls, expdesc *e, int ismethod, int line, int deferred
new_fs.f = addprototype(ls);
new_fs.f->linedefined = line;
open_func(ls, &new_fs, &bl);
checknext(ls, '(');
if (ismethod) {
new_localvarliteral(ls, "self"); /* create 'self' parameter */
adjustlocalvars(ls, 1);
if (!deferred) {
checknext(ls, '(');
if (ismethod) {
new_localvarliteral(ls, "self"); /* create 'self' parameter */
adjustlocalvars(ls, 1);
}
parlist(ls);
checknext(ls, ')');
}
parlist(ls);
checknext(ls, ')');
statlist(ls);
new_fs.f->lastlinedefined = ls->linenumber;
check_match(ls, TK_END, TK_FUNCTION, line);
Expand Down Expand Up @@ -2359,8 +2361,7 @@ static void statement (LexState *ls) {
break;
}
case TK_DEFER: { /* stat -> deferstat */
luaX_next(ls); /* skip LOCAL */
checknext(ls, TK_FUNCTION);
luaX_next(ls); /* skip DEFER */
localfunc(ls, 1);
break;
}
Expand Down

0 comments on commit c8e0163

Please sign in to comment.