diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-04-23 11:55:04 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-04-23 11:55:04 -0300 |
| commit | e05590591410a5e007a1e3f1691f6c1cf9d8fe45 (patch) | |
| tree | cd13636363ff66a9d56f979293e353975c36b77b | |
| parent | 9b014d4bcd4ebadb523f1c1a1d38148d8526e5ed (diff) | |
| download | lua-e05590591410a5e007a1e3f1691f6c1cf9d8fe45.tar.gz lua-e05590591410a5e007a1e3f1691f6c1cf9d8fe45.tar.bz2 lua-e05590591410a5e007a1e3f1691f6c1cf9d8fe45.zip | |
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'.
| -rw-r--r-- | lapi.c | 6 | ||||
| -rw-r--r-- | lcode.c | 6 | ||||
| -rw-r--r-- | ldebug.c | 8 | ||||
| -rw-r--r-- | lobject.h | 9 |
4 files changed, 13 insertions, 16 deletions
| @@ -593,12 +593,8 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { | |||
| 593 | const char *ret; | 593 | const char *ret; |
| 594 | va_list argp; | 594 | va_list argp; |
| 595 | lua_lock(L); | 595 | lua_lock(L); |
| 596 | va_start(argp, fmt); | 596 | pushvfstring(L, argp, fmt, ret); |
| 597 | ret = luaO_pushvfstring(L, fmt, argp); | ||
| 598 | va_end(argp); | ||
| 599 | luaC_checkGC(L); | 597 | luaC_checkGC(L); |
| 600 | if (ret == NULL) /* error? */ | ||
| 601 | luaD_throw(L, LUA_ERRMEM); | ||
| 602 | lua_unlock(L); | 598 | lua_unlock(L); |
| 603 | return ret; | 599 | return ret; |
| 604 | } | 600 | } |
| @@ -43,11 +43,7 @@ static int codesJ (FuncState *fs, OpCode o, int sj, int k); | |||
| 43 | l_noret luaK_semerror (LexState *ls, const char *fmt, ...) { | 43 | l_noret luaK_semerror (LexState *ls, const char *fmt, ...) { |
| 44 | const char *msg; | 44 | const char *msg; |
| 45 | va_list argp; | 45 | va_list argp; |
| 46 | va_start(argp, fmt); | 46 | pushvfstring(ls->L, argp, fmt, msg); |
| 47 | msg = luaO_pushvfstring(ls->L, fmt, argp); | ||
| 48 | va_end(argp); | ||
| 49 | if (msg == NULL) /* error? */ | ||
| 50 | luaD_throw(ls->L, LUA_ERRMEM); | ||
| 51 | ls->t.token = 0; /* remove "near <token>" from final message */ | 47 | ls->t.token = 0; /* remove "near <token>" from final message */ |
| 52 | luaX_syntaxerror(ls, msg); | 48 | luaX_syntaxerror(ls, msg); |
| 53 | } | 49 | } |
| @@ -852,12 +852,8 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { | |||
| 852 | const char *msg; | 852 | const char *msg; |
| 853 | va_list argp; | 853 | va_list argp; |
| 854 | luaC_checkGC(L); /* error message uses memory */ | 854 | luaC_checkGC(L); /* error message uses memory */ |
| 855 | va_start(argp, fmt); | 855 | pushvfstring(L, argp, fmt, msg); |
| 856 | msg = luaO_pushvfstring(L, fmt, argp); /* format message */ | 856 | if (isLua(ci)) { /* Lua function? */ |
| 857 | va_end(argp); | ||
| 858 | if (msg == NULL) /* no memory to format message? */ | ||
| 859 | luaD_throw(L, LUA_ERRMEM); | ||
| 860 | else if (isLua(ci)) { /* Lua function? */ | ||
| 861 | /* add source:line information */ | 857 | /* add source:line information */ |
| 862 | luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci)); | 858 | luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci)); |
| 863 | setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */ | 859 | setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */ |
| @@ -822,6 +822,15 @@ typedef struct Table { | |||
| 822 | /* size of buffer for 'luaO_utf8esc' function */ | 822 | /* size of buffer for 'luaO_utf8esc' function */ |
| 823 | #define UTF8BUFFSZ 8 | 823 | #define UTF8BUFFSZ 8 |
| 824 | 824 | ||
| 825 | |||
| 826 | /* macro to call 'luaO_pushvfstring' correctly */ | ||
| 827 | #define pushvfstring(L, argp, fmt, msg) \ | ||
| 828 | { va_start(argp, fmt); \ | ||
| 829 | msg = luaO_pushvfstring(L, fmt, argp); \ | ||
| 830 | va_end(argp); \ | ||
| 831 | if (msg == NULL) luaD_throw(L, LUA_ERRMEM); /* only after 'va_end' */ } | ||
| 832 | |||
| 833 | |||
| 825 | LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x); | 834 | LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x); |
| 826 | LUAI_FUNC lu_byte luaO_ceillog2 (unsigned int x); | 835 | LUAI_FUNC lu_byte luaO_ceillog2 (unsigned int x); |
| 827 | LUAI_FUNC lu_byte luaO_codeparam (unsigned int p); | 836 | LUAI_FUNC lu_byte luaO_codeparam (unsigned int p); |
