Bläddra i källkod

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'.
Roberto Ierusalimschy 3 månader sedan
förälder
incheckning
e055905914
4 ändrade filer med 13 tillägg och 16 borttagningar
  1. 1 5
      lapi.c
  2. 1 5
      lcode.c
  3. 2 6
      ldebug.c
  4. 9 0
      lobject.h

+ 1 - 5
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;
 }

+ 1 - 5
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 <token>" from final message */
   luaX_syntaxerror(ls, msg);
 }

+ 2 - 6
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' */

+ 9 - 0
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);