lua-users home
lua-l archive

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


Em seg., 9 de nov. de 2020 às 13:28, Ranier Vilela <ranier.vf@gmail.com> escreveu:
Hi,

More one suggestion to Lua code:

at lua_newstate function, the loop for initializing a fixed array,
can be securely changed by memset.

diff --git a/lstate.c b/lstate.c
index 42274292..153b000b 100644
--- a/lstate.c
+++ b/lstate.c
@@ -392,7 +392,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
   g->gcstepsize = LUAI_GCSTEPSIZE;
   setgcparam(g->genmajormul, LUAI_GENMAJORMUL);
   g->genminormul = LUAI_GENMINORMUL;
-  for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
+  memset(g->mt, 0, sizeof(struct Table *) * (size_t) LUA_NUMTAGS);  
   if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
     /* memory allocation error: free partial state */
     close_state(L);
Another place:

diff --git a/lundump.c b/lundump.c
index 5aa55c44..46ed812e 100644
--- a/lundump.c
+++ b/lundump.c
@@ -191,10 +191,9 @@ static void loadConstants (LoadState *S, Proto *f) {
 static void loadProtos (LoadState *S, Proto *f) {
   int i;
   int n = loadInt(S);
-  f->p = luaM_newvectorchecked(S->L, n, Proto *);
   f->sizep = n;
-  for (i = 0; i < n; i++)
-    f->p[i] = NULL;
+  f->p = luaM_newvectorchecked(S->L, n, Proto *);
+  memset(f->p, NULL, sizeof(Proto *) * (size_t) n);
   for (i = 0; i < n; i++) {
     f->p[i] = luaF_newproto(S->L);
     luaC_objbarrier(S->L, f, f->p[i]);

regards,
Ranier Vilela

Attachment: avoid_loop_for_init_fixed_array.patch
Description: Binary data