From 8cb84210ab95e882c01363a6794508ec923ade90 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 13 Nov 2018 13:50:33 -0200 Subject: String buffer using to-be-closed variable The string buffers in the C API now mark their boxes as to-be-closed variables, to release their buffers in case of errors. --- lauxlib.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'lauxlib.c') diff --git a/lauxlib.c b/lauxlib.c index 68842a98..6069ed1b 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -470,10 +470,8 @@ static void *resizebox (lua_State *L, int idx, size_t newsize) { lua_Alloc allocf = lua_getallocf(L, &ud); UBox *box = (UBox *)lua_touserdata(L, idx); void *temp = allocf(ud, box->box, box->bsize, newsize); - if (temp == NULL && newsize > 0) { /* allocation error? */ - resizebox(L, idx, 0); /* free buffer */ + if (temp == NULL && newsize > 0) /* allocation error? */ luaL_error(L, "not enough memory for buffer allocation"); - } box->box = temp; box->bsize = newsize; return temp; @@ -486,16 +484,20 @@ static int boxgc (lua_State *L) { } -static void *newbox (lua_State *L, size_t newsize) { +static const luaL_Reg boxmt[] = { /* box metamethods */ + {"__gc", boxgc}, + {"__close", boxgc}, + {NULL, NULL} +}; + + +static void newbox (lua_State *L) { UBox *box = (UBox *)lua_newuserdatauv(L, sizeof(UBox), 0); box->box = NULL; box->bsize = 0; - if (luaL_newmetatable(L, "_UBOX*")) { /* creating metatable? */ - lua_pushcfunction(L, boxgc); - lua_setfield(L, -2, "__gc"); /* metatable.__gc = boxgc */ - } + if (luaL_newmetatable(L, "_UBOX*")) /* creating metatable? */ + luaL_setfuncs(L, boxmt, 0); /* set its metamethods */ lua_setmetatable(L, -2); - return resizebox(L, -1, newsize); } @@ -536,9 +538,11 @@ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { if (buffonstack(B)) /* buffer already has a box? */ newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */ else { /* no box yet */ - newbuff = (char *)newbox(L, newsize); /* create a new box */ - memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ + newbox(L); /* create a new box */ lua_insert(L, boxidx); /* move box to its intended position */ + lua_toclose(L, boxidx); + newbuff = (char *)resizebox(L, boxidx, newsize); + memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ } B->b = newbuff; B->size = newsize; -- cgit v1.2.3-55-g6feb