New macro 'pushvfstring'

Helps to ensure that 'luaO_pushvfstring' is being called correctly,
with an error check after closing the vararg list with 'va_end'.
This commit is contained in:
Roberto Ierusalimschy 2025-04-23 11:55:04 -03:00
parent 9b014d4bcd
commit e055905914
4 changed files with 13 additions and 16 deletions

6
lapi.c
View file

@ -593,12 +593,8 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
const char *ret; const char *ret;
va_list argp; va_list argp;
lua_lock(L); lua_lock(L);
va_start(argp, fmt); pushvfstring(L, argp, fmt, ret);
ret = luaO_pushvfstring(L, fmt, argp);
va_end(argp);
luaC_checkGC(L); luaC_checkGC(L);
if (ret == NULL) /* error? */
luaD_throw(L, LUA_ERRMEM);
lua_unlock(L); lua_unlock(L);
return ret; return ret;
} }

View file

@ -43,11 +43,7 @@ static int codesJ (FuncState *fs, OpCode o, int sj, int k);
l_noret luaK_semerror (LexState *ls, const char *fmt, ...) { l_noret luaK_semerror (LexState *ls, const char *fmt, ...) {
const char *msg; const char *msg;
va_list argp; va_list argp;
va_start(argp, fmt); pushvfstring(ls->L, argp, fmt, msg);
msg = luaO_pushvfstring(ls->L, fmt, argp);
va_end(argp);
if (msg == NULL) /* error? */
luaD_throw(ls->L, LUA_ERRMEM);
ls->t.token = 0; /* remove "near <token>" from final message */ ls->t.token = 0; /* remove "near <token>" from final message */
luaX_syntaxerror(ls, msg); luaX_syntaxerror(ls, msg);
} }

View file

@ -852,12 +852,8 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
const char *msg; const char *msg;
va_list argp; va_list argp;
luaC_checkGC(L); /* error message uses memory */ luaC_checkGC(L); /* error message uses memory */
va_start(argp, fmt); pushvfstring(L, argp, fmt, msg);
msg = luaO_pushvfstring(L, fmt, argp); /* format message */ if (isLua(ci)) { /* Lua function? */
va_end(argp);
if (msg == NULL) /* no memory to format message? */
luaD_throw(L, LUA_ERRMEM);
else if (isLua(ci)) { /* Lua function? */
/* add source:line information */ /* add source:line information */
luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci)); luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci));
setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */ setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */

View file

@ -822,6 +822,15 @@ typedef struct Table {
/* size of buffer for 'luaO_utf8esc' function */ /* size of buffer for 'luaO_utf8esc' function */
#define UTF8BUFFSZ 8 #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 int luaO_utf8esc (char *buff, unsigned long x);
LUAI_FUNC lu_byte luaO_ceillog2 (unsigned int x); LUAI_FUNC lu_byte luaO_ceillog2 (unsigned int x);
LUAI_FUNC lu_byte luaO_codeparam (unsigned int p); LUAI_FUNC lu_byte luaO_codeparam (unsigned int p);