lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

More new assorted suggestions for the Lua.

10. Avoid calling strlen at lua_addstring, when adding a constant literal string.

diff --git a/lauxlib.c b/lauxlib.c
index 73504389..e28872cd 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -139,7 +139,7 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
     luaL_addstring(&b, msg);
     luaL_addchar(&b, '\n');
   }
-  luaL_addstring(&b, "stack traceback:");
+  luaL_addsltring(&b, "stack traceback:", sizeof("stack traceback:") - 1);
   while (lua_getstack(L1, level++, &ar)) {
     if (limit2show-- == 0) {  /* too many levels? */
       int n = last - level - LEVELS2 + 1;  /* number of levels to skip */
@@ -157,7 +157,7 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
       pushfuncname(L, &ar);
       luaL_addvalue(&b);
       if (ar.istailcall)
-        luaL_addstring(&b, "\n\t(...tail calls...)");
+        luaL_addlstring(&b, "\n\t(...tail calls...)", sizeof("\n\t(...tail calls...)") -1);
     }
   }
   luaL_pushresult(&b);

11. Avoid calling strlen at lua_addstring, when adding a constant literal string.

diff --git a/loadlib.c b/loadlib.c
index c0ec9a13..627cece5 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -469,9 +469,9 @@ static const char *getnextfilename (char **path, char *end) {
 static void pusherrornotfound (lua_State *L, const char *path) {
   luaL_Buffer b;
   luaL_buffinit(L, &b);
-  luaL_addstring(&b, "no file '");
+  luaL_addlstring(&b, "no file '", sizeof("no file '") - 1);
   luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '");
-  luaL_addstring(&b, "'");
+  luaL_addchar(&b, '\'');
   luaL_pushresult(&b);
 }

12. Avoid calling strlen at lua_addstring, when adding a constant literal string.

@@ -628,7 +628,7 @@ static void findloader (lua_State *L, const char *name) {
   luaL_buffinit(L, &msg);
   /*  iterate over available searchers to find a loader */
   for (i = 1; ; i++) {
-    luaL_addstring(&msg, "\n\t");  /* error-message prefix */
+    luaL_addlstring(&msg, "\n\t", sizeof("\n\t") - 1);  /* error-message prefix */
     if (lua_rawgeti(L, 3, i) == LUA_TNIL) {  /* no more searchers? */
       lua_pop(L, 1);  /* remove nil */
       luaL_buffsub(&msg, 2);  /* remove prefix */

13. Avoid calling strlen at lua_pushstring, when pushing a constant literal string.

diff --git a/ltablib.c b/ltablib.c
index d344a47e..d56f5394 100644
--- a/ltablib.c
+++ b/ltablib.c
@@ -33,8 +33,8 @@
 #define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n))
 
 
-static int checkfield (lua_State *L, const char *key, int n) {
-  lua_pushstring(L, key);
+static int checkfield (lua_State *L, const char *key, size_t len, int n) {
+  lua_pushlstring(L, key, len);
   return (lua_rawget(L, -n) != LUA_TNIL);
 }
 
@@ -47,9 +47,9 @@ static void checktab (lua_State *L, int arg, int what) {
   if (lua_type(L, arg) != LUA_TTABLE) {  /* is it not a table? */
     int n = 1;  /* number of elements to pop */
     if (lua_getmetatable(L, arg) &&  /* must have metatable */
-        (!(what & TAB_R) || checkfield(L, "__index", ++n)) &&
-        (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) &&
-        (!(what & TAB_L) || checkfield(L, "__len", ++n))) {
+        (!(what & TAB_R) || checkfield(L, "__index", sizeof("__index") - 1, ++n)) &&
+        (!(what & TAB_W) || checkfield(L, "__newindex", sizeof("__newindex") - 1, ++n)) &&
+        (!(what & TAB_L) || checkfield(L, "__len", sizeof("__len") - 1, ++n))) {
       lua_pop(L, n);  /* pop metatable and tested metamethods */
     }
     else

regards,
Ranier Vilela

Attachment: all2.patch
Description: Binary data