CLEANUP: lua: remove hard-coded sizeof() in object creations and mallocs
Last bug was an example of a side effect of abuse of copy-paste, but
there are other places at risk, so better fix all occurrences of sizeof
to really reference the object size in order to limit the risks in the
future.
diff --git a/src/hlua.c b/src/hlua.c
index d764fa5..ad2c22f 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -2709,7 +2709,7 @@
* transaction object.
*/
lua_newtable(L);
- hs = lua_newuserdata(L, sizeof(struct hlua_smp));
+ hs = lua_newuserdata(L, sizeof(*hs));
lua_rawseti(L, -2, 0);
hs->s = txn->s;
@@ -2813,7 +2813,7 @@
* same than the TXN object.
*/
lua_newtable(L);
- hs = lua_newuserdata(L, sizeof(struct hlua_smp));
+ hs = lua_newuserdata(L, sizeof(*hs));
lua_rawseti(L, -2, 0);
hs->s = txn->s;
@@ -2932,7 +2932,7 @@
* same than the TXN object.
*/
lua_newtable(L);
- ht = lua_newuserdata(L, sizeof(struct hlua_txn));
+ ht = lua_newuserdata(L, sizeof(*ht));
lua_rawseti(L, -2, 0);
ht->s = txn->s;
@@ -3330,7 +3330,7 @@
*/
/* Create the object: obj[0] = userdata. */
lua_newtable(L);
- hs = lua_newuserdata(L, sizeof(struct hlua_txn));
+ hs = lua_newuserdata(L, sizeof(*hs));
lua_rawseti(L, -2, 0);
hs->s = s;
@@ -3513,7 +3513,7 @@
mark = MAY_LJMP(luaL_checkinteger(L, 2));
if ((cli_conn = objt_conn(htxn->s->si[0].end)) && conn_ctrl_ready(cli_conn))
- setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(int));
+ setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
#endif
return 0;
}
@@ -4033,8 +4033,7 @@
ref = MAY_LJMP(hlua_checkfunction(L, 2));
/* Allocate and fill the sample fetch keyword struct. */
- sck = malloc(sizeof(struct sample_conv_kw_list) +
- sizeof(struct sample_conv) * 2);
+ sck = malloc(sizeof(*sck) + sizeof(struct sample_conv) * 2);
if (!sck)
WILL_LJMP(luaL_error(L, "lua out of memory error."));
fcn = malloc(sizeof(*fcn));
@@ -4094,8 +4093,7 @@
ref = MAY_LJMP(hlua_checkfunction(L, 2));
/* Allocate and fill the sample fetch keyword struct. */
- sfk = malloc(sizeof(struct sample_fetch_kw_list) +
- sizeof(struct sample_fetch) * 2);
+ sfk = malloc(sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
if (!sfk)
WILL_LJMP(luaL_error(L, "lua out of memory error."));
fcn = malloc(sizeof(*fcn));