diff --git a/lapi.c b/lapi.c index f59430a7..769eba13 100644 --- a/lapi.c +++ b/lapi.c @@ -593,12 +593,8 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { const char *ret; va_list argp; lua_lock(L); - va_start(argp, fmt); - ret = luaO_pushvfstring(L, fmt, argp); - va_end(argp); + pushvfstring(L, argp, fmt, ret); luaC_checkGC(L); - if (ret == NULL) /* error? */ - luaD_throw(L, LUA_ERRMEM); lua_unlock(L); return ret; } diff --git a/lcode.c b/lcode.c index d22a081a..e8b9bb5d 100644 --- a/lcode.c +++ b/lcode.c @@ -43,11 +43,7 @@ static int codesJ (FuncState *fs, OpCode o, int sj, int k); l_noret luaK_semerror (LexState *ls, const char *fmt, ...) { const char *msg; va_list argp; - va_start(argp, fmt); - msg = luaO_pushvfstring(ls->L, fmt, argp); - va_end(argp); - if (msg == NULL) /* error? */ - luaD_throw(ls->L, LUA_ERRMEM); + pushvfstring(ls->L, argp, fmt, msg); ls->t.token = 0; /* remove "near " from final message */ luaX_syntaxerror(ls, msg); } diff --git a/ldebug.c b/ldebug.c index 258a4394..f4bb0a08 100644 --- a/ldebug.c +++ b/ldebug.c @@ -852,12 +852,8 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { const char *msg; va_list argp; luaC_checkGC(L); /* error message uses memory */ - va_start(argp, fmt); - msg = luaO_pushvfstring(L, fmt, argp); /* format message */ - va_end(argp); - if (msg == NULL) /* no memory to format message? */ - luaD_throw(L, LUA_ERRMEM); - else if (isLua(ci)) { /* Lua function? */ + pushvfstring(L, argp, fmt, msg); + if (isLua(ci)) { /* Lua function? */ /* add source:line information */ luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci)); setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */ diff --git a/lobject.h b/lobject.h index 8c06a224..b5ca3668 100644 --- a/lobject.h +++ b/lobject.h @@ -822,6 +822,15 @@ typedef struct Table { /* size of buffer for 'luaO_utf8esc' function */ #define UTF8BUFFSZ 8 + +/* macro to call 'luaO_pushvfstring' correctly */ +#define pushvfstring(L, argp, fmt, msg) \ + { va_start(argp, fmt); \ + msg = luaO_pushvfstring(L, fmt, argp); \ + va_end(argp); \ + if (msg == NULL) luaD_throw(L, LUA_ERRMEM); /* only after 'va_end' */ } + + LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x); LUAI_FUNC lu_byte luaO_ceillog2 (unsigned int x); LUAI_FUNC lu_byte luaO_codeparam (unsigned int p);